diff --git a/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts b/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts index 90c2b0e1ba0c3..7be0447a0c1b4 100644 --- a/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts +++ b/packages/eslint/src/generators/convert-to-flat-config/generator.spec.ts @@ -1,6 +1,7 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { NxJsonConfiguration, + ProjectConfiguration, Tree, addProjectConfiguration, readJson, @@ -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(); + }); }); diff --git a/packages/eslint/src/generators/convert-to-flat-config/generator.ts b/packages/eslint/src/generators/convert-to-flat-config/generator.ts index 06e752a8bff59..6262a34185ac3 100644 --- a/packages/eslint/src/generators/convert-to-flat-config/generator.ts +++ b/packages/eslint/src/generators/convert-to-flat-config/generator.ts @@ -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) { @@ -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,