fix(linter): ensure config manipulations are run only if config is supported (#19035)
This commit is contained in:
parent
f994f54c52
commit
aa223621f7
@ -267,10 +267,14 @@ export async function addLint(
|
|||||||
// nx-ignore-next-line
|
// nx-ignore-next-line
|
||||||
} = require('@nx/linter/src/generators/utils/eslint-file');
|
} = require('@nx/linter/src/generators/utils/eslint-file');
|
||||||
|
|
||||||
|
// if config is not supported, we don't need to do anything
|
||||||
|
if (!isEslintConfigSupported(tree)) {
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
// Also update the root ESLint config. The lintProjectGenerator will not generate it for root projects.
|
// Also update the root ESLint config. The lintProjectGenerator will not generate it for root projects.
|
||||||
// But we need to set the package.json checks.
|
// But we need to set the package.json checks.
|
||||||
if (options.rootProject) {
|
if (options.rootProject) {
|
||||||
if (isEslintConfigSupported(tree)) {
|
|
||||||
addOverrideToLintConfig(tree, '', {
|
addOverrideToLintConfig(tree, '', {
|
||||||
files: ['*.json'],
|
files: ['*.json'],
|
||||||
parser: 'jsonc-eslint-parser',
|
parser: 'jsonc-eslint-parser',
|
||||||
@ -279,7 +283,6 @@ export async function addLint(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// If project lints package.json with @nx/dependency-checks, then add ignore files for
|
// If project lints package.json with @nx/dependency-checks, then add ignore files for
|
||||||
// build configuration files such as vite.config.ts. These config files need to be
|
// build configuration files such as vite.config.ts. These config files need to be
|
||||||
|
|||||||
@ -55,7 +55,7 @@ export function findEslintFile(tree: Tree, projectRoot = ''): string | null {
|
|||||||
export function isEslintConfigSupported(tree: Tree, projectRoot = ''): boolean {
|
export function isEslintConfigSupported(tree: Tree, projectRoot = ''): boolean {
|
||||||
const eslintFile = findEslintFile(tree, projectRoot);
|
const eslintFile = findEslintFile(tree, projectRoot);
|
||||||
if (!eslintFile) {
|
if (!eslintFile) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
return eslintFile.endsWith('.json') || eslintFile.endsWith('.config.js');
|
return eslintFile.endsWith('.json') || eslintFile.endsWith('.config.js');
|
||||||
}
|
}
|
||||||
@ -233,6 +233,9 @@ export function lintConfigHasOverride(
|
|||||||
lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean,
|
lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean,
|
||||||
checkBaseConfig = false
|
checkBaseConfig = false
|
||||||
): boolean {
|
): boolean {
|
||||||
|
if (!isEslintConfigSupported(tree, root)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const isBase =
|
const isBase =
|
||||||
checkBaseConfig && findEslintFile(tree, root).includes('.base');
|
checkBaseConfig && findEslintFile(tree, root).includes('.base');
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
@ -248,9 +251,7 @@ export function lintConfigHasOverride(
|
|||||||
isBase ? baseEsLintConfigFile : '.eslintrc.json'
|
isBase ? baseEsLintConfigFile : '.eslintrc.json'
|
||||||
);
|
);
|
||||||
|
|
||||||
return tree.exists(fileName)
|
return readJson(tree, fileName).overrides?.some(lookup) || false;
|
||||||
? readJson(tree, fileName).overrides?.some(lookup) || false
|
|
||||||
: false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ describe('update-16-8-0-add-ignored-files migration', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
tree = createTreeWithEmptyWorkspace();
|
tree = createTreeWithEmptyWorkspace();
|
||||||
|
tree.write('.eslintrc.json', '{}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should run successfully when eslint config is not present', async () => {
|
it('should run successfully when eslint config is not present', async () => {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { getProjects, Tree } from '@nx/devkit';
|
|||||||
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
|
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
|
||||||
import {
|
import {
|
||||||
findEslintFile,
|
findEslintFile,
|
||||||
|
isEslintConfigSupported,
|
||||||
lintConfigHasOverride,
|
lintConfigHasOverride,
|
||||||
updateOverrideInLintConfig,
|
updateOverrideInLintConfig,
|
||||||
} from '../../generators/utils/eslint-file';
|
} from '../../generators/utils/eslint-file';
|
||||||
@ -16,7 +17,12 @@ export default function update(tree: Tree) {
|
|||||||
const addIgnorePattern =
|
const addIgnorePattern =
|
||||||
(ignorePattern: string) => (_options: unknown, projectName: string) => {
|
(ignorePattern: string) => (_options: unknown, projectName: string) => {
|
||||||
const project = projects.get(projectName);
|
const project = projects.get(projectName);
|
||||||
if (!findEslintFile(tree, project.root)) return;
|
if (
|
||||||
|
!findEslintFile(tree, project.root) ||
|
||||||
|
!isEslintConfigSupported(tree)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
lintConfigHasOverride(
|
lintConfigHasOverride(
|
||||||
tree,
|
tree,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user