Skip to content

Commit

Permalink
fix(linter): ensure config manipulations are run only if config is su…
Browse files Browse the repository at this point in the history
…pported
  • Loading branch information
meeroslav committed Sep 6, 2023
1 parent ac85a16 commit 2efcea3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
21 changes: 12 additions & 9 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,21 @@ export async function addLint(
// nx-ignore-next-line
} = 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.
// But we need to set the package.json checks.
if (options.rootProject) {
if (isEslintConfigSupported(tree)) {
addOverrideToLintConfig(tree, '', {
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
});
}
addOverrideToLintConfig(tree, '', {
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
});
}

// If project lints package.json with @nx/dependency-checks, then add ignore files for
Expand Down
7 changes: 4 additions & 3 deletions packages/linter/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ export function lintConfigHasOverride(
lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean,
checkBaseConfig = false
): boolean {
if (!isEslintConfigSupported(tree, root)) {
return false;
}
const isBase =
checkBaseConfig && findEslintFile(tree, root).includes('.base');
if (useFlatConfig(tree)) {
Expand All @@ -248,9 +251,7 @@ export function lintConfigHasOverride(
isBase ? baseEsLintConfigFile : '.eslintrc.json'
);

return tree.exists(fileName)
? readJson(tree, fileName).overrides?.some(lookup) || false
: false;
return readJson(tree, fileName).overrides?.some(lookup) || false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getProjects, Tree } from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import {
findEslintFile,
isEslintConfigSupported,
lintConfigHasOverride,
updateOverrideInLintConfig,
} from '../../generators/utils/eslint-file';
Expand All @@ -16,7 +17,8 @@ export default function update(tree: Tree) {
const addIgnorePattern =
(ignorePattern: string) => (_options: unknown, projectName: string) => {
const project = projects.get(projectName);
if (!findEslintFile(tree, project.root)) return;
if (!findEslintFile(tree, project.root) || !isEslintConfigSupported(tree))
return;
if (
lintConfigHasOverride(
tree,
Expand Down

0 comments on commit 2efcea3

Please sign in to comment.