Skip to content

Commit

Permalink
fix(core): update ESLint config file references from cjs to mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Jan 16, 2025
1 parent 9e6861b commit 2a78ad4
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion e2e/angular/src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Move Angular Project', () => {
expect(moveOutput).toContain(`CREATE ${newPath}/tsconfig.app.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/tsconfig.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/tsconfig.spec.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/eslint.config.cjs`);
expect(moveOutput).toContain(`CREATE ${newPath}/eslint.config.mjs`);
expect(moveOutput).toContain(`CREATE ${newPath}/public/favicon.ico`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/index.html`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/main.ts`);
Expand Down
4 changes: 2 additions & 2 deletions e2e/angular/src/projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ describe('Angular Projects', () => {

it('should lint correctly with eslint and handle external HTML files and inline templates', async () => {
// disable the prefer-standalone rule for app1 which is not standalone
let app1EslintConfig = readFile(`${app1}/eslint.config.cjs`);
let app1EslintConfig = readFile(`${app1}/eslint.config.mjs`);
app1EslintConfig = app1EslintConfig.replace(
`'@angular-eslint/directive-selector': [`,
`'@angular-eslint/prefer-standalone': 'off',
'@angular-eslint/directive-selector': [`
);
updateFile(`${app1}/eslint.config.cjs`, app1EslintConfig);
updateFile(`${app1}/eslint.config.mjs`, app1EslintConfig);

// check apps and lib pass linting for initial generated code
runCLI(`run-many --target lint --projects=${app1},${lib1} --parallel`);
Expand Down
24 changes: 12 additions & 12 deletions e2e/eslint/src/linter-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ describe('Linter (legacy)', () => {
env: { NX_ADD_PLUGINS: 'false' },
});
checkFilesExist(
'eslint.config.cjs',
`apps/${myapp}/eslint.config.cjs`,
`libs/${mylib}/eslint.config.cjs`,
`libs/${mylib2}/eslint.config.cjs`
'eslint.config.mjs',
`apps/${myapp}/eslint.config.mjs`,
`libs/${mylib}/eslint.config.mjs`,
`libs/${mylib2}/eslint.config.mjs`
);
checkFilesDoNotExist(
'.eslintrc.json',
Expand All @@ -164,12 +164,12 @@ describe('Linter (legacy)', () => {

// move eslint.config one step up
// to test the absence of the flat eslint config in the project root folder
renameFile(`libs/${mylib2}/eslint.config.cjs`, `libs/eslint.config.cjs`);
renameFile(`libs/${mylib2}/eslint.config.mjs`, `libs/eslint.config.mjs`);
updateFile(
`libs/eslint.config.cjs`,
readFile(`libs/eslint.config.cjs`).replace(
`../../eslint.config.cjs`,
`../eslint.config.cjs`
`libs/eslint.config.mjs`,
readFile(`libs/eslint.config.mjs`).replace(
`../../eslint.config.mjs`,
`../eslint.config.mjs`
)
);

Expand Down Expand Up @@ -202,9 +202,9 @@ describe('Linter (legacy)', () => {
env: { NX_ADD_PLUGINS: 'false' },
});
checkFilesExist(
'eslint.config.cjs',
`${mylib}/eslint.config.cjs`,
'eslint.base.config.cjs'
'eslint.config.mjs',
`${mylib}/eslint.config.mjs`,
'eslint.base.config.mjs'
);
checkFilesDoNotExist(
'.eslintrc.json',
Expand Down
4 changes: 2 additions & 2 deletions e2e/eslint/src/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ describe('Linter', () => {
runCLI(`generate @nx/js:lib ${jsLib} --linter eslint`);

checkFilesExist(
`${reactLib}/eslint.config.cjs`,
`${jsLib}/eslint.config.cjs`
`${reactLib}/eslint.config.mjs`,
`${jsLib}/eslint.config.mjs`
);
checkFilesDoNotExist(
`${reactLib}/.eslintrc.json`,
Expand Down
4 changes: 2 additions & 2 deletions e2e/nx/src/__snapshots__/extras.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports[`Extra Nx Misc Tests task graph inputs should correctly expand dependent
],
"lib-base-123": [
"libs/lib-base-123/README.md",
"libs/lib-base-123/eslint.config.cjs",
"libs/lib-base-123/eslint.config.mjs",
"libs/lib-base-123/jest.config.ts",
"libs/lib-base-123/package.json",
"libs/lib-base-123/project.json",
Expand All @@ -39,7 +39,7 @@ exports[`Extra Nx Misc Tests task graph inputs should correctly expand dependent
],
"lib-dependent-123": [
"libs/lib-dependent-123/README.md",
"libs/lib-dependent-123/eslint.config.cjs",
"libs/lib-dependent-123/eslint.config.mjs",
"libs/lib-dependent-123/jest.config.ts",
"libs/lib-dependent-123/package.json",
"libs/lib-dependent-123/project.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('@nx/eslint:lint-project', () => {

expect(tree.read('eslint.config.mjs', 'utf-8')).toMatchInlineSnapshot(`
"import nx from "@nx/eslint-plugin";
export default [
...nx.configs["flat/base"],
...nx.configs["flat/typescript"],
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ function replaceFlatConfigPaths(
`require('${newPath}')` +
newConfig.slice(match.index + match[0].length);
}

// Handle import statements
const importRegex = RegExp(/import\s+.*?\s+from\s+['"](.*)['"]/g);
while ((match = importRegex.exec(newConfig)) !== null) {
const oldPath = match[1];
const newPath = offsetFilePath(sourceRoot, oldPath, offset, tree);

// Replace the old path with the updated path
newConfig =
newConfig.slice(0, match.index + match[0].indexOf(oldPath)) +
newPath +
newConfig.slice(match.index + match[0].indexOf(oldPath) + oldPath.length);
}
// replace projects
const projectRegex = RegExp(/project:\s?\[?['"](.*)['"]\]?/g);
while ((match = projectRegex.exec(newConfig)) !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ describe('ast-utils', () => {
);
expect(result).toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.config.mjs";
export default [
...baseConfig,
{
Expand Down Expand Up @@ -254,6 +255,7 @@ describe('ast-utils', () => {
);
expect(result).toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.config.mjs";
export default [
...config,
...baseConfig,
Expand Down
16 changes: 9 additions & 7 deletions packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function removeOverridesFromLintConfig(content: string): string {
const format = content.includes('export default') ? 'mjs' : 'cjs';

const exportsArray =
format === 'mjs' ? findAllBlocks(source) : findModuleExports(source);
format === 'mjs' ? findExportDefault(source) : findModuleExports(source);
if (!exportsArray) {
return content;
}
Expand All @@ -51,7 +51,7 @@ export function removeOverridesFromLintConfig(content: string): string {
}

// TODO Change name
function findAllBlocks(source: ts.SourceFile): ts.NodeArray<ts.Node> {
function findExportDefault(source: ts.SourceFile): ts.NodeArray<ts.Node> {
return ts.forEachChild(source, function analyze(node) {
if (
ts.isExportAssignment(node) &&
Expand Down Expand Up @@ -103,7 +103,7 @@ export function hasOverride(
);
const format = content.includes('export default') ? 'mjs' : 'cjs';
const exportsArray =
format === 'mjs' ? findAllBlocks(source) : findModuleExports(source);
format === 'mjs' ? findExportDefault(source) : findModuleExports(source);
if (!exportsArray) {
return false;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export function replaceOverride(
);
const format = content.includes('export default') ? 'mjs' : 'cjs';
const exportsArray =
format === 'mjs' ? findAllBlocks(source) : findModuleExports(source);
format === 'mjs' ? findExportDefault(source) : findModuleExports(source);
if (!exportsArray) {
return content;
}
Expand Down Expand Up @@ -683,7 +683,9 @@ function addBlockToFlatConfigExportESM(

const updatedSource = ts.factory.updateSourceFile(source, updatedStatements);

return printer.printFile(updatedSource);
return printer
.printFile(updatedSource)
.replace(/export default/, '\nexport default');
}

function addBlockToFlatConfigExportCJS(
Expand Down Expand Up @@ -918,7 +920,7 @@ export function removeCompatExtends(
const changes: StringChange[] = [];
const format = content.includes('export default') ? 'mjs' : 'cjs';
const exportsArray =
format === 'mjs' ? findAllBlocks(source) : findModuleExports(source);
format === 'mjs' ? findExportDefault(source) : findModuleExports(source);

if (!exportsArray) {
return content;
Expand Down Expand Up @@ -989,7 +991,7 @@ export function removePredefinedConfigs(
const changes: StringChange[] = [];
let removeImport = true;
const exportsArray =
format === 'mjs' ? findAllBlocks(source) : findModuleExports(source);
format === 'mjs' ? findExportDefault(source) : findModuleExports(source);
if (!exportsArray) {
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ describe('app', () => {
baseDirectory: dirname(fileURLToPath(import.meta.url)),
recommendedConfig: js.configs.recommended,
});
export default [
...compat.extends('next', 'next/core-web-vitals'),
...baseConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const compat = new FlatCompat({
baseDirectory: dirname(fileURLToPath(import.meta.url)),
recommendedConfig: js.configs.recommended,
});
export default [
...baseConfig,
{
Expand Down Expand Up @@ -469,6 +470,7 @@ const compat = new FlatCompat({
baseDirectory: dirname(fileURLToPath(import.meta.url)),
recommendedConfig: js.configs.recommended,
});
export default [
...baseConfig,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ module.exports = [
exports[`library should support eslint flat config ESM 1`] = `
"import vue from 'eslint-plugin-vue';
import baseConfig from '../eslint.config.mjs';
export default [
...baseConfig,
...vue.configs['flat/recommended'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export function updateEslintConfig(
!tree.exists('.eslintrc.json') &&
!tree.exists('eslint.config.js') &&
!tree.exists('eslint.config.cjs') &&
!tree.exists('eslint.config.mjs') &&
!tree.exists('.eslintrc.base.json') &&
!tree.exists('eslint.base.config.js') &&
!tree.exists('eslint.base.config.cjs')
!tree.exists('eslint.base.config.cjs') &&
!tree.exists('eslint.base.config.mjs')
) {
return;
}
Expand Down

0 comments on commit 2a78ad4

Please sign in to comment.