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 flat config generator works for pcv3 plugin #21485

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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,63 @@ 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;
}
);

await convertToFlatConfigGenerator(tree, options);
meeroslav marked this conversation as resolved.
Show resolved Hide resolved
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;
}
);

meeroslav marked this conversation as resolved.
Show resolved Hide resolved
await convertToFlatConfigGenerator(tree, options);
expect(tree.exists('eslint.config.js')).toBeTruthy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy();
});
});
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 ')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we want the space here, in v9 (in prerelease now) they have switched it so that eslint works in the current directory with no arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that having just eslint might have some unexpected things caught. But I guess we can solve that once it happens.

);
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 ')) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above

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