feat(linter): support eslint.config.cjs and *.cjs extension with flat config (#26637)
This commit is contained in:
parent
3cbe2abc25
commit
81fe13250a
@ -162,11 +162,11 @@ describe('eslint-utils', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('ESLint Flat Config', () => {
|
describe('ESLint Flat Config', () => {
|
||||||
it('should throw if a non eslint.config.js file is used with ESLint Flat Config', async () => {
|
it('should throw if a non eslint.config.js or eslint.config.cjs file is used with ESLint Flat Config', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
resolveAndInstantiateESLint('./.eslintrc.json', {} as any, true)
|
resolveAndInstantiateESLint('./.eslintrc.json', {} as any, true)
|
||||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||||
`"When using the new Flat Config with ESLint, all configs must be named eslint.config.js and .eslintrc files may not be used. See https://eslint.org/docs/latest/use/configure/configuration-files-new"`
|
`"When using the new Flat Config with ESLint, all configs must be named eslint.config.js or eslint.config.cjs and .eslintrc files may not be used. See https://eslint.org/docs/latest/use/configure/configuration-files"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,8 @@ export async function resolveAndInstantiateESLint(
|
|||||||
) {
|
) {
|
||||||
if (useFlatConfig && eslintConfigPath && !isFlatConfig(eslintConfigPath)) {
|
if (useFlatConfig && eslintConfigPath && !isFlatConfig(eslintConfigPath)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'When using the new Flat Config with ESLint, all configs must be named eslint.config.js and .eslintrc files may not be used. See https://eslint.org/docs/latest/use/configure/configuration-files-new'
|
// todo: add support for eslint.config.mjs,
|
||||||
|
'When using the new Flat Config with ESLint, all configs must be named eslint.config.js or eslint.config.cjs and .eslintrc files may not be used. See https://eslint.org/docs/latest/use/configure/configuration-files'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const ESLint = await resolveESLintClass(useFlatConfig);
|
const ESLint = await resolveESLintClass(useFlatConfig);
|
||||||
|
|||||||
@ -7,7 +7,10 @@ import {
|
|||||||
} from '@nx/devkit';
|
} from '@nx/devkit';
|
||||||
import type { Tree } from '@nx/devkit';
|
import type { Tree } from '@nx/devkit';
|
||||||
import type { Linter } from 'eslint';
|
import type { Linter } from 'eslint';
|
||||||
import { useFlatConfig } from '../../utils/flat-config';
|
import {
|
||||||
|
flatConfigEslintFilename,
|
||||||
|
useFlatConfig,
|
||||||
|
} from '../../utils/flat-config';
|
||||||
import {
|
import {
|
||||||
addBlockToFlatConfigExport,
|
addBlockToFlatConfigExport,
|
||||||
addCompatToFlatConfig,
|
addCompatToFlatConfig,
|
||||||
@ -174,7 +177,7 @@ export function addOverrideToLintConfig(
|
|||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(
|
const fileName = joinPathFragments(
|
||||||
root,
|
root,
|
||||||
isBase ? baseEsLintFlatConfigFile : 'eslint.config.js'
|
isBase ? baseEsLintFlatConfigFile : flatConfigEslintFilename(tree)
|
||||||
);
|
);
|
||||||
const flatOverride = generateFlatOverride(override);
|
const flatOverride = generateFlatOverride(override);
|
||||||
let content = tree.read(fileName, 'utf8');
|
let content = tree.read(fileName, 'utf8');
|
||||||
@ -220,7 +223,7 @@ export function updateOverrideInLintConfig(
|
|||||||
) => Linter.ConfigOverride<Linter.RulesRecord>
|
) => Linter.ConfigOverride<Linter.RulesRecord>
|
||||||
) {
|
) {
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(root, 'eslint.config.js');
|
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
|
||||||
let content = tree.read(fileName, 'utf8');
|
let content = tree.read(fileName, 'utf8');
|
||||||
content = replaceOverride(content, root, lookup, update);
|
content = replaceOverride(content, root, lookup, update);
|
||||||
tree.write(fileName, content);
|
tree.write(fileName, content);
|
||||||
@ -262,7 +265,7 @@ export function lintConfigHasOverride(
|
|||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(
|
const fileName = joinPathFragments(
|
||||||
root,
|
root,
|
||||||
isBase ? baseEsLintFlatConfigFile : 'eslint.config.js'
|
isBase ? baseEsLintFlatConfigFile : flatConfigEslintFilename(tree)
|
||||||
);
|
);
|
||||||
const content = tree.read(fileName, 'utf8');
|
const content = tree.read(fileName, 'utf8');
|
||||||
return hasOverride(content, lookup);
|
return hasOverride(content, lookup);
|
||||||
@ -282,7 +285,7 @@ export function replaceOverridesInLintConfig(
|
|||||||
overrides: Linter.ConfigOverride<Linter.RulesRecord>[]
|
overrides: Linter.ConfigOverride<Linter.RulesRecord>[]
|
||||||
) {
|
) {
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(root, 'eslint.config.js');
|
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
|
||||||
let content = tree.read(fileName, 'utf8');
|
let content = tree.read(fileName, 'utf8');
|
||||||
// we will be using compat here so we need to make sure it's added
|
// we will be using compat here so we need to make sure it's added
|
||||||
if (overrides.some(overrideNeedsCompat)) {
|
if (overrides.some(overrideNeedsCompat)) {
|
||||||
@ -311,7 +314,7 @@ export function addExtendsToLintConfig(
|
|||||||
) {
|
) {
|
||||||
const plugins = Array.isArray(plugin) ? plugin : [plugin];
|
const plugins = Array.isArray(plugin) ? plugin : [plugin];
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(root, 'eslint.config.js');
|
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
|
||||||
const pluginExtends = generatePluginExtendsElement(plugins);
|
const pluginExtends = generatePluginExtendsElement(plugins);
|
||||||
let content = tree.read(fileName, 'utf8');
|
let content = tree.read(fileName, 'utf8');
|
||||||
content = addCompatToFlatConfig(content);
|
content = addCompatToFlatConfig(content);
|
||||||
@ -341,7 +344,7 @@ export function addPluginsToLintConfig(
|
|||||||
) {
|
) {
|
||||||
const plugins = Array.isArray(plugin) ? plugin : [plugin];
|
const plugins = Array.isArray(plugin) ? plugin : [plugin];
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(root, 'eslint.config.js');
|
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
|
||||||
let content = tree.read(fileName, 'utf8');
|
let content = tree.read(fileName, 'utf8');
|
||||||
const mappedPlugins: { name: string; varName: string; imp: string }[] = [];
|
const mappedPlugins: { name: string; varName: string; imp: string }[] = [];
|
||||||
plugins.forEach((name) => {
|
plugins.forEach((name) => {
|
||||||
@ -369,7 +372,7 @@ export function addIgnoresToLintConfig(
|
|||||||
ignorePatterns: string[]
|
ignorePatterns: string[]
|
||||||
) {
|
) {
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const fileName = joinPathFragments(root, 'eslint.config.js');
|
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
|
||||||
const block = generateAst<ts.ObjectLiteralExpression>({
|
const block = generateAst<ts.ObjectLiteralExpression>({
|
||||||
ignores: ignorePatterns.map((path) => mapFilePath(path)),
|
ignores: ignorePatterns.map((path) => mapFilePath(path)),
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { existsSync, statSync } from 'fs';
|
import { existsSync, statSync } from 'fs';
|
||||||
import { basename, dirname, join, resolve } from 'path';
|
import { basename, dirname, join, resolve } from 'path';
|
||||||
|
import { eslintFlatConfigFilenames } from './flat-config';
|
||||||
|
|
||||||
// TODO(leo): add support for eslint.config.mjs and eslint.config.cjs
|
export const ESLINT_FLAT_CONFIG_FILENAMES = eslintFlatConfigFilenames;
|
||||||
export const ESLINT_FLAT_CONFIG_FILENAMES = ['eslint.config.js'];
|
|
||||||
|
|
||||||
export const ESLINT_OLD_CONFIG_FILENAMES = [
|
export const ESLINT_OLD_CONFIG_FILENAMES = [
|
||||||
'.eslintrc',
|
'.eslintrc',
|
||||||
@ -40,6 +40,7 @@ export function findFlatConfigFile(
|
|||||||
ESLINT_FLAT_CONFIG_FILENAMES
|
ESLINT_FLAT_CONFIG_FILENAMES
|
||||||
);
|
);
|
||||||
if (configFilePath) {
|
if (configFilePath) {
|
||||||
|
console.log(`Found eslint flat config file at: ${configFilePath}`);
|
||||||
return configFilePath;
|
return configFilePath;
|
||||||
}
|
}
|
||||||
if (currentDir === workspaceRoot) {
|
if (currentDir === workspaceRoot) {
|
||||||
|
|||||||
@ -1,5 +1,24 @@
|
|||||||
import { Tree } from '@nx/devkit';
|
import { Tree } from '@nx/devkit';
|
||||||
|
|
||||||
export function useFlatConfig(tree: Tree): boolean {
|
// todo: add support for eslint.config.mjs,
|
||||||
return tree.exists('eslint.config.js');
|
export const eslintFlatConfigFilenames = [
|
||||||
|
'eslint.config.js',
|
||||||
|
'eslint.config.cjs',
|
||||||
|
];
|
||||||
|
|
||||||
|
export function flatConfigEslintFilename(tree: Tree): string {
|
||||||
|
for (const file of eslintFlatConfigFilenames) {
|
||||||
|
if (tree.exists(file)) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error('Could not find flat config file');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useFlatConfig(tree: Tree): boolean {
|
||||||
|
try {
|
||||||
|
return !!flatConfigEslintFilename(tree);
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user