-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(linter): check for flat config correctly in @nx/eslint:lint execu…
…tor (#26350) <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #22575
- Loading branch information
1 parent
f8239de
commit f4b379f
Showing
3 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,93 @@ | ||
import { joinPathFragments } from '@nx/devkit'; | ||
import { existsSync } from 'fs'; | ||
import { existsSync, statSync } from 'fs'; | ||
import { basename, dirname, join, resolve } from 'path'; | ||
|
||
export const ESLINT_CONFIG_FILENAMES = [ | ||
// TODO(leo): add support for eslint.config.mjs and eslint.config.cjs | ||
export const ESLINT_FLAT_CONFIG_FILENAMES = ['eslint.config.js']; | ||
|
||
export const ESLINT_OLD_CONFIG_FILENAMES = [ | ||
'.eslintrc', | ||
'.eslintrc.js', | ||
'.eslintrc.cjs', | ||
'.eslintrc.yaml', | ||
'.eslintrc.yml', | ||
'.eslintrc.json', | ||
'eslint.config.js', | ||
]; | ||
|
||
export const ESLINT_CONFIG_FILENAMES = [ | ||
...ESLINT_OLD_CONFIG_FILENAMES, | ||
...ESLINT_FLAT_CONFIG_FILENAMES, | ||
]; | ||
|
||
export const baseEsLintConfigFile = '.eslintrc.base.json'; | ||
export const baseEsLintFlatConfigFile = 'eslint.base.config.js'; | ||
|
||
export function isFlatConfig(configFilePath: string): boolean { | ||
return configFilePath.endsWith('.config.js'); | ||
const configFileName = basename(configFilePath); | ||
|
||
return ESLINT_FLAT_CONFIG_FILENAMES.includes(configFileName); | ||
} | ||
|
||
// https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file-resolution | ||
export function findFlatConfigFile( | ||
directory: string, | ||
workspaceRoot: string | ||
): string | null { | ||
let currentDir = resolve(workspaceRoot, directory); | ||
|
||
if (currentDir === workspaceRoot) { | ||
return getConfigFileInDirectory(currentDir, ESLINT_FLAT_CONFIG_FILENAMES); | ||
} | ||
|
||
while (currentDir !== workspaceRoot) { | ||
const configFilePath = getConfigFileInDirectory( | ||
currentDir, | ||
ESLINT_FLAT_CONFIG_FILENAMES | ||
); | ||
if (configFilePath) { | ||
return configFilePath; | ||
} | ||
currentDir = dirname(currentDir); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function findOldConfigFile( | ||
filePathOrDirectory: string, | ||
workspaceRoot: string | ||
): string | null { | ||
let currentDir = statSync(filePathOrDirectory).isDirectory() | ||
? filePathOrDirectory | ||
: dirname(filePathOrDirectory); | ||
|
||
if (currentDir === workspaceRoot) { | ||
return getConfigFileInDirectory(currentDir, ESLINT_OLD_CONFIG_FILENAMES); | ||
} | ||
|
||
while (currentDir !== workspaceRoot) { | ||
const configFilePath = getConfigFileInDirectory( | ||
currentDir, | ||
ESLINT_OLD_CONFIG_FILENAMES | ||
); | ||
if (configFilePath) { | ||
return configFilePath; | ||
} | ||
currentDir = dirname(currentDir); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function getConfigFileInDirectory( | ||
directory: string, | ||
candidateFileNames: string[] | ||
): string | null { | ||
for (const filename of candidateFileNames) { | ||
const filePath = join(directory, filename); | ||
if (existsSync(filePath)) { | ||
return filePath; | ||
} | ||
} | ||
|
||
return null; | ||
} |