1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.TSLINT_VERSION = void 0;
|
---|
11 | const dependencies_1 = require("../../utility/dependencies");
|
---|
12 | const json_file_1 = require("../../utility/json-file");
|
---|
13 | exports.TSLINT_VERSION = '~6.1.0';
|
---|
14 | const TSLINT_CONFIG_PATH = '/tslint.json';
|
---|
15 | const RULES_TO_DELETE = ['no-use-before-declare', 'no-unused-variable'];
|
---|
16 | const RULES_TO_ADD = {
|
---|
17 | align: {
|
---|
18 | options: ['parameters', 'statements'],
|
---|
19 | },
|
---|
20 | 'arrow-return-shorthand': true,
|
---|
21 | curly: true,
|
---|
22 | eofline: true,
|
---|
23 | 'import-spacing': true,
|
---|
24 | indent: {
|
---|
25 | options: ['spaces'],
|
---|
26 | },
|
---|
27 | 'variable-name': {
|
---|
28 | options: ['ban-keywords', 'check-format', 'allow-pascal-case'],
|
---|
29 | },
|
---|
30 | semicolon: { options: ['always'] },
|
---|
31 | 'space-before-function-paren': {
|
---|
32 | options: {
|
---|
33 | anonymous: 'never',
|
---|
34 | asyncArrow: 'always',
|
---|
35 | constructor: 'never',
|
---|
36 | method: 'never',
|
---|
37 | named: 'never',
|
---|
38 | },
|
---|
39 | },
|
---|
40 | 'typedef-whitespace': {
|
---|
41 | options: [
|
---|
42 | {
|
---|
43 | 'call-signature': 'nospace',
|
---|
44 | 'index-signature': 'nospace',
|
---|
45 | parameter: 'nospace',
|
---|
46 | 'property-declaration': 'nospace',
|
---|
47 | 'variable-declaration': 'nospace',
|
---|
48 | },
|
---|
49 | {
|
---|
50 | 'call-signature': 'onespace',
|
---|
51 | 'index-signature': 'onespace',
|
---|
52 | parameter: 'onespace',
|
---|
53 | 'property-declaration': 'onespace',
|
---|
54 | 'variable-declaration': 'onespace',
|
---|
55 | },
|
---|
56 | ],
|
---|
57 | },
|
---|
58 | whitespace: {
|
---|
59 | options: [
|
---|
60 | 'check-branch',
|
---|
61 | 'check-decl',
|
---|
62 | 'check-operator',
|
---|
63 | 'check-separator',
|
---|
64 | 'check-type',
|
---|
65 | 'check-typecast',
|
---|
66 | ],
|
---|
67 | },
|
---|
68 | };
|
---|
69 | function default_1() {
|
---|
70 | return (tree, context) => {
|
---|
71 | const logger = context.logger;
|
---|
72 | // Update tslint dependency
|
---|
73 | const current = dependencies_1.getPackageJsonDependency(tree, 'tslint');
|
---|
74 | if (!current) {
|
---|
75 | logger.info('"tslint" in not a dependency of this workspace.');
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if (current.version !== exports.TSLINT_VERSION) {
|
---|
79 | dependencies_1.addPackageJsonDependency(tree, {
|
---|
80 | type: current.type,
|
---|
81 | name: 'tslint',
|
---|
82 | version: exports.TSLINT_VERSION,
|
---|
83 | overwrite: true,
|
---|
84 | });
|
---|
85 | }
|
---|
86 | // Update tslint config.
|
---|
87 | let json;
|
---|
88 | try {
|
---|
89 | json = new json_file_1.JSONFile(tree, TSLINT_CONFIG_PATH);
|
---|
90 | }
|
---|
91 | catch {
|
---|
92 | const config = ['tslint.js', 'tslint.yaml'].find((c) => tree.exists(c));
|
---|
93 | if (config) {
|
---|
94 | logger.warn(`Expected a JSON configuration file but found "${config}".`);
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | logger.warn('Cannot find "tslint.json" configuration file.');
|
---|
98 | }
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | // Remove old/deprecated rules.
|
---|
102 | for (const rule of RULES_TO_DELETE) {
|
---|
103 | json.remove(['rules', rule]);
|
---|
104 | }
|
---|
105 | // Add new rules only iif the configuration extends 'tslint:recommended'.
|
---|
106 | // This is because some rules conflict with prettier or other tools.
|
---|
107 | const extend = json.get(['extends']);
|
---|
108 | if (extend !== 'tslint:recommended' ||
|
---|
109 | (Array.isArray(extend) && extend.some((e) => e.value !== 'tslint:recommended'))) {
|
---|
110 | logger.warn(`tslint configuration does not extend "tslint:recommended" or it extends multiple configurations.` +
|
---|
111 | '\nSkipping rule changes as some rules might conflict.');
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | for (const [name, value] of Object.entries(RULES_TO_ADD)) {
|
---|
115 | const ruleName = ['rules', name];
|
---|
116 | if (json.get(ruleName) === undefined) {
|
---|
117 | json.modify(ruleName, value);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | };
|
---|
121 | }
|
---|
122 | exports.default = default_1;
|
---|