fix(linter): support adding plugin at a later stage (#20557)

This commit is contained in:
Miroslav Jonaš 2023-12-05 16:33:29 +01:00 committed by GitHub
parent 7e5c4598a3
commit 804959a454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 8 deletions

View File

@ -5,11 +5,17 @@ import { lintInitGenerator } from './init';
describe('@nx/eslint:init', () => {
let tree: Tree;
let envV3: string | undefined;
beforeEach(() => {
envV3 = process.env.NX_PCV3;
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});
afterEach(() => {
process.env.NX_PCV3 = envV3;
});
it('should generate the global eslint config', async () => {
await lintInitGenerator(tree, {
linter: Linter.EsLint,
@ -97,4 +103,31 @@ describe('@nx/eslint:init', () => {
]
`);
});
it('should add @nx/eslint/plugin in subsequent step', async () => {
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
json.namedInputs ??= {};
json.namedInputs.production = ['default'];
return json;
});
await lintInitGenerator(tree, {});
expect(
readJson<NxJsonConfiguration>(tree, 'nx.json').plugins
).not.toBeDefined();
process.env.NX_PCV3 = 'true';
await lintInitGenerator(tree, {});
expect(readJson<NxJsonConfiguration>(tree, 'nx.json').plugins)
.toMatchInlineSnapshot(`
[
{
"options": {
"targetName": "lint",
},
"plugin": "@nx/eslint/plugin",
},
]
`);
});
});

View File

@ -18,6 +18,7 @@ import { Linter } from '../utils/linter';
import { findEslintFile } from '../utils/eslint-file';
import { getGlobalEsLintConfiguration } from './global-eslint-config';
import { EslintPluginOptions } from '../../plugins/plugin';
import { hasEslintPlugin } from '../utils/plugin';
export interface LinterInitOptions {
linter?: Linter;
@ -94,7 +95,16 @@ function updateVSCodeExtensions(tree: Tree) {
* Initializes ESLint configuration in a workspace and adds necessary dependencies.
*/
function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
if (findEslintFile(tree)) {
const addPlugins = process.env.NX_PCV3 === 'true';
const hasPlugin = hasEslintPlugin(tree);
const rootEslintFile = findEslintFile(tree);
if (rootEslintFile && addPlugins && !hasPlugin) {
addPlugin(tree);
return () => {};
}
if (rootEslintFile) {
return () => {};
}
@ -111,7 +121,6 @@ function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
updateProductionFileset(tree);
const addPlugins = process.env.NX_PCV3 === 'true';
if (addPlugins) {
addPlugin(tree);
} else {

View File

@ -35,6 +35,7 @@ import {
baseEsLintConfigFile,
baseEsLintFlatConfigFile,
} from '../../utils/config-file';
import { hasEslintPlugin } from '../utils/plugin';
interface LintProjectOptions {
project: string;
@ -73,12 +74,7 @@ export async function lintProjectGenerator(
lintFilePatterns.push(`{projectRoot}/package.json`);
}
const nxJson = readNxJson(tree);
const hasPlugin = nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/eslint/plugin'
: p.plugin === '@nx/eslint/plugin'
);
const hasPlugin = hasEslintPlugin(tree);
if (hasPlugin) {
if (
lintFilePatterns &&

View File

@ -0,0 +1,10 @@
import { Tree, readNxJson } from '@nx/devkit';
export function hasEslintPlugin(tree: Tree): boolean {
const nxJson = readNxJson(tree);
return nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/eslint/plugin'
: p.plugin === '@nx/eslint/plugin'
);
}