fix(linter): add node_modules to linter ignore patterns (#14980)
This commit is contained in:
parent
56289555fc
commit
9b51f6b0aa
@ -28,6 +28,7 @@ module.exports = {
|
||||
|
||||
exports[`StorybookConfiguration generator should generate in the correct folder 1`] = `
|
||||
Array [
|
||||
".eslintignore",
|
||||
".eslintrc.json",
|
||||
".prettierrc",
|
||||
"apps/.gitignore",
|
||||
@ -157,6 +158,7 @@ Array [
|
||||
|
||||
exports[`StorybookConfiguration generator should generate the right files 1`] = `
|
||||
Array [
|
||||
".eslintignore",
|
||||
".eslintrc.json",
|
||||
".prettierrc",
|
||||
"apps/.gitignore",
|
||||
|
||||
@ -53,6 +53,12 @@
|
||||
"version": "15.0.0-beta.0",
|
||||
"description": "Stop hashing eslint config files for build targets and dependent tasks",
|
||||
"factory": "./src/migrations/update-15-0-0/add-eslint-inputs"
|
||||
},
|
||||
"add-eslint-ignore": {
|
||||
"cli": "nx",
|
||||
"version": "15.7.1-beta.0",
|
||||
"description": "Add node_modules to root eslint ignore",
|
||||
"factory": "./src/migrations/update-15-7-1/add-eslint-ignore"
|
||||
}
|
||||
},
|
||||
"packageJsonUpdates": {
|
||||
|
||||
@ -18,6 +18,10 @@ describe('@nrwl/linter:init', () => {
|
||||
});
|
||||
|
||||
expect(tree.read('.eslintrc.json', 'utf-8')).toMatchSnapshot();
|
||||
expect(tree.read('.eslintignore', 'utf-8')).toMatchInlineSnapshot(`
|
||||
"node_modules
|
||||
"
|
||||
`);
|
||||
});
|
||||
|
||||
it('should add the root eslint config to the lint targetDefaults for lint', async () => {
|
||||
@ -26,7 +30,11 @@ describe('@nrwl/linter:init', () => {
|
||||
});
|
||||
|
||||
expect(readJson(tree, 'nx.json').targetDefaults.lint).toEqual({
|
||||
inputs: ['default', '{workspaceRoot}/.eslintrc.json'],
|
||||
inputs: [
|
||||
'default',
|
||||
'{workspaceRoot}/.eslintrc.json',
|
||||
'{workspaceRoot}/.eslintignore',
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -42,6 +42,7 @@ function addTargetDefaults(tree: Tree) {
|
||||
nxJson.targetDefaults.lint.inputs ??= [
|
||||
'default',
|
||||
`{workspaceRoot}/.eslintrc.json`,
|
||||
`{workspaceRoot}/.eslintignore`,
|
||||
];
|
||||
updateNxJson(tree, nxJson);
|
||||
}
|
||||
@ -60,6 +61,7 @@ function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
|
||||
'.eslintrc.json',
|
||||
getGlobalEsLintConfiguration(options.unitTestRunner, options.rootProject)
|
||||
);
|
||||
tree.write('.eslintignore', 'node_modules\n');
|
||||
addTargetDefaults(tree);
|
||||
|
||||
if (tree.exists('.vscode/extensions.json')) {
|
||||
|
||||
10
packages/linter/src/generators/utils/eslint-targets.ts
Normal file
10
packages/linter/src/generators/utils/eslint-targets.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Tree } from '@nrwl/devkit';
|
||||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
|
||||
|
||||
export function getEslintTargets(tree: Tree) {
|
||||
const eslintTargetNames = new Set<string>();
|
||||
forEachExecutorOptions(tree, '@nrwl/linter:eslint', (_, __, target) => {
|
||||
eslintTargetNames.add(target);
|
||||
});
|
||||
return eslintTargetNames;
|
||||
}
|
||||
@ -5,8 +5,8 @@ import {
|
||||
Tree,
|
||||
updateNxJson,
|
||||
} from '@nrwl/devkit';
|
||||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
|
||||
import { eslintConfigFileWhitelist } from '../../generators/utils/eslint-file';
|
||||
import { getEslintTargets } from '../../generators/utils/eslint-targets';
|
||||
|
||||
export default async function addEslintInputs(tree: Tree) {
|
||||
const nxJson = readNxJson(tree);
|
||||
@ -40,11 +40,3 @@ export default async function addEslintInputs(tree: Tree) {
|
||||
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
function getEslintTargets(tree: Tree) {
|
||||
const eslintTargetNames = new Set<string>();
|
||||
forEachExecutorOptions(tree, '@nrwl/linter:eslint', (_, __, target) => {
|
||||
eslintTargetNames.add(target);
|
||||
});
|
||||
return eslintTargetNames;
|
||||
}
|
||||
|
||||
@ -0,0 +1,107 @@
|
||||
import {
|
||||
addProjectConfiguration,
|
||||
readJson,
|
||||
readNxJson,
|
||||
Tree,
|
||||
updateNxJson,
|
||||
} from '@nrwl/devkit';
|
||||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
|
||||
import addEslintIgnore from './add-eslint-ignore';
|
||||
|
||||
describe('15.7.1 migration (add-eslintignore)', () => {
|
||||
let tree: Tree;
|
||||
|
||||
beforeEach(() => {
|
||||
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
|
||||
|
||||
updateNxJson(tree, {
|
||||
namedInputs: {
|
||||
default: ['{projectRoot}/**/*', 'sharedGlobals'],
|
||||
sharedGlobals: [],
|
||||
},
|
||||
});
|
||||
|
||||
addProjectConfiguration(tree, 'proj', {
|
||||
root: 'proj',
|
||||
targets: {
|
||||
lint: {
|
||||
executor: '@nrwl/linter:eslint',
|
||||
options: {},
|
||||
},
|
||||
lint2: {
|
||||
executor: '@nrwl/linter:eslint',
|
||||
options: {},
|
||||
},
|
||||
notTest: {
|
||||
executor: 'nx:run-commands',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should not add .eslintignore if eslint config does not exist', async () => {
|
||||
await addEslintIgnore(tree);
|
||||
expect(tree.exists('.eslintignore')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should add .eslintignore if it does not exist', async () => {
|
||||
ensureGlobalConfig(tree);
|
||||
|
||||
await addEslintIgnore(tree);
|
||||
|
||||
expect(tree.exists('.eslintignore')).toBeTruthy();
|
||||
expect(tree.read('.eslintignore', 'utf-8')).toEqual('node_modules\n');
|
||||
});
|
||||
|
||||
it('should add node_modules if missing in .eslintignore', async () => {
|
||||
ensureGlobalConfig(tree);
|
||||
|
||||
const original = 'dist\ntmp\n';
|
||||
tree.write('.eslintignore', original);
|
||||
|
||||
await addEslintIgnore(tree);
|
||||
|
||||
expect(tree.read('.eslintignore', 'utf-8')).toEqual(
|
||||
`node_modules\n${original}`
|
||||
);
|
||||
});
|
||||
|
||||
it('should not add node_modules if already in .eslintignore', async () => {
|
||||
ensureGlobalConfig(tree);
|
||||
|
||||
const original = `dist\nnode_modules\ntmp\n`;
|
||||
tree.write('.eslintignore', original);
|
||||
|
||||
await addEslintIgnore(tree);
|
||||
|
||||
expect(tree.read('.eslintignore', 'utf-8')).toEqual(original);
|
||||
});
|
||||
|
||||
it('should add lint target', async () => {
|
||||
ensureGlobalConfig(tree);
|
||||
|
||||
await addEslintIgnore(tree);
|
||||
|
||||
expect(tree.exists('.eslintignore')).toBeTruthy();
|
||||
expect(readJson(tree, 'nx.json').targetDefaults).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"lint": Object {
|
||||
"inputs": Array [
|
||||
"default",
|
||||
"{workspaceRoot}/.eslintrc.json",
|
||||
],
|
||||
},
|
||||
"lint2": Object {
|
||||
"inputs": Array [
|
||||
"default",
|
||||
"{workspaceRoot}/.eslintrc.json",
|
||||
],
|
||||
},
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
function ensureGlobalConfig(tree: Tree) {
|
||||
tree.write('.eslintrc.json', '{}');
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
import {
|
||||
formatFiles,
|
||||
joinPathFragments,
|
||||
readJson,
|
||||
Tree,
|
||||
updateNxJson,
|
||||
} from '@nrwl/devkit';
|
||||
import { eslintConfigFileWhitelist } from '../../generators/utils/eslint-file';
|
||||
import { getEslintTargets } from '../../generators/utils/eslint-targets';
|
||||
|
||||
export default async function addEslintIgnore(tree: Tree) {
|
||||
const nxJson = readJson(tree, 'nx.json');
|
||||
|
||||
const globalEslintFile = eslintConfigFileWhitelist.find((file) =>
|
||||
tree.exists(file)
|
||||
);
|
||||
|
||||
if (globalEslintFile) {
|
||||
if (tree.exists('.eslintignore')) {
|
||||
const content = tree.read('.eslintignore', 'utf-8');
|
||||
if (!content.includes('node_modules')) {
|
||||
tree.write('.eslintignore', `node_modules\n${content}`);
|
||||
}
|
||||
} else {
|
||||
tree.write('.eslintignore', 'node_modules\n');
|
||||
}
|
||||
|
||||
for (const targetName of getEslintTargets(tree)) {
|
||||
nxJson.targetDefaults ??= {};
|
||||
const lintTargetDefaults = (nxJson.targetDefaults[targetName] ??= {});
|
||||
|
||||
const lintIgnorePath = joinPathFragments(
|
||||
'{workspaceRoot}',
|
||||
globalEslintFile
|
||||
);
|
||||
|
||||
if (lintTargetDefaults.inputs) {
|
||||
if (!lintTargetDefaults.inputs.includes(lintIgnorePath)) {
|
||||
lintTargetDefaults.inputs.push(lintIgnorePath);
|
||||
}
|
||||
} else {
|
||||
lintTargetDefaults.inputs = ['default', lintIgnorePath];
|
||||
}
|
||||
}
|
||||
|
||||
updateNxJson(tree, nxJson);
|
||||
await formatFiles(tree);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user