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): standalone e2e should not extend root config #20379

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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