diff --git a/packages/eslint/src/plugins/plugin.spec.ts b/packages/eslint/src/plugins/plugin.spec.ts index db7a928423dfb..86efd9459f1d0 100644 --- a/packages/eslint/src/plugins/plugin.spec.ts +++ b/packages/eslint/src/plugins/plugin.spec.ts @@ -120,6 +120,47 @@ describe('@nx/eslint/plugin', () => { `); }); + it('should create a node for just a package.json and root level eslint config if accompanied by a lib directory', async () => { + createFiles({ + '.eslintrc.json': `{}`, + 'package.json': `{}`, + 'lib/index.ts': `console.log('hello world')`, + }); + // NOTE: The command is specifically targeting the src directory in the case of a standalone Nx workspace + expect(await invokeCreateNodesOnMatchingFiles(context, 'lint')) + .toMatchInlineSnapshot(` + { + "projects": { + ".": { + "targets": { + "lint": { + "cache": true, + "command": "eslint ./lib", + "inputs": [ + "default", + "^default", + "{projectRoot}/eslintrc.json", + "{workspaceRoot}/tools/eslint-rules/**/*", + { + "externalDependencies": [ + "eslint", + ], + }, + ], + "options": { + "cwd": ".", + }, + "outputs": [ + "{options.outputFile}", + ], + }, + }, + }, + }, + } + `); + }); + it('should not create a node for just a package.json and root level eslint config if accompanied by a src directory when all files are ignored (.eslintignore)', async () => { createFiles({ '.eslintrc.json': `{}`, diff --git a/packages/eslint/src/plugins/plugin.ts b/packages/eslint/src/plugins/plugin.ts index 9fc77d68214f9..e89ca703d84f2 100644 --- a/packages/eslint/src/plugins/plugin.ts +++ b/packages/eslint/src/plugins/plugin.ts @@ -128,12 +128,18 @@ function getProjectsUsingESLintConfig( // Add a lint target for each child project without an eslint config, with the root level config as an input for (const projectRoot of childProjectRoots) { - // If there's no src folder, it's not a standalone project, do not add the target at all - const isStandaloneWorkspace = + let standaloneSrcPath: string | undefined; + if ( projectRoot === '.' && - existsSync(join(context.workspaceRoot, projectRoot, 'src')) && - existsSync(join(context.workspaceRoot, projectRoot, 'package.json')); - if (projectRoot === '.' && !isStandaloneWorkspace) { + existsSync(join(context.workspaceRoot, projectRoot, 'package.json')) + ) { + if (existsSync(join(context.workspaceRoot, projectRoot, 'src'))) { + standaloneSrcPath = 'src'; + } else if (existsSync(join(context.workspaceRoot, projectRoot, 'lib'))) { + standaloneSrcPath = 'lib'; + } + } + if (projectRoot === '.' && !standaloneSrcPath) { continue; } @@ -150,7 +156,7 @@ function getProjectsUsingESLintConfig( projectRoot, context.workspaceRoot, options, - isStandaloneWorkspace + standaloneSrcPath ), }; } @@ -164,14 +170,16 @@ function buildEslintTargets( projectRoot: string, workspaceRoot: string, options: EslintPluginOptions, - isStandaloneWorkspace = false + standaloneSrcPath?: string ) { const isRootProject = projectRoot === '.'; const targets: Record = {}; const targetConfig: TargetConfiguration = { - command: `eslint ${isRootProject && isStandaloneWorkspace ? './src' : '.'}`, + command: `eslint ${ + isRootProject && standaloneSrcPath ? `./${standaloneSrcPath}` : '.' + }`, cache: true, options: { cwd: projectRoot,