From 8bf6a97a29dc4d25f03ffd08b1c5299bdb5b8759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Tue, 10 Oct 2023 21:24:11 +0200 Subject: [PATCH] fix(linter): fix linter standalone patterns in Js and Next (#19508) --- packages/js/src/generators/library/library.ts | 9 ++++++++- .../linter/src/generators/lint-project/lint-project.ts | 7 +++++-- .../next/src/generators/application/lib/add-linting.ts | 9 ++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 47560e74b2015..ef3451ede0ba6 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -244,6 +244,9 @@ export async function addLint( options: AddLintOptions ): Promise { const { lintProjectGenerator } = ensurePackage('@nx/linter', nxVersion); + const { mapLintPattern } = + // nx-ignore-next-line + require('@nx/linter/src/generators/lint-project/lint-project'); const projectConfiguration = readProjectConfiguration(tree, options.name); const task = lintProjectGenerator(tree, { project: options.name, @@ -254,7 +257,11 @@ export async function addLint( ], unitTestRunner: options.unitTestRunner, eslintFilePatterns: [ - `${options.projectRoot}/**/*.${options.js ? 'js' : 'ts'}`, + mapLintPattern( + options.projectRoot, + options.js ? 'js' : 'ts', + options.rootProject + ), ], setParserOptionsProject: options.setParserOptionsProject, rootProject: options.rootProject, diff --git a/packages/linter/src/generators/lint-project/lint-project.ts b/packages/linter/src/generators/lint-project/lint-project.ts index c46646159f84e..dd631df918753 100644 --- a/packages/linter/src/generators/lint-project/lint-project.ts +++ b/packages/linter/src/generators/lint-project/lint-project.ts @@ -52,8 +52,11 @@ export function mapLintPattern( extension: string, rootProject?: boolean ) { - const infix = rootProject ? 'src/' : ''; - return `${projectRoot}/${infix}**/*.${extension}`; + if (rootProject && (projectRoot === '.' || projectRoot === '')) { + return `${projectRoot}/src/**/*.${extension}`; + } else { + return `${projectRoot}/**/*.${extension}`; + } } export async function lintProjectGenerator( diff --git a/packages/next/src/generators/application/lib/add-linting.ts b/packages/next/src/generators/application/lib/add-linting.ts index 56f5d37a12d4a..4ee945c296265 100644 --- a/packages/next/src/generators/application/lib/add-linting.ts +++ b/packages/next/src/generators/application/lib/add-linting.ts @@ -15,6 +15,7 @@ import { isEslintConfigSupported, updateOverrideInLintConfig, } from '@nx/linter/src/generators/utils/eslint-file'; +import { mapLintPattern } from '@nx/linter/src/generators/lint-project/lint-project'; export async function addLinting( host: Tree, @@ -27,7 +28,13 @@ export async function addLinting( joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'), ], unitTestRunner: options.unitTestRunner, - eslintFilePatterns: [`${options.appProjectRoot}/**/*.{ts,tsx,js,jsx}`], + eslintFilePatterns: [ + mapLintPattern( + options.appProjectRoot, + '{ts,tsx,js,jsx}', + options.rootProject + ), + ], skipFormat: true, rootProject: options.rootProject, });