Skip to content

Commit

Permalink
feat(eslint): rename eslint.config.js to eslint.config.cjs to they ar…
Browse files Browse the repository at this point in the history
…e resolved as CommonJS
  • Loading branch information
jaysoo committed Dec 12, 2024
1 parent a8de7df commit 144e881
Show file tree
Hide file tree
Showing 48 changed files with 254 additions and 301 deletions.
2 changes: 1 addition & 1 deletion docs/shared/packages/eslint/dependency-checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions docs/shared/recipes/tips-n-tricks/migrating-to-flat-eslint.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
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.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`);
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.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`);
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.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',
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.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`
)
);

Expand Down Expand Up @@ -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',
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.js`,
`${jsLib}/eslint.config.js`
`${reactLib}/eslint.config.cjs`,
`${jsLib}/eslint.config.cjs`
);
checkFilesDoNotExist(
`${reactLib}/.eslintrc.json`,
Expand Down
10 changes: 0 additions & 10 deletions e2e/js/src/js-packaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
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.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",
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.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",
Expand Down
6 changes: 3 additions & 3 deletions packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
},
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/generators/ng-add/utilities/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand All @@ -85,7 +85,7 @@ export function createNxJson(
inputs: [
'default',
'{workspaceRoot}/.eslintrc.json',
'{workspaceRoot}/eslint.config.js',
'{workspaceRoot}/eslint.config.cjs',
],
cache: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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 = [
{
Expand All @@ -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'],
Expand All @@ -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 = [
{
Expand All @@ -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'],
Expand All @@ -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 = [
{
Expand All @@ -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'],
Expand All @@ -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 = [
{
Expand All @@ -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'],
Expand All @@ -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 = [
{
Expand Down
Loading

0 comments on commit 144e881

Please sign in to comment.