Skip to content

Commit

Permalink
fix(linter): standalone e2e should not extend root config (#20379)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 23, 2023
1 parent c8e753a commit 0f6c116
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
28 changes: 28 additions & 0 deletions packages/eslint/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { Linter } from '../utils/linter';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { lintProjectGenerator } from './lint-project';
import { eslintVersion } from '../../utils/versions';

describe('@nx/eslint:lint-project', () => {
let tree: Tree;
Expand Down Expand Up @@ -221,4 +222,31 @@ describe('@nx/eslint:lint-project', () => {
analyzeSourceFiles: true,
});
});

it('should extend root config', async () => {
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/test-lib/**/*.ts'],
project: 'test-lib',
setParserOptionsProject: false,
});

const eslintConfig = readJson(tree, 'libs/test-lib/.eslintrc.json');
expect(eslintConfig.extends).toBeDefined();
});

it('should not extend root config if rootProject is set', async () => {
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/test-lib/**/*.ts'],
project: 'test-lib',
setParserOptionsProject: false,
rootProject: true,
});

const eslintConfig = readJson(tree, 'libs/test-lib/.eslintrc.json');
expect(eslintConfig.extends).toBeUndefined();
});
});
17 changes: 10 additions & 7 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export async function lintProjectGenerator(
createEsLintConfiguration(
tree,
projectConfig,
options.setParserOptionsProject
options.setParserOptionsProject,
options.rootProject
);
}

Expand Down Expand Up @@ -142,11 +143,13 @@ export async function lintProjectGenerator(
function createEsLintConfiguration(
tree: Tree,
projectConfig: ProjectConfiguration,
setParserOptionsProject: boolean
setParserOptionsProject: boolean,
rootProject: boolean
) {
const eslintConfig = findEslintFile(tree);
const pathToRootConfig = eslintConfig
? `${offsetFromRoot(projectConfig.root)}${eslintConfig}`
// we are only extending root for non-standalone projects or their complementary e2e apps
const extendedRootConfig = rootProject ? undefined : findEslintFile(tree);
const pathToRootConfig = extendedRootConfig
? `${offsetFromRoot(projectConfig.root)}${extendedRootConfig}`
: undefined;
const addDependencyChecks = isBuildableLibraryProject(projectConfig);

Expand Down Expand Up @@ -201,7 +204,7 @@ function createEsLintConfiguration(
const isCompatNeeded = addDependencyChecks;
const nodes = [];
const importMap = new Map();
if (eslintConfig) {
if (extendedRootConfig) {
importMap.set(pathToRootConfig, 'baseConfig');
nodes.push(generateSpreadElement('baseConfig'));
}
Expand All @@ -213,7 +216,7 @@ function createEsLintConfiguration(
tree.write(join(projectConfig.root, 'eslint.config.js'), content);
} else {
writeJson(tree, join(projectConfig.root, `.eslintrc.json`), {
extends: eslintConfig ? [pathToRootConfig] : undefined,
extends: extendedRootConfig ? [pathToRootConfig] : undefined,
// Include project files to be linted since the global one excludes all files.
ignorePatterns: ['!**/*'],
overrides,
Expand Down

0 comments on commit 0f6c116

Please sign in to comment.