Skip to content

Commit

Permalink
fix(linter): ensure flat config generator works for pcv3 plugin (#21485)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Feb 1, 2024
1 parent 8762c38 commit c753838
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import {
NxJsonConfiguration,
ProjectConfiguration,
Tree,
addProjectConfiguration,
readJson,
Expand Down Expand Up @@ -430,4 +431,67 @@ describe('convert-to-flat-config generator', () => {
"
`);
});

it('should convert project if target is defined via plugin as string', async () => {
await lintProjectGenerator(tree, {
skipFormat: false,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
});
updateJson(tree, 'nx.json', (json: NxJsonConfiguration) => {
delete json.targetDefaults;
json.plugins = ['@nx/eslint/plugin'];
return json;
});
updateJson(
tree,
'libs/test-lib/project.json',
(json: ProjectConfiguration) => {
delete json.targets.lint;
return json;
}
);

expect(tree.exists('eslint.config.js')).toBeFalsy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeFalsy();
await convertToFlatConfigGenerator(tree, options);
expect(tree.exists('eslint.config.js')).toBeTruthy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy();
});

it('should convert project if target is defined via plugin as object', async () => {
await lintProjectGenerator(tree, {
skipFormat: false,
linter: Linter.EsLint,
project: 'test-lib',
setParserOptionsProject: false,
});
updateJson(tree, 'nx.json', (json: NxJsonConfiguration) => {
delete json.targetDefaults;
json.plugins = [
{
plugin: '@nx/eslint/plugin',
options: {
targetName: 'lint',
},
},
];
return json;
});
updateJson(
tree,
'libs/test-lib/project.json',
(json: ProjectConfiguration) => {
delete json.targets.lint;
return json;
}
);

expect(tree.exists('eslint.config.js')).toBeFalsy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeFalsy();
await convertToFlatConfigGenerator(tree, options);
expect(tree.exists('eslint.config.js')).toBeTruthy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy();
});
});
17 changes: 13 additions & 4 deletions packages/eslint/src/generators/convert-to-flat-config/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ function convertProjectToFlatConfig(
if (eslintFile && !eslintFile.endsWith('.js')) {
if (projectConfig.targets) {
const eslintTargets = Object.keys(projectConfig.targets || {}).filter(
(t) => projectConfig.targets[t].executor === '@nx/eslint:lint'
(t) =>
projectConfig.targets[t].executor === '@nx/eslint:lint' ||
projectConfig.targets[t].command?.includes('eslint')
);
let ignorePath: string | undefined;
for (const target of eslintTargets) {
Expand All @@ -102,13 +104,20 @@ function convertProjectToFlatConfig(
}
updateProjectConfiguration(tree, project, projectConfig);
}
const nxHasLintTargets = Object.keys(nxJson.targetDefaults || {}).some(
const nxHasEsLintTargets = Object.keys(nxJson.targetDefaults || {}).some(
(t) =>
(t === '@nx/eslint:lint' ||
nxJson.targetDefaults[t].executor === '@nx/eslint:lint') &&
nxJson.targetDefaults[t].executor === '@nx/eslint:lint' ||
nxJson.targetDefaults[t].command?.includes('eslint')) &&
projectConfig.targets?.[t]
);
if (nxHasLintTargets || eslintTargets.length > 0) {
const nxHasEsLintPlugin = (nxJson.plugins || []).some((p) =>
typeof p === 'string'
? p === '@nx/eslint/plugin'
: p.plugin === '@nx/eslint/plugin'
);

if (nxHasEsLintTargets || nxHasEsLintPlugin || eslintTargets.length > 0) {
convertConfigToFlatConfig(
tree,
projectConfig.root,
Expand Down

0 comments on commit c753838

Please sign in to comment.