|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { JsonAstObject, JsonValue } from '@angular-devkit/core'; |
| 9 | +import { Rule } from '@angular-devkit/schematics'; |
| 10 | +import { addPackageJsonDependency, getPackageJsonDependency } from '../../utility/dependencies'; |
| 11 | +import { findPropertyInAstObject, insertPropertyInAstObjectInOrder, removePropertyInAstObject } from '../../utility/json-utils'; |
| 12 | +import { readJsonFileAsAstObject } from '../update-9/utils'; |
| 13 | + |
| 14 | +export const TSLINT_VERSION = '~6.1.0'; |
| 15 | +const TSLINT_CONFIG_PATH = '/tslint.json'; |
| 16 | + |
| 17 | +const RULES_TO_DELETE: string[] = [ |
| 18 | + 'no-use-before-declare', |
| 19 | + 'no-unused-variable', |
| 20 | +]; |
| 21 | + |
| 22 | +const RULES_TO_ADD: Record<string, JsonValue> = { |
| 23 | + align: { |
| 24 | + options: ['parameters', 'statements'], |
| 25 | + }, |
| 26 | + 'arrow-return-shorthand': true, |
| 27 | + curly: true, |
| 28 | + eofline: true, |
| 29 | + 'import-spacing': true, |
| 30 | + indent: { |
| 31 | + options: ['spaces'], |
| 32 | + }, |
| 33 | + 'variable-name': { |
| 34 | + options: ['ban-keywords', 'check-format', 'allow-pascal-case'], |
| 35 | + }, |
| 36 | + semicolon: { options: ['always'] }, |
| 37 | + 'space-before-function-paren': { |
| 38 | + options: { |
| 39 | + anonymous: 'never', |
| 40 | + asyncArrow: 'always', |
| 41 | + constructor: 'never', |
| 42 | + method: 'never', |
| 43 | + named: 'never', |
| 44 | + }, |
| 45 | + }, |
| 46 | + 'typedef-whitespace': { |
| 47 | + options: [ |
| 48 | + { |
| 49 | + 'call-signature': 'nospace', |
| 50 | + 'index-signature': 'nospace', |
| 51 | + parameter: 'nospace', |
| 52 | + 'property-declaration': 'nospace', |
| 53 | + 'variable-declaration': 'nospace', |
| 54 | + }, |
| 55 | + { |
| 56 | + 'call-signature': 'onespace', |
| 57 | + 'index-signature': 'onespace', |
| 58 | + parameter: 'onespace', |
| 59 | + 'property-declaration': 'onespace', |
| 60 | + 'variable-declaration': 'onespace', |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + whitespace: { |
| 65 | + options: [ |
| 66 | + 'check-branch', |
| 67 | + 'check-decl', |
| 68 | + 'check-operator', |
| 69 | + 'check-separator', |
| 70 | + 'check-type', |
| 71 | + 'check-typecast', |
| 72 | + ], |
| 73 | + }, |
| 74 | +}; |
| 75 | + |
| 76 | +export default function (): Rule { |
| 77 | + return (tree, context) => { |
| 78 | + const logger = context.logger; |
| 79 | + |
| 80 | + // Update tslint dependency |
| 81 | + const current = getPackageJsonDependency(tree, 'tslint'); |
| 82 | + |
| 83 | + if (!current) { |
| 84 | + logger.info('"tslint" in not a dependency of this workspace.'); |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (current.version !== TSLINT_VERSION) { |
| 90 | + addPackageJsonDependency(tree, { |
| 91 | + type: current.type, |
| 92 | + name: 'tslint', |
| 93 | + version: TSLINT_VERSION, |
| 94 | + overwrite: true, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + // Update tslint config. |
| 99 | + const tslintJsonAst = readJsonFileAsAstObject(tree, TSLINT_CONFIG_PATH); |
| 100 | + if (!tslintJsonAst) { |
| 101 | + const config = ['tslint.js', 'tslint.yaml'].find(c => tree.exists(c)); |
| 102 | + if (config) { |
| 103 | + logger.warn(`Expected a JSON configuration file but found "${config}".`); |
| 104 | + } else { |
| 105 | + logger.warn('Cannot find "tslint.json" configuration file.'); |
| 106 | + } |
| 107 | + |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Remove old/deprecated rules. |
| 112 | + for (const rule of RULES_TO_DELETE) { |
| 113 | + const tslintJsonAst = readJsonFileAsAstObject(tree, TSLINT_CONFIG_PATH) as JsonAstObject; |
| 114 | + const rulesAst = findPropertyInAstObject(tslintJsonAst, 'rules'); |
| 115 | + if (rulesAst?.kind !== 'object') { |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + const recorder = tree.beginUpdate(TSLINT_CONFIG_PATH); |
| 120 | + removePropertyInAstObject(recorder, rulesAst, rule); |
| 121 | + tree.commitUpdate(recorder); |
| 122 | + } |
| 123 | + |
| 124 | + // Add new rules only iif the configuration extends 'tslint:recommended'. |
| 125 | + // This is because some rules conflict with prettier or other tools. |
| 126 | + const extendsAst = findPropertyInAstObject(tslintJsonAst, 'extends'); |
| 127 | + if ( |
| 128 | + !extendsAst || |
| 129 | + (extendsAst.kind === 'string' && extendsAst.value !== 'tslint:recommended') || |
| 130 | + (extendsAst.kind === 'array' && extendsAst.elements.some(e => e.value !== 'tslint:recommended')) |
| 131 | + ) { |
| 132 | + logger.warn(`tslint configuration does not extend "tslint:recommended".` |
| 133 | + + '\nMigration will terminate as some rules might conflict.'); |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + for (const [name, value] of Object.entries(RULES_TO_ADD)) { |
| 139 | + const tslintJsonAst = readJsonFileAsAstObject(tree, TSLINT_CONFIG_PATH) as JsonAstObject; |
| 140 | + const rulesAst = findPropertyInAstObject(tslintJsonAst, 'rules'); |
| 141 | + if (rulesAst?.kind !== 'object') { |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + if (findPropertyInAstObject(rulesAst, name)) { |
| 146 | + // Skip as rule already exists. |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + const recorder = tree.beginUpdate(TSLINT_CONFIG_PATH); |
| 151 | + insertPropertyInAstObjectInOrder(recorder, rulesAst, name, value, 4); |
| 152 | + tree.commitUpdate(recorder); |
| 153 | + } |
| 154 | + |
| 155 | + }; |
| 156 | +} |
0 commit comments