From 144e88178430677d4773756819506481c540d68e Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Thu, 12 Dec 2024 14:52:33 -0500 Subject: [PATCH] feat(eslint): rename eslint.config.js to eslint.config.cjs to they are resolved as CommonJS --- .../packages/eslint/dependency-checks.md | 2 +- .../tips-n-tricks/migrating-to-flat-eslint.md | 4 +- e2e/angular/src/misc.test.ts | 2 +- e2e/angular/src/projects.test.ts | 4 +- e2e/eslint/src/linter-legacy.test.ts | 24 +++--- e2e/eslint/src/linter.test.ts | 4 +- e2e/js/src/js-packaging.test.ts | 10 --- e2e/nx/src/__snapshots__/extras.test.ts.snap | 4 +- .../src/generators/library/library.spec.ts | 6 +- .../migrate-from-angular-cli.spec.ts.snap | 6 +- .../generators/ng-add/utilities/workspace.ts | 4 +- ...e-angular-eslint-prefer-standalone.spec.ts | 30 +++---- .../remove-angular-eslint-rules.spec.ts | 38 ++++----- .../remove-angular-eslint-rules.ts | 4 +- .../__directory__/tsconfig.json__ext__ | 2 +- packages/eslint/docs/eslint-examples.md | 4 +- .../src/executors/lint/lint.impl.spec.ts | 14 ++-- .../lint/utility/eslint-utils.spec.ts | 2 +- .../convert-to-flat-config/generator.spec.ts | 80 +++++++++---------- .../convert-to-flat-config/generator.ts | 16 ++-- .../convert-to-inferred.spec.ts | 12 +-- .../convert-to-inferred.ts | 2 +- .../src/generators/init/init-migration.ts | 8 +- .../eslint/src/generators/init/init.spec.ts | 4 +- packages/eslint/src/generators/init/init.ts | 4 +- .../lint-project/lint-project.spec.ts | 2 +- .../lint-project/setup-root-eslint.ts | 2 +- .../src/generators/utils/eslint-file.spec.ts | 50 ++++++------ .../utils/flat-config/ast-utils.spec.ts | 50 ++++++------ packages/eslint/src/plugins/plugin.spec.ts | 6 +- packages/eslint/src/utils/config-file.ts | 2 +- .../application/application.spec.ts | 6 +- .../application/lib/add-linting.spec.ts | 6 +- .../remove-eslint-rules-patch.spec.ts | 8 +- .../__snapshots__/application.spec.ts.snap | 4 +- .../application/application.spec.ts | 4 +- .../angular/standalone-workspace.ts | 10 ++- .../release/config/config.spec.ts | 4 +- .../application/application.impl.spec.ts | 2 +- .../application/application.impl.ts | 46 ----------- .../__snapshots__/library.spec.ts.snap | 2 +- .../src/generators/library/library.spec.ts | 6 +- ...figuration-in-new-destination.spec.ts.snap | 2 +- ...t-configuration-in-new-destination.spec.ts | 2 +- .../generators/move/lib/move-project-files.ts | 1 + .../move/lib/update-eslint-config.spec.ts | 38 ++++----- .../move/lib/update-eslint-config.ts | 4 +- .../move/lib/update-project-root-files.ts | 8 +- 48 files changed, 254 insertions(+), 301 deletions(-) diff --git a/docs/shared/packages/eslint/dependency-checks.md b/docs/shared/packages/eslint/dependency-checks.md index e50ef5fc7362b..7a3a3398f305b 100644 --- a/docs/shared/packages/eslint/dependency-checks.md +++ b/docs/shared/packages/eslint/dependency-checks.md @@ -69,7 +69,7 @@ Sometimes we intentionally want to add or remove a dependency from our `package. "checkObsoleteDependencies": true, // toggle to disable "checkVersionMismatches": true, // toggle to disable "ignoredDependencies": ["lodash"], // these libs will be omitted from checks - "ignoredFiles": ["webpack.config.js", "eslint.config.js"], // list of files that should be skipped for check + "ignoredFiles": ["webpack.config.js", "eslint.config.cjs"], // list of files that should be skipped for check "includeTransitiveDependencies": true, // collect dependencies transitively from children "useLocalPathsForWorkspaceDependencies": true // toggle to disable } diff --git a/docs/shared/recipes/tips-n-tricks/migrating-to-flat-eslint.md b/docs/shared/recipes/tips-n-tricks/migrating-to-flat-eslint.md index 6d254d91749e7..a075d3da03780 100644 --- a/docs/shared/recipes/tips-n-tricks/migrating-to-flat-eslint.md +++ b/docs/shared/recipes/tips-n-tricks/migrating-to-flat-eslint.md @@ -10,12 +10,12 @@ See below a direct comparison between `JSON`, `JS` and `Flat` config: {% tabs %} {% tab label="Flat" %} -```js {% fileName="eslint.config.js" %} +```js {% fileName="eslint.config.cjs" %} // the older versions were magically interpreting all the imports // in flat config we do it explicitly const nxPlugin = require('@nx/eslint-plugin'); const js = require('@eslint/js'); -const baseConfig = require('./eslint.base.config.js'); +const baseConfig = require('./eslint.base.config.cjs'); const globals = require('globals'); const jsoncParser = require('jsonc-eslint-parser'); const tsParser = require('@typescript-eslint/parser'); diff --git a/e2e/angular/src/misc.test.ts b/e2e/angular/src/misc.test.ts index a6199b0549d79..f61900397dd14 100644 --- a/e2e/angular/src/misc.test.ts +++ b/e2e/angular/src/misc.test.ts @@ -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.js`); + expect(moveOutput).toContain(`CREATE ${newPath}/eslint.config.cjs`); expect(moveOutput).toContain(`CREATE ${newPath}/public/favicon.ico`); expect(moveOutput).toContain(`CREATE ${newPath}/src/index.html`); expect(moveOutput).toContain(`CREATE ${newPath}/src/main.ts`); diff --git a/e2e/angular/src/projects.test.ts b/e2e/angular/src/projects.test.ts index 9a630ef1d9a19..103f19f6d220a 100644 --- a/e2e/angular/src/projects.test.ts +++ b/e2e/angular/src/projects.test.ts @@ -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.js`); + let app1EslintConfig = readFile(`${app1}/eslint.config.cjs`); app1EslintConfig = app1EslintConfig.replace( `'@angular-eslint/directive-selector': [`, `'@angular-eslint/prefer-standalone': 'off', '@angular-eslint/directive-selector': [` ); - updateFile(`${app1}/eslint.config.js`, app1EslintConfig); + updateFile(`${app1}/eslint.config.cjs`, app1EslintConfig); // check apps and lib pass linting for initial generated code runCLI(`run-many --target lint --projects=${app1},${lib1} --parallel`); diff --git a/e2e/eslint/src/linter-legacy.test.ts b/e2e/eslint/src/linter-legacy.test.ts index b561d4419fbb8..f921c94c31cf0 100644 --- a/e2e/eslint/src/linter-legacy.test.ts +++ b/e2e/eslint/src/linter-legacy.test.ts @@ -150,10 +150,10 @@ describe('Linter (legacy)', () => { env: { NX_ADD_PLUGINS: 'false' }, }); checkFilesExist( - 'eslint.config.js', - `apps/${myapp}/eslint.config.js`, - `libs/${mylib}/eslint.config.js`, - `libs/${mylib2}/eslint.config.js` + 'eslint.config.cjs', + `apps/${myapp}/eslint.config.cjs`, + `libs/${mylib}/eslint.config.cjs`, + `libs/${mylib2}/eslint.config.cjs` ); checkFilesDoNotExist( '.eslintrc.json', @@ -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.js`, `libs/eslint.config.js`); + renameFile(`libs/${mylib2}/eslint.config.cjs`, `libs/eslint.config.cjs`); updateFile( - `libs/eslint.config.js`, - readFile(`libs/eslint.config.js`).replace( - `../../eslint.config.js`, - `../eslint.config.js` + `libs/eslint.config.cjs`, + readFile(`libs/eslint.config.cjs`).replace( + `../../eslint.config.cjs`, + `../eslint.config.cjs` ) ); @@ -202,9 +202,9 @@ describe('Linter (legacy)', () => { env: { NX_ADD_PLUGINS: 'false' }, }); checkFilesExist( - 'eslint.config.js', - `${mylib}/eslint.config.js`, - 'eslint.base.config.js' + 'eslint.config.cjs', + `${mylib}/eslint.config.cjs`, + 'eslint.base.config.cjs' ); checkFilesDoNotExist( '.eslintrc.json', diff --git a/e2e/eslint/src/linter.test.ts b/e2e/eslint/src/linter.test.ts index 2a3c1faa1e580..963466368af2b 100644 --- a/e2e/eslint/src/linter.test.ts +++ b/e2e/eslint/src/linter.test.ts @@ -615,8 +615,8 @@ describe('Linter', () => { runCLI(`generate @nx/js:lib ${jsLib} --linter eslint`); checkFilesExist( - `${reactLib}/eslint.config.js`, - `${jsLib}/eslint.config.js` + `${reactLib}/eslint.config.cjs`, + `${jsLib}/eslint.config.cjs` ); checkFilesDoNotExist( `${reactLib}/.eslintrc.json`, diff --git a/e2e/js/src/js-packaging.test.ts b/e2e/js/src/js-packaging.test.ts index 3f8fae5256de1..43e92e606af03 100644 --- a/e2e/js/src/js-packaging.test.ts +++ b/e2e/js/src/js-packaging.test.ts @@ -178,16 +178,6 @@ describe('packaging libs', () => { `libs/${swcEsmLib}/src/index.ts`, `export * from './lib/${swcEsmLib}.js';` ); - // We also need to update the eslint config file extensions to be explicitly commonjs - // TODO: re-evaluate this once we support ESM eslint configs - renameFile( - `libs/${tscEsmLib}/eslint.config.js`, - `libs/${tscEsmLib}/eslint.config.cjs` - ); - renameFile( - `libs/${swcEsmLib}/eslint.config.js`, - `libs/${swcEsmLib}/eslint.config.cjs` - ); // Add additional entry points for `exports` field updateJson(join('libs', tscLib, 'project.json'), (json) => { diff --git a/e2e/nx/src/__snapshots__/extras.test.ts.snap b/e2e/nx/src/__snapshots__/extras.test.ts.snap index fbc8c2974e87d..c06c81383bef8 100644 --- a/e2e/nx/src/__snapshots__/extras.test.ts.snap +++ b/e2e/nx/src/__snapshots__/extras.test.ts.snap @@ -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.js", + "libs/lib-base-123/eslint.config.cjs", "libs/lib-base-123/jest.config.ts", "libs/lib-base-123/package.json", "libs/lib-base-123/project.json", @@ -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.js", + "libs/lib-dependent-123/eslint.config.cjs", "libs/lib-dependent-123/jest.config.ts", "libs/lib-dependent-123/package.json", "libs/lib-dependent-123/project.json", diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index 64253f5de7e0c..b49e802c8145c 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -1204,14 +1204,14 @@ describe('lib', () => { describe('--linter', () => { describe('eslint', () => { it('should add valid eslint JSON configuration which extends from Nx presets (flat config)', async () => { - tree.write('eslint.config.js', ''); + tree.write('eslint.config.cjs', ''); await runLibraryGeneratorWithOpts({ linter: Linter.EsLint }); - const eslintConfig = tree.read('my-lib/eslint.config.js', 'utf-8'); + const eslintConfig = tree.read('my-lib/eslint.config.cjs', 'utf-8'); expect(eslintConfig).toMatchInlineSnapshot(` "const nx = require("@nx/eslint-plugin"); - const baseConfig = require("../eslint.config.js"); + const baseConfig = require("../eslint.config.cjs"); module.exports = [ ...baseConfig, diff --git a/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap b/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap index 9b8175483ae34..92ad21afdcba4 100644 --- a/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap +++ b/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap @@ -93,7 +93,7 @@ exports[`workspace move to nx layout should create nx.json 1`] = ` "!{projectRoot}/**/*.spec.[jt]s", "!{projectRoot}/karma.conf.js", "!{projectRoot}/.eslintrc.json", - "!{projectRoot}/eslint.config.js", + "!{projectRoot}/eslint.config.cjs", ], "sharedGlobals": [], }, @@ -104,7 +104,7 @@ exports[`workspace move to nx layout should create nx.json 1`] = ` "default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore", - "{workspaceRoot}/eslint.config.js", + "{workspaceRoot}/eslint.config.cjs", ], }, "build": { @@ -129,7 +129,7 @@ exports[`workspace move to nx layout should create nx.json 1`] = ` "inputs": [ "default", "{workspaceRoot}/.eslintrc.json", - "{workspaceRoot}/eslint.config.js", + "{workspaceRoot}/eslint.config.cjs", ], }, "test": { diff --git a/packages/angular/src/generators/ng-add/utilities/workspace.ts b/packages/angular/src/generators/ng-add/utilities/workspace.ts index 492a351fca0e8..6d5a349f2465e 100644 --- a/packages/angular/src/generators/ng-add/utilities/workspace.ts +++ b/packages/angular/src/generators/ng-add/utilities/workspace.ts @@ -64,7 +64,7 @@ export function createNxJson( ] : []), ...(targets.lint - ? ['!{projectRoot}/.eslintrc.json', '!{projectRoot}/eslint.config.js'] + ? ['!{projectRoot}/.eslintrc.json', '!{projectRoot}/eslint.config.cjs'] : []), ].filter(Boolean), }, @@ -85,7 +85,7 @@ export function createNxJson( inputs: [ 'default', '{workspaceRoot}/.eslintrc.json', - '{workspaceRoot}/eslint.config.js', + '{workspaceRoot}/eslint.config.cjs', ], cache: true, } diff --git a/packages/angular/src/migrations/update-20-2-0/disable-angular-eslint-prefer-standalone.spec.ts b/packages/angular/src/migrations/update-20-2-0/disable-angular-eslint-prefer-standalone.spec.ts index 0fb1b4bedc586..4cb5acc84d497 100644 --- a/packages/angular/src/migrations/update-20-2-0/disable-angular-eslint-prefer-standalone.spec.ts +++ b/packages/angular/src/migrations/update-20-2-0/disable-angular-eslint-prefer-standalone.spec.ts @@ -241,9 +241,9 @@ describe('disable-angular-eslint-prefer-standalone', () => { describe('flat config', () => { it('should not disable @angular-eslint/prefer-standalone when it is set', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -255,7 +255,7 @@ describe('disable-angular-eslint-prefer-standalone', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { @@ -268,9 +268,9 @@ describe('disable-angular-eslint-prefer-standalone', () => { }); it('should not disable @angular-eslint/prefer-standalone when there are multiple overrides for angular eslint and the rule is set in one of them', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -291,7 +291,7 @@ describe('disable-angular-eslint-prefer-standalone', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { @@ -313,9 +313,9 @@ describe('disable-angular-eslint-prefer-standalone', () => { }); it('should disable @angular-eslint/prefer-standalone in an existing override for angular eslint', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -336,7 +336,7 @@ describe('disable-angular-eslint-prefer-standalone', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { @@ -363,9 +363,9 @@ describe('disable-angular-eslint-prefer-standalone', () => { }); it('should disable @angular-eslint/prefer-standalone in an existing override for ts files', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -377,7 +377,7 @@ describe('disable-angular-eslint-prefer-standalone', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { @@ -393,9 +393,9 @@ describe('disable-angular-eslint-prefer-standalone', () => { }); it('should disable @angular-eslint/prefer-standalone in a new override', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.html'], @@ -407,7 +407,7 @@ describe('disable-angular-eslint-prefer-standalone', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { diff --git a/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.spec.ts b/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.spec.ts index 985ee4c3aaf37..54667cdc475ca 100644 --- a/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.spec.ts +++ b/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.spec.ts @@ -172,9 +172,9 @@ describe('remove-angular-eslint-rules', () => { describe('flat config', () => { it.each(rulesToRemove)('should remove %s rule', async (rule) => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -186,15 +186,15 @@ describe('remove-angular-eslint-rules', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')).not.toContain( + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')).not.toContain( rule ); }); it('should remove multiple rules', async () => { - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', + 'apps/app1/eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -210,7 +210,7 @@ describe('remove-angular-eslint-rules', () => { await migration(tree); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` "module.exports = [ { @@ -224,7 +224,7 @@ describe('remove-angular-eslint-rules', () => { it('should handle rules set in the root config', async () => { tree.write( - 'eslint.config.js', + 'eslint.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -238,8 +238,8 @@ describe('remove-angular-eslint-rules', () => { ` ); tree.write( - 'apps/app1/eslint.config.js', - `const baseConfig = require('../../eslint.config.js'); + 'apps/app1/eslint.config.cjs', + `const baseConfig = require('../../eslint.config.cjs'); module.exports = [...baseConfig]; ` @@ -247,7 +247,7 @@ describe('remove-angular-eslint-rules', () => { await migration(tree); - expect(tree.read('eslint.config.js', 'utf8')).toMatchInlineSnapshot(` + expect(tree.read('eslint.config.cjs', 'utf8')).toMatchInlineSnapshot(` "module.exports = [ { files: ['**/*.ts'], @@ -256,9 +256,9 @@ describe('remove-angular-eslint-rules', () => { ]; " `); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` - "const baseConfig = require('../../eslint.config.js'); + "const baseConfig = require('../../eslint.config.cjs'); module.exports = [...baseConfig]; " @@ -267,7 +267,7 @@ describe('remove-angular-eslint-rules', () => { it('should handle rules set in the root base config', async () => { tree.write( - 'eslint.base.config.js', + 'eslint.base.config.cjs', `module.exports = [ { files: ['*.ts'], @@ -280,10 +280,10 @@ describe('remove-angular-eslint-rules', () => { ]; ` ); - tree.write('eslint.config.js', 'module.exports = [];'); + tree.write('eslint.config.cjs', 'module.exports = [];'); tree.write( - 'apps/app1/eslint.config.js', - `const baseConfig = require('../../eslint.base.config.js'); + 'apps/app1/eslint.config.cjs', + `const baseConfig = require('../../eslint.base.config.cjs'); module.exports = [...baseConfig]; ` @@ -291,7 +291,7 @@ describe('remove-angular-eslint-rules', () => { await migration(tree); - expect(tree.read('eslint.base.config.js', 'utf8')).toMatchInlineSnapshot(` + expect(tree.read('eslint.base.config.cjs', 'utf8')).toMatchInlineSnapshot(` "module.exports = [ { files: ['**/*.ts'], @@ -300,9 +300,9 @@ describe('remove-angular-eslint-rules', () => { ]; " `); - expect(tree.read('apps/app1/eslint.config.js', 'utf8')) + expect(tree.read('apps/app1/eslint.config.cjs', 'utf8')) .toMatchInlineSnapshot(` - "const baseConfig = require('../../eslint.base.config.js'); + "const baseConfig = require('../../eslint.base.config.cjs'); module.exports = [...baseConfig]; " diff --git a/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.ts b/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.ts index abab46fc440b7..d6679109990d7 100644 --- a/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.ts +++ b/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.ts @@ -35,8 +35,8 @@ export default async function (tree: Tree) { } /** - * We need to handle both a root config file (e.g. eslint.config.js) and a - * potential base config file (e.g. eslint.base.config.js). We can't use + * We need to handle both a root config file (e.g. eslint.config.cjs) and a + * potential base config file (e.g. eslint.base.config.cjs). We can't use * `findEslintFile` because it would return only one or the other depending * on whether a root is provided and the existence of the files. So, we * handle each of them separately. diff --git a/packages/cypress/src/generators/base-setup/files/tsconfig/ts-solution/__directory__/tsconfig.json__ext__ b/packages/cypress/src/generators/base-setup/files/tsconfig/ts-solution/__directory__/tsconfig.json__ext__ index 88dc50152e31c..f7f3ee6097808 100644 --- a/packages/cypress/src/generators/base-setup/files/tsconfig/ts-solution/__directory__/tsconfig.json__ext__ +++ b/packages/cypress/src/generators/base-setup/files/tsconfig/ts-solution/__directory__/tsconfig.json__ext__ @@ -17,5 +17,5 @@ <%_ if (jsx) { _%>"<%= offsetFromProjectRoot %>**/*.cy.jsx",<%_ } _%> "<%= offsetFromProjectRoot %>**/*.d.ts" ], - "exclude": ["dist"<% if (linter === 'eslint') { %>, "eslint.config.js"<% } %>] + "exclude": ["dist"<% if (linter === 'eslint') { %>, "eslint.config.js", "eslint.config.cjs", "eslint.config.mjs"<% } %>] } diff --git a/packages/eslint/docs/eslint-examples.md b/packages/eslint/docs/eslint-examples.md index 03f7651a5e98b..4830039142231 100644 --- a/packages/eslint/docs/eslint-examples.md +++ b/packages/eslint/docs/eslint-examples.md @@ -96,12 +96,12 @@ We can also set this via project configuration as a default option. "outputs": ["{options.outputFile}"], "options": { "lintFilePatterns": ["apps/frontend/**/*.ts"], - "eslintConfig": "eslint.config.js" + "eslintConfig": "eslint.config.cjs" } } ``` -**Note:** In contrast to other configuration formats, the `Flat Config` requires that all configuration files are converted to `eslint.config.js`. Built-in migrations and generators support only `.eslintrc.json` at the moment. +**Note:** In contrast to other configuration formats, the `Flat Config` requires that all configuration files are converted to `eslint.config.cjs`. Built-in migrations and generators support only `.eslintrc.json` at the moment. {% /tab %} {% /tabs %} diff --git a/packages/eslint/src/executors/lint/lint.impl.spec.ts b/packages/eslint/src/executors/lint/lint.impl.spec.ts index ca460f1d79205..23067df279cab 100644 --- a/packages/eslint/src/executors/lint/lint.impl.spec.ts +++ b/packages/eslint/src/executors/lint/lint.impl.spec.ts @@ -436,7 +436,7 @@ Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how it('should intercept the error from `@typescript-eslint` regarding missing parserServices and provide a more detailed user-facing message logging the found flat config', async () => { setupMocks(); - tempFs.createFileSync('apps/proj/eslint.config.js', ''); + tempFs.createFileSync('apps/proj/eslint.config.cjs', ''); tempFs.createFileSync('apps/proj/src/some-file.ts', ''); mockLintFiles.mockImplementation(() => { @@ -456,7 +456,7 @@ Occurred while linting ${mockContext.root}/apps/proj/src/some-file.ts` ); expect(console.error).toHaveBeenCalledWith( ` -Error: You have attempted to use the lint rule "@typescript-eslint/await-thenable" which requires the full TypeScript type-checker to be available, but you do not have "parserOptions.project" configured to point at your project tsconfig.json files in the relevant TypeScript file "overrides" block of your ESLint config "apps/proj/eslint.config.js" +Error: You have attempted to use the lint rule "@typescript-eslint/await-thenable" which requires the full TypeScript type-checker to be available, but you do not have "parserOptions.project" configured to point at your project tsconfig.json files in the relevant TypeScript file "overrides" block of your ESLint config "apps/proj/eslint.config.cjs" Occurred while linting ${mockContext.root}/apps/proj/src/some-file.ts Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how to resolve this issue. @@ -466,7 +466,7 @@ Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how it('should intercept the error from `@typescript-eslint` regarding missing parserServices and provide a more detailed user-facing message logging the found flat config at the workspace root', async () => { setupMocks(); - tempFs.createFileSync('eslint.config.js', ''); + tempFs.createFileSync('eslint.config.cjs', ''); tempFs.createFileSync('apps/proj/src/some-file.ts', ''); mockLintFiles.mockImplementation(() => { @@ -486,7 +486,7 @@ Occurred while linting ${mockContext.root}/apps/proj/src/some-file.ts` ); expect(console.error).toHaveBeenCalledWith( ` -Error: You have attempted to use the lint rule "@typescript-eslint/await-thenable" which requires the full TypeScript type-checker to be available, but you do not have "parserOptions.project" configured to point at your project tsconfig.json files in the relevant TypeScript file "overrides" block of your ESLint config "eslint.config.js" +Error: You have attempted to use the lint rule "@typescript-eslint/await-thenable" which requires the full TypeScript type-checker to be available, but you do not have "parserOptions.project" configured to point at your project tsconfig.json files in the relevant TypeScript file "overrides" block of your ESLint config "eslint.config.cjs" Occurred while linting ${mockContext.root}/apps/proj/src/some-file.ts Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how to resolve this issue. @@ -906,15 +906,15 @@ Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how expect(result).toEqual({ success: true }); }); - it('should pass path to eslint.config.js to resolveAndInstantiateESLint if it is unspecified and we are using flag configuration', async () => { + it('should pass path to eslint.config.cjs to resolveAndInstantiateESLint if it is unspecified and we are using flag configuration', async () => { setupMocks(); jest.spyOn(fs, 'existsSync').mockReturnValue(true); await lintExecutor(createValidRunBuilderOptions(), mockContext); expect(mockResolveAndInstantiateESLint).toHaveBeenCalledWith( - `${mockContext.root}/apps/proj/eslint.config.js`, + `${mockContext.root}/apps/proj/eslint.config.cjs`, { lintFilePatterns: [], - eslintConfig: 'apps/proj/eslint.config.js', + eslintConfig: 'apps/proj/eslint.config.cjs', fix: true, cache: true, cacheLocation: 'cacheLocation1/proj', diff --git a/packages/eslint/src/executors/lint/utility/eslint-utils.spec.ts b/packages/eslint/src/executors/lint/utility/eslint-utils.spec.ts index a4cb7ed8f18aa..abc48ee24e258 100644 --- a/packages/eslint/src/executors/lint/utility/eslint-utils.spec.ts +++ b/packages/eslint/src/executors/lint/utility/eslint-utils.spec.ts @@ -162,7 +162,7 @@ describe('eslint-utils', () => { }); describe('ESLint Flat Config', () => { - it('should throw if a non eslint.config.js or eslint.config.cjs file is used with ESLint Flat Config', async () => { + it('should throw if a non eslint.config.cjs or eslint.config.cjs file is used with ESLint Flat Config', async () => { await expect( resolveAndInstantiateESLint('./.eslintrc.json', {} as any, true) ).rejects.toThrowErrorMatchingInlineSnapshot( diff --git a/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts b/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts index ae0a73e12b396..467e6e5259033 100644 --- a/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts +++ b/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts @@ -82,19 +82,19 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy(); expect( - tree.read('libs/test-lib/eslint.config.js', 'utf-8') + tree.read('libs/test-lib/eslint.config.cjs', 'utf-8') ).toMatchSnapshot(); // check nx.json changes const nxJson = readJson(tree, 'nx.json'); expect(nxJson.targetDefaults.lint.inputs).toContain( - '{workspaceRoot}/eslint.config.js' + '{workspaceRoot}/eslint.config.cjs' ); expect(nxJson.namedInputs.production).toContain( - '!{projectRoot}/eslint.config.js' + '!{projectRoot}/eslint.config.cjs' ); }); @@ -112,19 +112,19 @@ describe('convert-to-flat-config generator', () => { await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy(); expect( - tree.read('libs/test-lib/eslint.config.js', 'utf-8') + tree.read('libs/test-lib/eslint.config.cjs', 'utf-8') ).toMatchSnapshot(); // check nx.json changes const nxJson = readJson(tree, 'nx.json'); expect(nxJson.targetDefaults.lint.inputs).toContain( - '{workspaceRoot}/eslint.config.js' + '{workspaceRoot}/eslint.config.cjs' ); expect(nxJson.namedInputs.production).toContain( - '!{projectRoot}/eslint.config.js' + '!{projectRoot}/eslint.config.cjs' ); }); @@ -142,19 +142,19 @@ describe('convert-to-flat-config generator', () => { await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy(); expect( - tree.read('libs/test-lib/eslint.config.js', 'utf-8') + tree.read('libs/test-lib/eslint.config.cjs', 'utf-8') ).toMatchSnapshot(); // check nx.json changes const nxJson = readJson(tree, 'nx.json'); expect(nxJson.targetDefaults.lint.inputs).toContain( - '{workspaceRoot}/eslint.config.js' + '{workspaceRoot}/eslint.config.cjs' ); expect(nxJson.namedInputs.production).toContain( - '!{projectRoot}/eslint.config.js' + '!{projectRoot}/eslint.config.cjs' ); }); @@ -171,7 +171,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchInlineSnapshot(` + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchInlineSnapshot(` "const { FlatCompat } = require('@eslint/eslintrc'); const js = require('@eslint/js'); const nxEslintPlugin = require('@nx/eslint-plugin'); @@ -227,7 +227,7 @@ describe('convert-to-flat-config generator', () => { ]; " `); - expect(tree.read('libs/test-lib/eslint.config.js', 'utf-8')) + expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const baseConfig = require('../../eslint.config.js'); @@ -266,7 +266,7 @@ describe('convert-to-flat-config generator', () => { tree.write('.eslintignore', 'ignore/me'); await convertToFlatConfigGenerator(tree, options); - const config = tree.read('eslint.config.js', 'utf-8'); + const config = tree.read('eslint.config.cjs', 'utf-8'); expect(config).toContain('ignore/me'); expect(config).toMatchSnapshot(); expect(tree.exists('.eslintignore')).toBeFalsy(); @@ -290,7 +290,7 @@ describe('convert-to-flat-config generator', () => { await convertToFlatConfigGenerator(tree, options); expect( - tree.read('libs/test-lib/eslint.config.js', 'utf-8') + tree.read('libs/test-lib/eslint.config.cjs', 'utf-8') ).toMatchSnapshot(); expect(tree.exists('another-folder/.myeslintignore')).toBeFalsy(); expect(tree.exists('libs/test-lib/.eslintignore')).toBeFalsy(); @@ -316,7 +316,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add env configuration', async () => { @@ -335,7 +335,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add global configuration', async () => { @@ -353,7 +353,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add global and env configuration', async () => { @@ -374,7 +374,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add plugins', async () => { @@ -395,7 +395,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add parser', async () => { @@ -411,7 +411,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot(); }); it('should add linter options', async () => { @@ -427,7 +427,7 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchInlineSnapshot(` + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchInlineSnapshot(` "const { FlatCompat } = require('@eslint/eslintrc'); const js = require('@eslint/js'); const nxEslintPlugin = require('@nx/eslint-plugin'); @@ -510,11 +510,11 @@ describe('convert-to-flat-config generator', () => { } ); - expect(tree.exists('eslint.config.js')).toBeFalsy(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeFalsy(); + expect(tree.exists('eslint.config.cjs')).toBeFalsy(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeFalsy(); await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy(); }); it('should convert project if target is defined via plugin as object', async () => { @@ -545,11 +545,11 @@ describe('convert-to-flat-config generator', () => { } ); - expect(tree.exists('eslint.config.js')).toBeFalsy(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeFalsy(); + expect(tree.exists('eslint.config.cjs')).toBeFalsy(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeFalsy(); await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy(); }); it('should handle parser options even if parser is extended', async () => { @@ -589,9 +589,9 @@ describe('convert-to-flat-config generator', () => { }); await convertToFlatConfigGenerator(tree, options); - expect(tree.exists('apps/dx-assets-ui/eslint.config.js')).toBeTruthy(); - expect(tree.exists('eslint.config.js')).toBeTruthy(); - expect(tree.read('apps/dx-assets-ui/eslint.config.js', 'utf-8')) + expect(tree.exists('apps/dx-assets-ui/eslint.config.cjs')).toBeTruthy(); + expect(tree.exists('eslint.config.cjs')).toBeTruthy(); + expect(tree.read('apps/dx-assets-ui/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const baseConfig = require('../../eslint.config.js'); diff --git a/packages/eslint/src/generators/convert-to-flat-config/generator.ts b/packages/eslint/src/generators/convert-to-flat-config/generator.ts index 5e68663fda610..1cfbc7a527e76 100644 --- a/packages/eslint/src/generators/convert-to-flat-config/generator.ts +++ b/packages/eslint/src/generators/convert-to-flat-config/generator.ts @@ -42,10 +42,10 @@ export async function convertToFlatConfigGenerator( const eslintIgnoreFiles = new Set(['.eslintignore']); - // convert root eslint config to eslint.config.js + // convert root eslint config to eslint.config.cjs convertRootToFlatConfig(tree, eslintFile); - // convert project eslint files to eslint.config.js + // convert project eslint files to eslint.config.cjs const projects = getProjects(tree); for (const [project, projectConfig] of projects) { convertProjectToFlatConfig( @@ -77,13 +77,13 @@ export default convertToFlatConfigGenerator; function convertRootToFlatConfig(tree: Tree, eslintFile: string) { if (/\.base\.(js|json|yml|yaml)$/.test(eslintFile)) { - convertConfigToFlatConfig(tree, '', eslintFile, 'eslint.base.config.js'); + convertConfigToFlatConfig(tree, '', eslintFile, 'eslint.base.config.cjs'); } convertConfigToFlatConfig( tree, '', eslintFile.replace('.base.', '.'), - 'eslint.config.js' + 'eslint.config.cjs' ); } @@ -132,7 +132,7 @@ function convertProjectToFlatConfig( tree, projectConfig.root, eslintFile, - 'eslint.config.js', + 'eslint.config.cjs', ignorePath ); eslintIgnoreFiles.add(`${projectConfig.root}/.eslintignore`); @@ -151,17 +151,17 @@ function updateNxJsonConfig(tree: Tree) { updateJson(tree, 'nx.json', (json: NxJsonConfiguration) => { if (json.targetDefaults?.lint?.inputs) { const inputSet = new Set(json.targetDefaults.lint.inputs); - inputSet.add('{workspaceRoot}/eslint.config.js'); + inputSet.add('{workspaceRoot}/eslint.config.cjs'); json.targetDefaults.lint.inputs = Array.from(inputSet); } if (json.targetDefaults?.['@nx/eslint:lint']?.inputs) { const inputSet = new Set(json.targetDefaults['@nx/eslint:lint'].inputs); - inputSet.add('{workspaceRoot}/eslint.config.js'); + inputSet.add('{workspaceRoot}/eslint.config.cjs'); json.targetDefaults['@nx/eslint:lint'].inputs = Array.from(inputSet); } if (json.namedInputs?.production) { const inputSet = new Set(json.namedInputs.production); - inputSet.add('!{projectRoot}/eslint.config.js'); + inputSet.add('!{projectRoot}/eslint.config.cjs'); json.namedInputs.production = Array.from(inputSet); } return json; diff --git a/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.spec.ts b/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.spec.ts index a482c80ce9d78..cd4b550961981 100644 --- a/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.spec.ts +++ b/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.spec.ts @@ -573,7 +573,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', ], }; updateNxJson(tree, nxJson); @@ -589,7 +589,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', { externalDependencies: ['eslint'] }, ]); }); @@ -604,7 +604,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', { externalDependencies: ['eslint-plugin-react'] }, ], }; @@ -621,7 +621,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', { externalDependencies: ['eslint-plugin-react', 'eslint'] }, ]); }); @@ -636,7 +636,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', { externalDependencies: ['eslint', 'eslint-plugin-react'] }, ], }; @@ -653,7 +653,7 @@ describe('Eslint - Convert Executors To Plugin', () => { 'default', '{projectRoot}/.eslintrc.json', '{projectRoot}/.eslintignore', - '{projectRoot}/eslint.config.js', + '{projectRoot}/eslint.config.cjs', { externalDependencies: ['eslint', 'eslint-plugin-react'] }, ]); }); diff --git a/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.ts b/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.ts index a3237fbb8f2b4..c50ea644554b4 100644 --- a/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.ts +++ b/packages/eslint/src/generators/convert-to-inferred/convert-to-inferred.ts @@ -65,7 +65,7 @@ function postTargetTransformer( 'default', '{workspaceRoot}/.eslintrc.json', '{workspaceRoot}/.eslintignore', - '{workspaceRoot}/eslint.config.js', + '{workspaceRoot}/eslint.config.cjs', ].includes(input) ); if (inputs.length === 0) { diff --git a/packages/eslint/src/generators/init/init-migration.ts b/packages/eslint/src/generators/init/init-migration.ts index d304973c8118f..6e43b7a8613cf 100644 --- a/packages/eslint/src/generators/init/init-migration.ts +++ b/packages/eslint/src/generators/init/init-migration.ts @@ -57,9 +57,9 @@ export function migrateConfigToMonorepoStyle( keepExistingVersions ); tree.write( - tree.exists('eslint.config.js') - ? 'eslint.base.config.js' - : 'eslint.config.js', + tree.exists('eslint.config.cjs') + ? 'eslint.base.config.cjs' + : 'eslint.config.cjs', getGlobalFlatEslintConfiguration() ); } else { @@ -134,7 +134,7 @@ function migrateEslintFile(projectEslintPath: string, tree: Tree) { let config = tree.read(projectEslintPath, 'utf-8'); // remove @nx plugin config = removePlugin(config, '@nx', '@nx/eslint-plugin-nx'); - // extend eslint.base.config.js + // extend eslint.base.config.cjs config = addImportToFlatConfig( config, 'baseConfig', diff --git a/packages/eslint/src/generators/init/init.spec.ts b/packages/eslint/src/generators/init/init.spec.ts index 57f87c29d48d9..a43b6935e4235 100644 --- a/packages/eslint/src/generators/init/init.spec.ts +++ b/packages/eslint/src/generators/init/init.spec.ts @@ -113,7 +113,7 @@ describe('@nx/eslint:init', () => { 'default', '{workspaceRoot}/.eslintrc.json', '{workspaceRoot}/.eslintignore', - '{workspaceRoot}/eslint.config.js', + '{workspaceRoot}/eslint.config.cjs', ], }); }); @@ -137,7 +137,7 @@ describe('@nx/eslint:init', () => { 'default', '{workspaceRoot}/.eslintrc.json', '{workspaceRoot}/.eslintignore', - '{workspaceRoot}/eslint.config.js', + '{workspaceRoot}/eslint.config.cjs', ], }); }); diff --git a/packages/eslint/src/generators/init/init.ts b/packages/eslint/src/generators/init/init.ts index 7eedb55492abb..f297352d56d50 100644 --- a/packages/eslint/src/generators/init/init.ts +++ b/packages/eslint/src/generators/init/init.ts @@ -28,7 +28,7 @@ function updateProductionFileset(tree: Tree) { const productionFileSet = nxJson.namedInputs?.production; if (productionFileSet) { productionFileSet.push('!{projectRoot}/.eslintrc.json'); - productionFileSet.push('!{projectRoot}/eslint.config.js'); + productionFileSet.push('!{projectRoot}/eslint.config.cjs'); // Dedupe and set nxJson.namedInputs.production = Array.from(new Set(productionFileSet)); } @@ -45,7 +45,7 @@ function addTargetDefaults(tree: Tree) { 'default', `{workspaceRoot}/.eslintrc.json`, `{workspaceRoot}/.eslintignore`, - `{workspaceRoot}/eslint.config.js`, + `{workspaceRoot}/eslint.config.cjs`, ]; updateNxJson(tree, nxJson); } diff --git a/packages/eslint/src/generators/lint-project/lint-project.spec.ts b/packages/eslint/src/generators/lint-project/lint-project.spec.ts index 3fa984d3ccb49..3ca6d6c474b8c 100644 --- a/packages/eslint/src/generators/lint-project/lint-project.spec.ts +++ b/packages/eslint/src/generators/lint-project/lint-project.spec.ts @@ -53,7 +53,7 @@ describe('@nx/eslint:lint-project', () => { skipFormat: true, }); - expect(tree.read('eslint.config.js', 'utf-8')).toMatchInlineSnapshot(` + expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchInlineSnapshot(` "const nx = require("@nx/eslint-plugin"); module.exports = [ diff --git a/packages/eslint/src/generators/lint-project/setup-root-eslint.ts b/packages/eslint/src/generators/lint-project/setup-root-eslint.ts index 6a486e7f71e0b..3de9e0f99b397 100644 --- a/packages/eslint/src/generators/lint-project/setup-root-eslint.ts +++ b/packages/eslint/src/generators/lint-project/setup-root-eslint.ts @@ -71,7 +71,7 @@ function setUpLegacyRootEslintRc(tree: Tree, options: SetupRootEsLintOptions) { function setUpRootFlatConfig(tree: Tree, options: SetupRootEsLintOptions) { tree.write( - 'eslint.config.js', + 'eslint.config.cjs', getGlobalFlatEslintConfiguration(options.rootProject) ); diff --git a/packages/eslint/src/generators/utils/eslint-file.spec.ts b/packages/eslint/src/generators/utils/eslint-file.spec.ts index 619128da6753e..dd5976b08202f 100644 --- a/packages/eslint/src/generators/utils/eslint-file.spec.ts +++ b/packages/eslint/src/generators/utils/eslint-file.spec.ts @@ -122,10 +122,10 @@ describe('@nx/eslint:lint-file', () => { }); it('should add extends to flat config', () => { - tree.write('eslint.config.js', 'module.exports = {};'); + tree.write('eslint.config.cjs', 'module.exports = {};'); tree.write( - 'apps/demo/eslint.config.js', - `const baseConfig = require("../../eslint.config.js"); + 'apps/demo/eslint.config.cjs', + `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, @@ -143,11 +143,11 @@ module.exports = [ addExtendsToLintConfig(tree, 'apps/demo', 'plugin:playwright/recommend'); - expect(tree.read('apps/demo/eslint.config.js', 'utf-8')) + expect(tree.read('apps/demo/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -177,10 +177,10 @@ module.exports = [ packageJson: { name: 'eslint', version: '9.0.0' }, path: '', }); - tree.write('eslint.config.js', 'module.exports = {};'); + tree.write('eslint.config.cjs', 'module.exports = {};'); tree.write( - 'apps/demo/eslint.config.js', - `const baseConfig = require("../../eslint.config.js"); + 'apps/demo/eslint.config.cjs', + `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, @@ -201,12 +201,12 @@ module.exports = [ needCompatFixup: true, }); - expect(tree.read('apps/demo/eslint.config.js', 'utf-8')) + expect(tree.read('apps/demo/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); const { fixupConfigRules } = require("@eslint/compat"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -236,10 +236,10 @@ module.exports = [ packageJson: { name: 'eslint', version: '9.0.0' }, path: '', }); - tree.write('eslint.config.js', 'module.exports = {};'); + tree.write('eslint.config.cjs', 'module.exports = {};'); tree.write( - 'apps/demo/eslint.config.js', - `const baseConfig = require("../../eslint.config.js"); + 'apps/demo/eslint.config.cjs', + `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, @@ -264,12 +264,12 @@ module.exports = [ { name: 'incompatible-plugin3', needCompatFixup: true }, ]); - expect(tree.read('apps/demo/eslint.config.js', 'utf-8')) + expect(tree.read('apps/demo/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); const { fixupConfigRules } = require("@eslint/compat"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -307,10 +307,10 @@ module.exports = [ packageJson: { name: 'eslint', version: '8.0.0' }, path: '', }); - tree.write('eslint.config.js', 'module.exports = {};'); + tree.write('eslint.config.cjs', 'module.exports = {};'); tree.write( - 'apps/demo/eslint.config.js', - `const baseConfig = require("../../eslint.config.js"); + 'apps/demo/eslint.config.cjs', + `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, @@ -331,11 +331,11 @@ module.exports = [ needCompatFixup: true, }); - expect(tree.read('apps/demo/eslint.config.js', 'utf-8')) + expect(tree.read('apps/demo/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -362,10 +362,10 @@ module.exports = [ describe('replaceOverridesInLintConfig', () => { it('should replace overrides when using flat config', () => { - tree.write('eslint.config.js', 'module.exports = {};'); + tree.write('eslint.config.cjs', 'module.exports = {};'); tree.write( - 'apps/demo/eslint.config.js', - `const baseConfig = require("../../eslint.config.js"); + 'apps/demo/eslint.config.cjs', + `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, @@ -428,11 +428,11 @@ module.exports = [ }, ]); - expect(tree.read('apps/demo/eslint.config.js', 'utf-8')) + expect(tree.read('apps/demo/eslint.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, diff --git a/packages/eslint/src/generators/utils/flat-config/ast-utils.spec.ts b/packages/eslint/src/generators/utils/flat-config/ast-utils.spec.ts index 3d17c64a3a8c5..1909cbbd15c6f 100644 --- a/packages/eslint/src/generators/utils/flat-config/ast-utils.spec.ts +++ b/packages/eslint/src/generators/utils/flat-config/ast-utils.spec.ts @@ -182,7 +182,7 @@ describe('ast-utils', () => { describe('addBlockToFlatConfigExport', () => { it('should inject block to the end of the file', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -204,7 +204,7 @@ describe('ast-utils', () => { }) ); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -228,7 +228,7 @@ describe('ast-utils', () => { }); it('should inject spread to the beginning of the file', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -246,7 +246,7 @@ describe('ast-utils', () => { { insertAtTheEnd: false } ); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...config, @@ -266,7 +266,7 @@ describe('ast-utils', () => { describe('addImportToFlatConfig', () => { it('should inject import if not found', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -285,7 +285,7 @@ describe('ast-utils', () => { ); expect(result).toMatchInlineSnapshot(` "const varName = require("@myorg/awesome-config"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -302,7 +302,7 @@ describe('ast-utils', () => { it('should update import if already found', () => { const content = `const { varName } = require("@myorg/awesome-config"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -321,7 +321,7 @@ describe('ast-utils', () => { ); expect(result).toMatchInlineSnapshot(` "const { varName, otherName, someName } = require("@myorg/awesome-config"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -338,7 +338,7 @@ describe('ast-utils', () => { it('should not inject import if already exists', () => { const content = `const { varName, otherName } = require("@myorg/awesome-config"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -360,7 +360,7 @@ describe('ast-utils', () => { it('should not update import if already exists', () => { const content = `const varName = require("@myorg/awesome-config"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -409,7 +409,7 @@ describe('ast-utils', () => { describe('addCompatToFlatConfig', () => { it('should add compat to config', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...baseConfig, { @@ -425,7 +425,7 @@ describe('ast-utils', () => { expect(result).toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -446,7 +446,7 @@ describe('ast-utils', () => { }); it('should add only partially compat to config if parts exist', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); const js = require("@eslint/js"); module.exports = [ ...baseConfig, @@ -462,7 +462,7 @@ describe('ast-utils', () => { const result = addFlatCompatToFlatConfig(content); expect(result).toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const js = require("@eslint/js"); const compat = new FlatCompat({ @@ -485,7 +485,7 @@ describe('ast-utils', () => { it('should not add compat to config if exist', () => { const content = `const FlatCompat = require("@eslint/eslintrc"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const js = require("@eslint/js"); const compat = new FlatCompat({ @@ -512,7 +512,7 @@ describe('ast-utils', () => { describe('removeOverridesFromLintConfig', () => { it('should remove all rules from config', () => { const content = `const FlatCompat = require("@eslint/eslintrc"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const js = require("@eslint/js"); const compat = new FlatCompat({ @@ -552,7 +552,7 @@ describe('ast-utils', () => { const result = removeOverridesFromLintConfig(content); expect(result).toMatchInlineSnapshot(` "const FlatCompat = require("@eslint/eslintrc"); - const baseConfig = require("../../eslint.config.js"); + const baseConfig = require("../../eslint.config.cjs"); const js = require("@eslint/js"); const compat = new FlatCompat({ @@ -568,7 +568,7 @@ describe('ast-utils', () => { }); it('should remove all rules from starting with first', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ { @@ -599,7 +599,7 @@ describe('ast-utils', () => { ];`; const result = removeOverridesFromLintConfig(content); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ];" @@ -609,7 +609,7 @@ describe('ast-utils', () => { describe('replaceOverride', () => { it('should find and replace rules in override', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ { @@ -651,7 +651,7 @@ module.exports = [ }) ); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ { @@ -686,7 +686,7 @@ module.exports = [ }); it('should append rules in override', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ { @@ -722,7 +722,7 @@ module.exports = [ }) ); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ { @@ -749,7 +749,7 @@ module.exports = [ }); it('should work for compat overrides', () => { - const content = `const baseConfig = require("../../eslint.config.js"); + const content = `const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...compat.config({ extends: ["plugin:@nx/typescript"] }).map(config => ({ @@ -777,7 +777,7 @@ module.exports = [ }) ); expect(result).toMatchInlineSnapshot(` - "const baseConfig = require("../../eslint.config.js"); + "const baseConfig = require("../../eslint.config.cjs"); module.exports = [ ...compat.config({ extends: ["plugin:@nx/typescript"] }).map(config => ({ diff --git a/packages/eslint/src/plugins/plugin.spec.ts b/packages/eslint/src/plugins/plugin.spec.ts index f392ebcca622c..7470d906a2983 100644 --- a/packages/eslint/src/plugins/plugin.spec.ts +++ b/packages/eslint/src/plugins/plugin.spec.ts @@ -73,13 +73,13 @@ describe('@nx/eslint/plugin', () => { // TODO(leo): dynamic import of the flat config fails with jest: // "TypeError: A dynamic import callback was invoked without --experimental-vm-modules" - // mocking the "eslint.config.js" file import is not working, figure out if there's a way + // mocking the "eslint.config.cjs" file import is not working, figure out if there's a way it.skip('should not create a node for a root level eslint config when accompanied by a project.json, if no src directory is present', async () => { createFiles({ - 'eslint.config.js': `module.exports = {};`, + 'eslint.config.cjs': `module.exports = {};`, 'project.json': `{}`, }); - // NOTE: It should set ESLINT_USE_FLAT_CONFIG to true because of the use of eslint.config.js + // NOTE: It should set ESLINT_USE_FLAT_CONFIG to true because of the use of eslint.config.cjs expect( await invokeCreateNodesOnMatchingFiles(context, { targetName: 'lint' }) ).toMatchInlineSnapshot(` diff --git a/packages/eslint/src/utils/config-file.ts b/packages/eslint/src/utils/config-file.ts index fc1636f3c065b..33e6d6d869658 100644 --- a/packages/eslint/src/utils/config-file.ts +++ b/packages/eslint/src/utils/config-file.ts @@ -19,7 +19,7 @@ export const ESLINT_CONFIG_FILENAMES = [ ]; export const baseEsLintConfigFile = '.eslintrc.base.json'; -export const baseEsLintFlatConfigFile = 'eslint.base.config.js'; +export const baseEsLintFlatConfigFile = 'eslint.base.config.cjs'; export function isFlatConfig(configFilePath: string): boolean { const configFileName = basename(configFilePath); diff --git a/packages/next/src/generators/application/application.spec.ts b/packages/next/src/generators/application/application.spec.ts index a1977fc601f22..fe32567c95be0 100644 --- a/packages/next/src/generators/application/application.spec.ts +++ b/packages/next/src/generators/application/application.spec.ts @@ -624,7 +624,7 @@ describe('app', () => { describe('--linter', () => { describe('default (eslint)', () => { it('should add flat config as needed', async () => { - tree.write('eslint.config.js', ''); + tree.write('eslint.config.cjs', ''); const name = uniq(); await applicationGenerator(tree, { @@ -632,12 +632,12 @@ describe('app', () => { style: 'css', }); - expect(tree.read(`${name}/eslint.config.js`, 'utf-8')) + expect(tree.read(`${name}/eslint.config.cjs`, 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require('@eslint/eslintrc'); const js = require('@eslint/js'); const nx = require('@nx/eslint-plugin'); - const baseConfig = require('../eslint.config.js'); + const baseConfig = require('../eslint.config.cjs'); const compat = new FlatCompat({ baseDirectory: __dirname, diff --git a/packages/next/src/generators/application/lib/add-linting.spec.ts b/packages/next/src/generators/application/lib/add-linting.spec.ts index 37782d4096eaa..b7f1ace6c414c 100644 --- a/packages/next/src/generators/application/lib/add-linting.spec.ts +++ b/packages/next/src/generators/application/lib/add-linting.spec.ts @@ -96,16 +96,16 @@ describe('updateEslint', () => { }); it('should update the flat config', async () => { - tree.write('eslint.config.js', `module.exports = []`); + tree.write('eslint.config.cjs', `module.exports = []`); await addLinting(tree, schema); - expect(tree.read(`${schema.appProjectRoot}/eslint.config.js`, 'utf-8')) + expect(tree.read(`${schema.appProjectRoot}/eslint.config.cjs`, 'utf-8')) .toMatchInlineSnapshot(` "const { FlatCompat } = require("@eslint/eslintrc"); const js = require("@eslint/js"); const nx = require("@nx/eslint-plugin"); - const baseConfig = require("../eslint.config.js"); + const baseConfig = require("../eslint.config.cjs"); const compat = new FlatCompat({ baseDirectory: __dirname, diff --git a/packages/next/src/migrations/update-17-2-7/remove-eslint-rules-patch.spec.ts b/packages/next/src/migrations/update-17-2-7/remove-eslint-rules-patch.spec.ts index 6737d41a51e1c..059bdb8626bed 100644 --- a/packages/next/src/migrations/update-17-2-7/remove-eslint-rules-patch.spec.ts +++ b/packages/next/src/migrations/update-17-2-7/remove-eslint-rules-patch.spec.ts @@ -92,15 +92,15 @@ describe('update-nx-next-dependency', () => { }); it('should remove @next/next/no-html-link-for-pages in flat configs', async () => { - tree.write('eslint.config.js', 'module.exports = []'); + tree.write('eslint.config.cjs', 'module.exports = []'); addProjectConfiguration(tree, 'my-pkg', { root: 'packages/my-pkg', }); tree.write( - `packages/my-pkg/eslint.config.js`, + `packages/my-pkg/eslint.config.cjs`, `const { FlatCompat } = require('@eslint/eslintrc'); - const baseConfig = require('../../eslint.config.js'); + const baseConfig = require('../../eslint.config.cjs'); const js = require('@eslint/js'); const compat = new FlatCompat({ @@ -145,7 +145,7 @@ describe('update-nx-next-dependency', () => { await update(tree); expect( - tree.read(`packages/my-pkg/eslint.config.js`, 'utf-8') + tree.read(`packages/my-pkg/eslint.config.cjs`, 'utf-8') ).not.toContain("'@next/next/no-html-link-for-pages': 'off'"); }); }); diff --git a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap index f080ce37ee798..78d84c22e25a2 100644 --- a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap @@ -35,7 +35,7 @@ exports[`app generated files content - as-provided - my-app general application exports[`app generated files content - as-provided - my-app general application should configure eslint correctly (flat config) 1`] = ` "const { FlatCompat } = require('@eslint/eslintrc'); const js = require('@eslint/js'); -const baseConfig = require('../eslint.config.js'); +const baseConfig = require('../eslint.config.cjs'); const compat = new FlatCompat({ baseDirectory: __dirname, @@ -394,7 +394,7 @@ exports[`app generated files content - as-provided - myApp general application s exports[`app generated files content - as-provided - myApp general application should configure eslint correctly (flat config) 1`] = ` "const { FlatCompat } = require('@eslint/eslintrc'); const js = require('@eslint/js'); -const baseConfig = require('../eslint.config.js'); +const baseConfig = require('../eslint.config.cjs'); const compat = new FlatCompat({ baseDirectory: __dirname, diff --git a/packages/nuxt/src/generators/application/application.spec.ts b/packages/nuxt/src/generators/application/application.spec.ts index d5c222c75a2d1..7044f93383854 100644 --- a/packages/nuxt/src/generators/application/application.spec.ts +++ b/packages/nuxt/src/generators/application/application.spec.ts @@ -66,7 +66,7 @@ describe('app', () => { }); it('should configure eslint correctly (flat config)', async () => { - tree.write('eslint.config.js', ''); + tree.write('eslint.config.cjs', ''); await applicationGenerator(tree, { directory: name, @@ -74,7 +74,7 @@ describe('app', () => { }); expect( - tree.read(`${name}/eslint.config.js`, 'utf-8') + tree.read(`${name}/eslint.config.cjs`, 'utf-8') ).toMatchSnapshot(); }); diff --git a/packages/nx/src/command-line/init/implementation/angular/standalone-workspace.ts b/packages/nx/src/command-line/init/implementation/angular/standalone-workspace.ts index 9b739048c53c0..5204cd6954f4a 100644 --- a/packages/nx/src/command-line/init/implementation/angular/standalone-workspace.ts +++ b/packages/nx/src/command-line/init/implementation/angular/standalone-workspace.ts @@ -91,7 +91,7 @@ function createNxJson( ].filter(Boolean) : []), ...(eslintProjectConfigFile - ? ['!{projectRoot}/.eslintrc.json', '!{projectRoot}/eslint.config.js'] + ? ['!{projectRoot}/.eslintrc.json', '!{projectRoot}/eslint.config.cjs'] : []), ].filter(Boolean), }; @@ -124,8 +124,8 @@ function createNxJson( if (fileExists(join(repoRoot, '.eslintrc.json'))) { inputs.push('{workspaceRoot}/.eslintrc.json'); } - if (fileExists(join(repoRoot, 'eslint.config.js'))) { - inputs.push('{workspaceRoot}/eslint.config.js'); + if (fileExists(join(repoRoot, 'eslint.config.cjs'))) { + inputs.push('{workspaceRoot}/eslint.config.cjs'); } nxJson.targetDefaults.lint = { ...nxJson.targetDefaults.lint, @@ -230,7 +230,9 @@ function projectHasEslintConfig( ): boolean { return ( fileExists(join(project.root, '.eslintrc.json')) || - fileExists(join(project.root, 'eslint.config.js')) + fileExists(join(project.root, 'eslint.config.js')) || + fileExists(join(project.root, 'eslint.config.mjs')) || + fileExists(join(project.root, 'eslint.config.cjs')) ); } diff --git a/packages/nx/src/command-line/release/config/config.spec.ts b/packages/nx/src/command-line/release/config/config.spec.ts index 7c75ef51d7373..290fb2c5e9252 100644 --- a/packages/nx/src/command-line/release/config/config.spec.ts +++ b/packages/nx/src/command-line/release/config/config.spec.ts @@ -10642,7 +10642,7 @@ describe('createNxReleaseConfig()', () => { 'group-1': { projects: 'nx', versionPlans: { - ignorePatternsForPlanCheck: ['**/eslint.config.js'], + ignorePatternsForPlanCheck: ['**/eslint.config.cjs'], }, }, 'group-2': { @@ -10793,7 +10793,7 @@ describe('createNxReleaseConfig()', () => { }, "versionPlans": { "ignorePatternsForPlanCheck": [ - "**/eslint.config.js", + "**/eslint.config.cjs", ], }, }, diff --git a/packages/remix/src/generators/application/application.impl.spec.ts b/packages/remix/src/generators/application/application.impl.spec.ts index 9737ca910b0e4..560f64cbfc525 100644 --- a/packages/remix/src/generators/application/application.impl.spec.ts +++ b/packages/remix/src/generators/application/application.impl.spec.ts @@ -443,7 +443,7 @@ describe('Remix Application', () => { "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts", - "eslint.config.js", + "eslint.config.cjs", "eslint.config.cjs", "eslint.config.mjs", ], diff --git a/packages/remix/src/generators/application/application.impl.ts b/packages/remix/src/generators/application/application.impl.ts index e66b13fd2885d..580d43a00816f 100644 --- a/packages/remix/src/generators/application/application.impl.ts +++ b/packages/remix/src/generators/application/application.impl.ts @@ -308,52 +308,6 @@ export default {...nxPreset}; tasks.push(await addE2E(tree, options)); - // If the project package.json uses type module, and the project uses flat eslint config, we need to make sure the eslint config uses an explicit .cjs extension - // TODO: This could be re-evaluated once we support ESM in eslint configs - if ( - tree.exists(joinPathFragments(options.projectRoot, 'package.json')) && - tree.exists(joinPathFragments(options.projectRoot, 'eslint.config.js')) - ) { - const pkgJson = readJson( - tree, - joinPathFragments(options.projectRoot, 'package.json') - ); - if (pkgJson.type === 'module') { - tree.rename( - joinPathFragments(options.projectRoot, 'eslint.config.js'), - joinPathFragments(options.projectRoot, 'eslint.config.cjs') - ); - visitNotIgnoredFiles(tree, options.projectRoot, (file) => { - if (file.endsWith('eslint.config.js')) { - // Replace any extends on the eslint config to use the .cjs extension - const content = tree.read(file).toString(); - if (content.includes('eslint.config')) { - tree.write( - file, - content - .replace(/eslint\.config'/g, `eslint.config.cjs'`) - .replace(/eslint\.config"/g, `eslint.config.cjs"`) - .replace(/eslint\.config\.js/g, `eslint.config.cjs`) - ); - } - - // If there is no sibling package.json with type commonjs, we need to rename the .js files to .cjs - const siblingPackageJsonPath = joinPathFragments( - dirname(file), - 'package.json' - ); - if (tree.exists(siblingPackageJsonPath)) { - const siblingPkgJson = readJson(tree, siblingPackageJsonPath); - if (siblingPkgJson.type === 'module') { - return; - } - } - tree.rename(file, file.replace('.js', '.cjs')); - } - }); - } - } - addViteTempFilesToGitIgnore(tree); if (!options.skipFormat) { await formatFiles(tree); diff --git a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap index 0981da1f57e59..89fd88a7b05fa 100644 --- a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap @@ -279,7 +279,7 @@ exports[`library should ignore test files in tsconfig.lib.json 1`] = ` exports[`library should support eslint flat config 1`] = ` "const vue = require('eslint-plugin-vue'); -const baseConfig = require('../eslint.config.js'); +const baseConfig = require('../eslint.config.cjs'); module.exports = [ ...baseConfig, diff --git a/packages/vue/src/generators/library/library.spec.ts b/packages/vue/src/generators/library/library.spec.ts index 79ebba4e39a17..67fe3ad6fa02e 100644 --- a/packages/vue/src/generators/library/library.spec.ts +++ b/packages/vue/src/generators/library/library.spec.ts @@ -151,7 +151,7 @@ describe('library', () => { it('should support eslint flat config', async () => { tree.write( - 'eslint.config.js', + 'eslint.config.cjs', `const { FlatCompat } = require('@eslint/eslintrc'); const nxEslintPlugin = require('@nx/eslint-plugin'); const js = require('@eslint/js'); @@ -208,10 +208,10 @@ module.exports = [ await libraryGenerator(tree, defaultSchema); - const eslintJson = tree.read('my-lib/eslint.config.js', 'utf-8'); + const eslintJson = tree.read('my-lib/eslint.config.cjs', 'utf-8'); expect(eslintJson).toMatchSnapshot(); // assert **/*.vue was added to override in base eslint config - const eslintBaseJson = tree.read('eslint.config.js', 'utf-8'); + const eslintBaseJson = tree.read('eslint.config.cjs', 'utf-8'); expect(eslintBaseJson).toContain( `files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx', '**/*.vue'],` ); diff --git a/packages/workspace/src/generators/move/lib/__snapshots__/create-project-configuration-in-new-destination.spec.ts.snap b/packages/workspace/src/generators/move/lib/__snapshots__/create-project-configuration-in-new-destination.spec.ts.snap index 658a6e7771a2e..7d684a3df06d9 100644 --- a/packages/workspace/src/generators/move/lib/__snapshots__/create-project-configuration-in-new-destination.spec.ts.snap +++ b/packages/workspace/src/generators/move/lib/__snapshots__/create-project-configuration-in-new-destination.spec.ts.snap @@ -140,7 +140,7 @@ exports[`moveProjectConfiguration should rename the project correctly except for "default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore", - "{workspaceRoot}/eslint.config.js", + "{workspaceRoot}/eslint.config.cjs", ], "options": {}, "outputs": [ diff --git a/packages/workspace/src/generators/move/lib/create-project-configuration-in-new-destination.spec.ts b/packages/workspace/src/generators/move/lib/create-project-configuration-in-new-destination.spec.ts index 6132e29fe9906..0cd0ac2c0aea1 100644 --- a/packages/workspace/src/generators/move/lib/create-project-configuration-in-new-destination.spec.ts +++ b/packages/workspace/src/generators/move/lib/create-project-configuration-in-new-destination.spec.ts @@ -199,7 +199,7 @@ describe('moveProjectConfiguration', () => { 'default', '{workspaceRoot}/.eslintrc.json', '{workspaceRoot}/.eslintignore', - '{workspaceRoot}/eslint.config.js', + '{workspaceRoot}/eslint.config.cjs', ], executor: '@nx/eslint:lint', outputs: ['{options.outputFile}'], diff --git a/packages/workspace/src/generators/move/lib/move-project-files.ts b/packages/workspace/src/generators/move/lib/move-project-files.ts index d57c8c1fdd440..9446f306bc31b 100644 --- a/packages/workspace/src/generators/move/lib/move-project-files.ts +++ b/packages/workspace/src/generators/move/lib/move-project-files.ts @@ -22,6 +22,7 @@ export function moveProjectFiles( '.babelrc', '.eslintrc.json', 'eslint.config.js', + 'eslint.config.cjs', /^jest\.config\.((app|lib)\.)?[jt]s$/, 'vite.config.ts', /^webpack.*\.js$/, diff --git a/packages/workspace/src/generators/move/lib/update-eslint-config.spec.ts b/packages/workspace/src/generators/move/lib/update-eslint-config.spec.ts index 0a0ff080b8bb7..beafb32fd6b11 100644 --- a/packages/workspace/src/generators/move/lib/update-eslint-config.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-eslint-config.spec.ts @@ -243,7 +243,7 @@ describe('updateEslint (flat config)', () => { tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); tree.delete('.eslintrc.json'); - tree.write('eslint.config.js', `module.exports = [];`); + tree.write('eslint.config.cjs', `module.exports = [];`); }); it('should handle config not existing', async () => { @@ -267,16 +267,16 @@ describe('updateEslint (flat config)', () => { convertToFlat(tree, 'my-lib'); // This step is usually handled elsewhere tree.rename( - 'my-lib/eslint.config.js', - 'shared/my-destination/eslint.config.js' + 'my-lib/eslint.config.cjs', + 'shared/my-destination/eslint.config.cjs' ); const projectConfig = readProjectConfiguration(tree, 'my-lib'); updateEslintConfig(tree, schema, projectConfig); expect( - tree.read('shared/my-destination/eslint.config.js', 'utf-8') - ).toEqual(expect.stringContaining(`require('../../eslint.config.js')`)); + tree.read('shared/my-destination/eslint.config.cjs', 'utf-8') + ).toEqual(expect.stringContaining(`require('../../eslint.config.cjs')`)); }); it('should update config extends path when project is moved from subdirectory', async () => { @@ -287,7 +287,7 @@ describe('updateEslint (flat config)', () => { }); convertToFlat(tree, 'api/test'); // This step is usually handled elsewhere - tree.rename('api/test/eslint.config.js', 'test/eslint.config.js'); + tree.rename('api/test/eslint.config.cjs', 'test/eslint.config.cjs'); const projectConfig = readProjectConfiguration(tree, 'api-test'); @@ -302,8 +302,8 @@ describe('updateEslint (flat config)', () => { updateEslintConfig(tree, newSchema, projectConfig); - expect(tree.read('test/eslint.config.js', 'utf-8')).toEqual( - expect.stringContaining(`require('../eslint.config.js')`) + expect(tree.read('test/eslint.config.cjs', 'utf-8')).toEqual( + expect.stringContaining(`require('../eslint.config.cjs')`) ); }); @@ -316,15 +316,15 @@ describe('updateEslint (flat config)', () => { convertToFlat(tree, 'my-lib', { hasParser: true }); // This step is usually handled elsewhere tree.rename( - 'my-lib/eslint.config.js', - 'shared/my-destination/eslint.config.js' + 'my-lib/eslint.config.cjs', + 'shared/my-destination/eslint.config.cjs' ); const projectConfig = readProjectConfiguration(tree, 'my-lib'); updateEslintConfig(tree, schema, projectConfig); expect( - tree.read('shared/my-destination/eslint.config.js', 'utf-8') + tree.read('shared/my-destination/eslint.config.cjs', 'utf-8') ).toEqual( expect.stringContaining( `project: ["shared/my-destination/tsconfig.*?.json"]` @@ -346,15 +346,15 @@ describe('updateEslint (flat config)', () => { }); // This step is usually handled elsewhere tree.rename( - 'my-lib/eslint.config.js', - 'shared/my-destination/eslint.config.js' + 'my-lib/eslint.config.cjs', + 'shared/my-destination/eslint.config.cjs' ); const projectConfig = readProjectConfiguration(tree, 'my-lib'); updateEslintConfig(tree, schema, projectConfig); expect( - tree.read('shared/my-destination/eslint.config.js', 'utf-8') + tree.read('shared/my-destination/eslint.config.cjs', 'utf-8') ).toEqual( expect.stringContaining( `project: ["shared/my-destination/tsconfig.*?.json", "shared/my-destination/${storybookProject}"]` @@ -372,15 +372,15 @@ describe('updateEslint (flat config)', () => { convertToFlat(tree, 'my-lib', { hasParser: true, isString: true }); // This step is usually handled elsewhere tree.rename( - 'my-lib/eslint.config.js', - 'shared/my-destination/eslint.config.js' + 'my-lib/eslint.config.cjs', + 'shared/my-destination/eslint.config.cjs' ); const projectConfig = readProjectConfiguration(tree, 'my-lib'); updateEslintConfig(tree, schema, projectConfig); expect( - tree.read('shared/my-destination/eslint.config.js', 'utf-8') + tree.read('shared/my-destination/eslint.config.cjs', 'utf-8') ).toEqual( expect.stringContaining( `project: "shared/my-destination/tsconfig.*?.json"` @@ -417,9 +417,9 @@ function convertToFlat( } tree.write( - joinPathFragments(path, 'eslint.config.js'), + joinPathFragments(path, 'eslint.config.cjs'), `const { FlatCompat } = require("@eslint/eslintrc"); - const baseConfig = require("${offset}eslint.config.js"); + const baseConfig = require("${offset}eslint.config.cjs"); const js = require("@eslint/js"); const compat = new FlatCompat({ baseDirectory: __dirname, diff --git a/packages/workspace/src/generators/move/lib/update-eslint-config.ts b/packages/workspace/src/generators/move/lib/update-eslint-config.ts index 4dd03c8c0e179..7575c4ded7d79 100644 --- a/packages/workspace/src/generators/move/lib/update-eslint-config.ts +++ b/packages/workspace/src/generators/move/lib/update-eslint-config.ts @@ -15,8 +15,10 @@ export function updateEslintConfig( if ( !tree.exists('.eslintrc.json') && !tree.exists('eslint.config.js') && + !tree.exists('eslint.config.cjs') && !tree.exists('.eslintrc.base.json') && - !tree.exists('eslint.base.config.js') + !tree.exists('eslint.base.config.js') && + !tree.exists('eslint.base.config.cjs') ) { return; } diff --git a/packages/workspace/src/generators/move/lib/update-project-root-files.ts b/packages/workspace/src/generators/move/lib/update-project-root-files.ts index c516488cc0aa7..e9f09542ea4dc 100644 --- a/packages/workspace/src/generators/move/lib/update-project-root-files.ts +++ b/packages/workspace/src/generators/move/lib/update-project-root-files.ts @@ -51,7 +51,7 @@ export function updateFilesForRootProjects( if (!allowedExt.includes(ext)) { continue; } - if (file === '.eslintrc.json' || file === 'eslint.config.js') { + if (file === '.eslintrc.json' || file === 'eslint.config.cjs') { continue; } @@ -104,7 +104,11 @@ export function updateFilesForNonRootProjects( if (!allowedExt.includes(ext)) { continue; } - if (file === '.eslintrc.json' || file === 'eslint.config.js') { + if ( + file === '.eslintrc.json' || + file === 'eslint.config.cjs' || + file === 'eslint.config.js' + ) { continue; }