fix(linter): support adding plugin at a later stage (#20557)
This commit is contained in:
parent
7e5c4598a3
commit
804959a454
@ -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",
|
||||
},
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 &&
|
||||
|
||||
10
packages/eslint/src/generators/utils/plugin.ts
Normal file
10
packages/eslint/src/generators/utils/plugin.ts
Normal 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'
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user