UNPKG

11.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.addLintFiles = exports.generateProjectLint = void 0;
4const tslib_1 = require("tslib");
5const core_1 = require("@angular-devkit/core");
6const schematics_1 = require("@angular-devkit/schematics");
7const ast_utils_1 = require("./ast-utils");
8const versions_1 = require("./versions");
9const devkit_1 = require("@nrwl/devkit");
10function generateProjectLint(projectRoot, tsConfigPath, linter, eslintFilePatterns) {
11 if (linter === "tslint" /* Linter.TsLint */) {
12 return {
13 builder: '@angular-devkit/build-angular:tslint',
14 options: {
15 tsConfig: [tsConfigPath],
16 exclude: ['**/node_modules/**', `!${projectRoot}/**/*`],
17 },
18 };
19 }
20 else if (linter === "eslint" /* Linter.EsLint */) {
21 return {
22 builder: '@nrwl/linter:eslint',
23 options: {
24 lintFilePatterns: eslintFilePatterns,
25 },
26 };
27 }
28 else {
29 return undefined;
30 }
31}
32exports.generateProjectLint = generateProjectLint;
33function addLintFiles(projectRoot, linter, options = {}) {
34 return (host, context) => {
35 var _a, _b;
36 if (options.onlyGlobal && options.localConfig) {
37 throw new Error('onlyGlobal and localConfig cannot be used at the same time');
38 }
39 const chainedCommands = [];
40 if (linter === 'tslint') {
41 chainedCommands.push((host) => {
42 if (!host.exists('/tslint.json')) {
43 host.create('/tslint.json', globalTsLint);
44 }
45 if (!options.onlyGlobal) {
46 host.create((0, core_1.join)(projectRoot, `tslint.json`), JSON.stringify({
47 extends: `${(0, devkit_1.offsetFromRoot)(projectRoot)}tslint.json`,
48 // Include project files to be linted since the global one excludes all files.
49 linterOptions: {
50 exclude: ['!**/*'],
51 },
52 rules: {},
53 }));
54 }
55 });
56 chainedCommands.push((0, ast_utils_1.addDepsToPackageJson)({}, {
57 tslint: versions_1.tslintVersion,
58 '@angular-devkit/build-angular': versions_1.angularCliVersion,
59 }));
60 return (0, schematics_1.chain)(chainedCommands);
61 }
62 if (linter === 'eslint') {
63 if (!host.exists('/.eslintrc.json')) {
64 host.create('/.eslintrc.json', globalESLint);
65 }
66 chainedCommands.push((0, ast_utils_1.addDepsToPackageJson)(Object.assign({}, (options.extraPackageDeps
67 ? options.extraPackageDeps.dependencies
68 : {})), Object.assign({ '@nrwl/linter': versions_1.nxVersion, '@nrwl/eslint-plugin-nx': versions_1.nxVersion, '@typescript-eslint/parser': versions_1.typescriptESLintVersion, '@typescript-eslint/eslint-plugin': versions_1.typescriptESLintVersion, eslint: versions_1.eslintVersion, 'eslint-config-prettier': versions_1.eslintConfigPrettierVersion }, ((_b = (_a = options.extraPackageDeps) === null || _a === void 0 ? void 0 : _a.devDependencies) !== null && _b !== void 0 ? _b : {}))));
69 if (!options.onlyGlobal) {
70 chainedCommands.push((host) => {
71 let configJson;
72 const rootConfig = `${(0, devkit_1.offsetFromRoot)(projectRoot)}.eslintrc.json`;
73 // Include all project files to be linted (since they are turned off in the root eslintrc file).
74 const ignorePatterns = ['!**/*'];
75 if (options.localConfig) {
76 /**
77 * The end config is much easier to reason about if "extends" comes first,
78 * so as well as applying the extension from the root lint config, we also
79 * adjust the config to make extends come first.
80 */
81 const _a = options.localConfig, { extends: extendsVal } = _a, localConfigExceptExtends = tslib_1.__rest(_a, ["extends"]);
82 const extendsOption = extendsVal
83 ? Array.isArray(extendsVal)
84 ? extendsVal
85 : [extendsVal]
86 : [];
87 configJson = Object.assign({ extends: [...extendsOption, rootConfig], ignorePatterns }, localConfigExceptExtends);
88 }
89 else {
90 configJson = {
91 extends: rootConfig,
92 ignorePatterns,
93 overrides: [
94 {
95 files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
96 /**
97 * NOTE: We no longer set parserOptions.project by default when creating new projects.
98 *
99 * We have observed that users rarely add rules requiring type-checking to their Nx workspaces, and therefore
100 * do not actually need the capabilites which parserOptions.project provides. When specifying parserOptions.project,
101 * typescript-eslint needs to create full TypeScript Programs for you. When omitting it, it can perform a simple
102 * parse (and AST tranformation) of the source files it encounters during a lint run, which is much faster and much
103 * less memory intensive.
104 *
105 * In the rare case that users attempt to add rules requiring type-checking to their setup later on (and haven't set
106 * parserOptions.project), the executor will attempt to look for the particular error typescript-eslint gives you
107 * and provide feedback to the user.
108 */
109 parserOptions: !options.setParserOptionsProject
110 ? undefined
111 : {
112 project: [`${projectRoot}/tsconfig.*?.json`],
113 },
114 /**
115 * Having an empty rules object present makes it more obvious to the user where they would
116 * extend things from if they needed to
117 */
118 rules: {},
119 },
120 {
121 files: ['*.ts', '*.tsx'],
122 rules: {},
123 },
124 {
125 files: ['*.js', '*.jsx'],
126 rules: {},
127 },
128 ],
129 };
130 }
131 host.create((0, core_1.join)(projectRoot, `.eslintrc.json`), JSON.stringify(configJson));
132 });
133 }
134 return (0, schematics_1.chain)(chainedCommands);
135 }
136 };
137}
138exports.addLintFiles = addLintFiles;
139const globalTsLint = `
140{
141 "rulesDirectory": ["node_modules/@nrwl/workspace/src/tslint"],
142 "linterOptions": {
143 "exclude": ["**/*"]
144 },
145 "rules": {
146 "arrow-return-shorthand": true,
147 "callable-types": true,
148 "class-name": true,
149 "deprecation": {
150 "severity": "warn"
151 },
152 "forin": true,
153 "import-blacklist": [true, "rxjs/Rx"],
154 "interface-over-type-literal": true,
155 "member-access": false,
156 "member-ordering": [
157 true,
158 {
159 "order": [
160 "static-field",
161 "instance-field",
162 "static-method",
163 "instance-method"
164 ]
165 }
166 ],
167 "no-arg": true,
168 "no-bitwise": true,
169 "no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
170 "no-construct": true,
171 "no-debugger": true,
172 "no-duplicate-super": true,
173 "no-empty": false,
174 "no-empty-interface": true,
175 "no-eval": true,
176 "no-inferrable-types": [true, "ignore-params"],
177 "no-misused-new": true,
178 "no-non-null-assertion": true,
179 "no-shadowed-variable": true,
180 "no-string-literal": false,
181 "no-string-throw": true,
182 "no-switch-case-fall-through": true,
183 "no-unnecessary-initializer": true,
184 "no-unused-expression": true,
185 "no-var-keyword": true,
186 "object-literal-sort-keys": false,
187 "prefer-const": true,
188 "radix": true,
189 "triple-equals": [true, "allow-null-check"],
190 "unified-signatures": true,
191 "variable-name": false,
192
193 "nx-enforce-module-boundaries": [
194 true,
195 {
196 "enforceBuildableLibDependency": true,
197 "allow": [],
198 "depConstraints": [
199 { "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
200 ]
201 }
202 ]
203 }
204}
205`;
206const globalESLint = JSON.stringify({
207 root: true,
208 ignorePatterns: ['**/*'],
209 plugins: ['@nrwl/nx'],
210 /**
211 * We leverage ESLint's "overrides" capability so that we can set up a root config which will support
212 * all permutations of Nx workspaces across all frameworks, libraries and tools.
213 *
214 * The key point is that we need entirely different ESLint config to apply to different types of files,
215 * but we still want to share common config where possible.
216 */
217 overrides: [
218 /**
219 * This configuration is intended to apply to all "source code" (but not
220 * markup like HTML, or other custom file types like GraphQL)
221 */
222 {
223 files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
224 rules: {
225 '@nrwl/nx/enforce-module-boundaries': [
226 'error',
227 {
228 enforceBuildableLibDependency: true,
229 allow: [],
230 depConstraints: [
231 { sourceTag: '*', onlyDependOnLibsWithTags: ['*'] },
232 ],
233 },
234 ],
235 },
236 },
237 /**
238 * This configuration is intended to apply to all TypeScript source files.
239 * See the eslint-plugin-nx package for what is in the referenced shareable config.
240 */
241 {
242 files: ['*.ts', '*.tsx'],
243 extends: ['plugin:@nrwl/nx/typescript'],
244 /**
245 * Having an empty rules object present makes it more obvious to the user where they would
246 * extend things from if they needed to
247 */
248 rules: {},
249 },
250 /**
251 * This configuration is intended to apply to all JavaScript source files.
252 * See the eslint-plugin-nx package for what is in the referenced shareable config.
253 */
254 {
255 files: ['*.js', '*.jsx'],
256 extends: ['plugin:@nrwl/nx/javascript'],
257 /**
258 * Having an empty rules object present makes it more obvious to the user where they would
259 * extend things from if they needed to
260 */
261 rules: {},
262 },
263 ],
264});
265//# sourceMappingURL=lint.js.map
\No newline at end of file