Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): ensure config manipulations are run only if config is supported #19035

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 5 additions & 4 deletions packages/linter/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function findEslintFile(tree: Tree, projectRoot = ''): string | null {
export function isEslintConfigSupported(tree: Tree, projectRoot = ''): boolean {
const eslintFile = findEslintFile(tree, projectRoot);
if (!eslintFile) {
return;
return false;
}
return eslintFile.endsWith('.json') || eslintFile.endsWith('.config.js');
}
Expand Down 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 @@ -8,6 +8,7 @@ describe('update-16-8-0-add-ignored-files migration', () => {

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
tree.write('.eslintrc.json', '{}');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary for isEslintConfigSupported to work. Normally, the root eslintrc would be there

});

it('should run successfully when eslint config is not present', async () => {
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,12 @@ 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