-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
add-linting.ts
99 lines (88 loc) · 3.17 KB
/
add-linting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import {
addDependenciesToPackageJson,
GeneratorCallback,
joinPathFragments,
Tree,
updateJson,
} from '@nrwl/devkit';
import { extendReactEslintJson, extraEslintDependencies } from '@nrwl/react';
import { NormalizedSchema } from './normalize-options';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
export async function addLinting(
host: Tree,
options: NormalizedSchema
): Promise<GeneratorCallback> {
const lintTask = await lintProjectGenerator(host, {
linter: options.linter,
project: options.projectName,
tsConfigPaths: [
joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'),
],
unitTestRunner: options.unitTestRunner,
eslintFilePatterns: [`${options.appProjectRoot}/**/*.{ts,tsx,js,jsx}`],
skipFormat: true,
});
if (options.linter === Linter.EsLint) {
updateJson(
host,
joinPathFragments(options.appProjectRoot, '.eslintrc.json'),
(json) => {
json = extendReactEslintJson(json);
// Turn off @next/next/no-html-link-for-pages since there is an issue with nextjs throwing linting errors
// TODO(nicholas): remove after Vercel updates nextjs linter to only lint ["*.ts", "*.tsx", "*.js", "*.jsx"]
json.ignorePatterns = [...json.ignorePatterns, '.next/**/*'];
json.rules = {
'@next/next/no-html-link-for-pages': 'off',
...json.rules,
};
// Find the override that handles both TS and JS files.
const commonOverride = json.overrides?.find((o) =>
['*.ts', '*.tsx', '*.js', '*.jsx'].every((ext) =>
o.files.includes(ext)
)
);
if (commonOverride) {
// Only set parserOptions.project if it already exists (defined by options.setParserOptionsProject)
if (commonOverride.parserOptions?.project) {
commonOverride.parserOptions.project = [
`${options.appProjectRoot}/tsconfig(.*)?.json`,
];
}
// Configure custom pages directory for next rule
if (commonOverride.rules) {
commonOverride.rules = {
...commonOverride.rules,
'@next/next/no-html-link-for-pages': [
'error',
`${options.appProjectRoot}/pages`,
],
};
}
}
json.extends ??= [];
if (typeof json.extends === 'string') {
json.extends = [json.extends];
}
// add next.js configuration
json.extends.unshift(...['next', 'next/core-web-vitals']);
// remove nx/react plugin, as it conflicts with the next.js one
json.extends = json.extends.filter(
(name) => name !== 'plugin:@nrwl/nx/react'
);
json.extends.unshift('plugin:@nrwl/nx/react-typescript');
if (!json.env) {
json.env = {};
}
json.env.jest = true;
return json;
}
);
}
const installTask = addDependenciesToPackageJson(
host,
extraEslintDependencies.dependencies,
extraEslintDependencies.devDependencies
);
return runTasksInSerial(lintTask, installTask);
}