Skip to content

Commit

Permalink
feat(core): improve base ESLint config filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Jan 17, 2025
1 parent 8136107 commit db46958
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
"description": "Skip formatting files.",
"default": false,
"x-priority": "internal"
},
"eslintConfigFormat": {
"type": "string",
"description": "The format of the ESLint configuration file",
"enum": ["cjs", "mjs"],
"default": "mjs"
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function convertToFlatConfigGenerator(
'Only json and yaml eslint config files are supported for conversion'
);
}

options.eslintConfigFormat ??= 'mjs';

const eslintIgnoreFiles = new Set<string>(['.eslintignore']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface ConvertToFlatConfigGeneratorSchema {
skipFormat?: boolean;
// Internal option
eslintConfigFormat?: 'mjs' | 'cjs';
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
"description": "Skip formatting files.",
"default": false,
"x-priority": "internal"
},
"eslintConfigFormat": {
"type": "string",
"description": "The format of the ESLint configuration file",
"enum": ["cjs", "mjs"],
"default": "mjs"
}
},
"additionalProperties": false,
Expand Down
289 changes: 202 additions & 87 deletions packages/eslint/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,119 +42,234 @@ describe('@nx/eslint:lint-project', () => {
});
});

it('should generate a flat eslint config format based on base config (JS with CJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

// CJS config
tree.write('eslint.base.config.js', 'module.exports = {};');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
describe('Eslint base config named eslint.base.config', () => {
it('should generate a flat eslint config format based on base config (JS with CJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

// CJS config
tree.write('eslint.base.config.js', 'module.exports = {};');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.base.config.js");
module.exports = [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.base.config.js");
it('should generate a flat eslint config format based on base config (JS with MJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

module.exports = [
...baseConfig
];
"
`);
// MJS config
tree.write('eslint.base.config.js', 'export default {};');

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

it('should generate a flat eslint config format based on base config (JS with MJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';
expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.base.config.js";
// MJS config
tree.write('eslint.base.config.js', 'export default {};');
export default [
...baseConfig
];
"
`);

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.base.config.js";
it('should generate a flat eslint config format based on base config (mjs)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

// MJS config
tree.write('eslint.base.config.mjs', 'export default {};');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
eslintConfigFormat: 'mjs',
});

expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.base.config.mjs";
export default [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

export default [
...baseConfig
];
"
`);
it('should generate a flat eslint config format based on base config CJS', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});
// CJS config
tree.write('eslint.base.config.cjs', 'module.exports = {};');

it('should generate a flat eslint config format based on base config (mjs)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

// MJS config
tree.write('eslint.base.config.mjs', 'export default {};');
expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.base.config.cjs");
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
eslintConfigFormat: 'mjs',
module.exports = [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});
});

expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.base.config.mjs";
describe('Eslint base config named eslint.config', () => {
it('should generate a flat eslint config format based on base config (JS with CJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

// CJS config
tree.write('eslint.config.js', 'module.exports = {};');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.config.js");
module.exports = [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

export default [
...baseConfig
];
"
`);
it('should generate a flat eslint config format based on base config (JS with MJS export)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});
// MJS config
tree.write('eslint.config.js', 'export default {};');

it('should generate a flat eslint config format based on base config CJS', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

// CJS config
tree.write('eslint.config.cjs', 'module.exports = {};');
expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.config.js";
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
export default [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

console.log(tree.children('.'));
it('should generate a flat eslint config format based on base config (mjs)', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

// MJS config
tree.write('eslint.config.mjs', 'export default {};');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
eslintConfigFormat: 'mjs',
});

expect(tree.read('libs/test-lib/eslint.config.mjs', 'utf-8'))
.toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.config.mjs";
export default [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});

expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.config.cjs");
it('should generate a flat eslint config format based on base config CJS', async () => {
const originalEslintUseFlatConfigVal = process.env.ESLINT_USE_FLAT_CONFIG;
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

module.exports = [
...baseConfig
];
"
`);
// CJS config
tree.write('eslint.config.cjs', 'module.exports = {};');

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
skipFormat: true,
});

expect(tree.read('libs/test-lib/eslint.config.cjs', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.config.cjs");
module.exports = [
...baseConfig
];
"
`);

process.env.ESLINT_USE_FLAT_CONFIG = originalEslintUseFlatConfigVal;
});
});

it('should generate a flat eslint base config ESM', async () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from '../utils/flat-config/ast-utils';
import {
baseEsLintConfigFile,
baseEsLintFlatConfigFile,
BASE_ESLINT_CONFIG_FILENAMES,
} from '../../utils/config-file';
import { hasEslintPlugin } from '../utils/plugin';
import { setupRootEsLint } from './setup-root-eslint';
Expand Down Expand Up @@ -344,8 +344,9 @@ function isBuildableLibraryProject(
function isMigrationToMonorepoNeeded(tree: Tree, graph: ProjectGraph): boolean {
// the base config is already created, migration has been done
if (
tree.exists(baseEsLintConfigFile) ||
tree.exists(baseEsLintFlatConfigFile)
[baseEsLintConfigFile, ...BASE_ESLINT_CONFIG_FILENAMES].some((f) =>
tree.exists(f)
)
) {
return false;
}
Expand Down
Loading

0 comments on commit db46958

Please sign in to comment.