-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add migration for updating eslint flat config with new …
…extensions
- Loading branch information
Showing
5 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
packages/eslint/src/migrations/update-20-3-0/add-file-extensions-to-overrides.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import { type Tree } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import migration from './add-file-extensions-to-overrides'; | ||
|
||
describe('add-file-extensions-to-overrides', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it('should add .cjs, .mjs, .cts, .mts file extensions to overrides converted using convert-to-flat-config', async () => { | ||
tree.write( | ||
'eslint.config.js', | ||
`const { FlatCompat } = require('@eslint/eslintrc'); | ||
const js = require('@eslint/js'); | ||
const nxEslintPlugin = require('@nx/eslint-plugin'); | ||
const compat = new FlatCompat({ | ||
baseDirectory: __dirname, | ||
recommendedConfig: js.configs.recommended, | ||
}); | ||
module.exports = [ | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/typescript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.ts', '**/*.tsx'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/javascript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.js', '**/*.jsx'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
];` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const updated = tree.read('eslint.config.js', 'utf-8'); | ||
expect(updated).toMatchInlineSnapshot(` | ||
"const { FlatCompat } = require('@eslint/eslintrc'); | ||
const js = require('@eslint/js'); | ||
const nxEslintPlugin = require('@nx/eslint-plugin'); | ||
const compat = new FlatCompat({ | ||
baseDirectory: __dirname, | ||
recommendedConfig: js.configs.recommended, | ||
}); | ||
module.exports = [ | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/typescript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.ts', '**/*.tsx', '**/*.cts', '**/*.mts'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/javascript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.js', '**/*.jsx', '**/*.cjs', '**/*.mjs'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
];" | ||
`); | ||
}); | ||
|
||
it('should handle duplicates', async () => { | ||
tree.write( | ||
'eslint.config.js', | ||
`const { FlatCompat } = require('@eslint/eslintrc'); | ||
const js = require('@eslint/js'); | ||
const nxEslintPlugin = require('@nx/eslint-plugin'); | ||
const compat = new FlatCompat({ | ||
baseDirectory: __dirname, | ||
recommendedConfig: js.configs.recommended, | ||
}); | ||
module.exports = [ | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/javascript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.js', '**/*.jsx', '**/*.mjs'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
];` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const updated = tree.read('eslint.config.js', 'utf-8'); | ||
expect(updated).toMatchInlineSnapshot(` | ||
"const { FlatCompat } = require('@eslint/eslintrc'); | ||
const js = require('@eslint/js'); | ||
const nxEslintPlugin = require('@nx/eslint-plugin'); | ||
const compat = new FlatCompat({ | ||
baseDirectory: __dirname, | ||
recommendedConfig: js.configs.recommended, | ||
}); | ||
module.exports = [ | ||
...compat | ||
.config({ | ||
extends: ['plugin:@nx/javascript'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
];" | ||
`); | ||
}); | ||
|
||
it('should not update if plugin:@nx/javascript and plugin:@nx/typescript are not used', async () => { | ||
const original = `const { FlatCompat } = require('@eslint/eslintrc'); | ||
const js = require('@eslint/js'); | ||
const nxEslintPlugin = require('@nx/eslint-plugin'); | ||
const compat = new FlatCompat({ | ||
baseDirectory: __dirname, | ||
recommendedConfig: js.configs.recommended, | ||
}); | ||
module.exports = [ | ||
...compat | ||
.config({ | ||
extends: ['plugin:@acme/foo'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.ts', '**/*.tsx'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
...compat | ||
.config({ | ||
extends: ['plugin:@acme/bar'], | ||
}) | ||
.map((config) => ({ | ||
...config, | ||
files: ['**/*.js', '**/*.jsx'], | ||
rules: { | ||
...config.rules, | ||
}, | ||
})), | ||
];`; | ||
tree.write('eslint.config.js', original); | ||
|
||
await migration(tree); | ||
|
||
const updated = tree.read('eslint.config.js', 'utf-8'); | ||
expect(updated).toEqual(original); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
packages/eslint/src/migrations/update-20-3-0/add-file-extensions-to-overrides.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { type Tree } from '@nx/devkit'; | ||
import * as ts from 'typescript'; | ||
import { findNodes, replaceChange } from '@nx/js'; | ||
|
||
export default async function (tree: Tree): Promise<void> { | ||
let rootConfig: string; | ||
|
||
// NOTE: we don't support generating ESM base config currently so they are not handled. | ||
for (const candidate of ['eslint.config.js', 'eslint.config.cjs']) { | ||
if (tree.exists(candidate)) { | ||
rootConfig = candidate; | ||
break; | ||
} | ||
} | ||
|
||
if (!rootConfig) return; | ||
|
||
updateOverrideFileExtensions( | ||
tree, | ||
rootConfig, | ||
'plugin:@nx/typescript', | ||
[`'**/*.ts'`, `'**/*.tsx'`], | ||
[`'**/*.cts'`, `'**/*.mts'`] | ||
); | ||
|
||
updateOverrideFileExtensions( | ||
tree, | ||
rootConfig, | ||
'plugin:@nx/javascript', | ||
[`'**/*.js'`, `'**/*.jsx'`], | ||
[`'**/*.cjs'`, `'**/*.mjs'`] | ||
); | ||
} | ||
|
||
function updateOverrideFileExtensions( | ||
tree: Tree, | ||
configFile: string, | ||
plugin: string, | ||
matchingExts: string[], | ||
newExts: string[] | ||
): void { | ||
const content = tree.read(configFile, 'utf-8'); | ||
const source = ts.createSourceFile( | ||
'', | ||
content, | ||
ts.ScriptTarget.Latest, | ||
true, | ||
ts.ScriptKind.JS | ||
); | ||
let compatNode: ts.SpreadElement; | ||
|
||
const spreadElementNodes = findNodes( | ||
source, | ||
ts.SyntaxKind.SpreadElement | ||
) as ts.SpreadElement[]; | ||
for (const a of spreadElementNodes) { | ||
const assignmentNodes = findNodes( | ||
a, | ||
ts.SyntaxKind.PropertyAssignment | ||
) as ts.PropertyAssignment[]; | ||
if (assignmentNodes.length === 0) continue; | ||
for (const b of assignmentNodes) { | ||
if ( | ||
b.name.getText() === 'extends' && | ||
b.initializer.getText().includes(plugin) | ||
) { | ||
compatNode = a; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (compatNode) { | ||
const arrayNodes = findNodes( | ||
compatNode, | ||
ts.SyntaxKind.ArrayLiteralExpression | ||
) as ts.ArrayLiteralExpression[]; | ||
for (const a of arrayNodes) { | ||
if ( | ||
matchingExts.every((ext) => a.elements.some((e) => e.getText() === ext)) | ||
) { | ||
const exts = new Set(a.elements.map((e) => e.getText())); | ||
for (const ext of newExts) { | ||
exts.add(ext); | ||
} | ||
replaceChange( | ||
tree, | ||
source, | ||
configFile, | ||
a.getStart(a.getSourceFile()), | ||
`[${Array.from(exts).join(', ')}]`, | ||
a.getText() | ||
).getText(); | ||
} | ||
} | ||
} | ||
} |