Skip to content

Commit

Permalink
feat(linter): support eslint.config.cjs with flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
benpsnyder committed Jun 22, 2024
1 parent 963f753 commit 65edf38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 8 additions & 8 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@nx/devkit';
import type { Tree } from '@nx/devkit';
import type { Linter } from 'eslint';
import { useFlatConfig } from '../../utils/flat-config';
import { flatConfigEslintFilename, useFlatConfig } from '../../utils/flat-config';
import {
addBlockToFlatConfigExport,
addCompatToFlatConfig,
Expand Down Expand Up @@ -174,7 +174,7 @@ export function addOverrideToLintConfig(
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(
root,
isBase ? baseEsLintFlatConfigFile : 'eslint.config.js'
isBase ? baseEsLintFlatConfigFile : flatConfigEslintFilename(tree)
);
const flatOverride = generateFlatOverride(override);
let content = tree.read(fileName, 'utf8');
Expand Down Expand Up @@ -220,7 +220,7 @@ export function updateOverrideInLintConfig(
) => Linter.ConfigOverride<Linter.RulesRecord>
) {
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
let content = tree.read(fileName, 'utf8');
content = replaceOverride(content, root, lookup, update);
tree.write(fileName, content);
Expand Down Expand Up @@ -262,7 +262,7 @@ export function lintConfigHasOverride(
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(
root,
isBase ? baseEsLintFlatConfigFile : 'eslint.config.js'
isBase ? baseEsLintFlatConfigFile : flatConfigEslintFilename(tree)
);
const content = tree.read(fileName, 'utf8');
return hasOverride(content, lookup);
Expand All @@ -282,7 +282,7 @@ export function replaceOverridesInLintConfig(
overrides: Linter.ConfigOverride<Linter.RulesRecord>[]
) {
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
let content = tree.read(fileName, 'utf8');
// we will be using compat here so we need to make sure it's added
if (overrides.some(overrideNeedsCompat)) {
Expand Down Expand Up @@ -311,7 +311,7 @@ export function addExtendsToLintConfig(
) {
const plugins = Array.isArray(plugin) ? plugin : [plugin];
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
const pluginExtends = generatePluginExtendsElement(plugins);
let content = tree.read(fileName, 'utf8');
content = addCompatToFlatConfig(content);
Expand Down Expand Up @@ -341,7 +341,7 @@ export function addPluginsToLintConfig(
) {
const plugins = Array.isArray(plugin) ? plugin : [plugin];
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
let content = tree.read(fileName, 'utf8');
const mappedPlugins: { name: string; varName: string; imp: string }[] = [];
plugins.forEach((name) => {
Expand Down Expand Up @@ -369,7 +369,7 @@ export function addIgnoresToLintConfig(
ignorePatterns: string[]
) {
if (useFlatConfig(tree)) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const fileName = joinPathFragments(root, flatConfigEslintFilename(tree));
const block = generateAst<ts.ObjectLiteralExpression>({
ignores: ignorePatterns.map((path) => mapFilePath(path)),
});
Expand Down
16 changes: 15 additions & 1 deletion packages/eslint/src/utils/flat-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { Tree } from '@nx/devkit';

export function flatConfigEslintFilename(tree: Tree): string {
const configFiles = ['eslint.config.js', 'eslint.config.cjs'];
for (const file of configFiles) {
if (tree.exists(file)) {
return file;
}
}
throw new Error('Could not find flat config file');
}

export function useFlatConfig(tree: Tree): boolean {
return tree.exists('eslint.config.js');
try {
return !!flatConfigEslintFilename(tree);
} catch {
return false;
}
}

0 comments on commit 65edf38

Please sign in to comment.