From 65edf387692567ed9cb0d9dc28063e05871d11be Mon Sep 17 00:00:00 2001 From: Ben Snyder Date: Sat, 22 Jun 2024 14:35:21 -0400 Subject: [PATCH] feat(linter): support eslint.config.cjs with flat config --- .../eslint/src/generators/utils/eslint-file.ts | 16 ++++++++-------- packages/eslint/src/utils/flat-config.ts | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/eslint/src/generators/utils/eslint-file.ts b/packages/eslint/src/generators/utils/eslint-file.ts index 90e23965f48373..01f17ec9f25902 100644 --- a/packages/eslint/src/generators/utils/eslint-file.ts +++ b/packages/eslint/src/generators/utils/eslint-file.ts @@ -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, @@ -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'); @@ -220,7 +220,7 @@ export function updateOverrideInLintConfig( ) => Linter.ConfigOverride ) { 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); @@ -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); @@ -282,7 +282,7 @@ export function replaceOverridesInLintConfig( overrides: Linter.ConfigOverride[] ) { 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)) { @@ -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); @@ -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) => { @@ -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({ ignores: ignorePatterns.map((path) => mapFilePath(path)), }); diff --git a/packages/eslint/src/utils/flat-config.ts b/packages/eslint/src/utils/flat-config.ts index 2943a4948e39a2..ea6594cadcd1c4 100644 --- a/packages/eslint/src/utils/flat-config.ts +++ b/packages/eslint/src/utils/flat-config.ts @@ -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; + } }