|
| 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 { JsonParseMode, Path, normalize, parseJson, parseJsonAst } from '@angular-devkit/core'; |
| 9 | +import { |
| 10 | + Rule, |
| 11 | + SchematicsException, |
| 12 | + Tree, |
| 13 | + chain, |
| 14 | +} from '@angular-devkit/schematics'; |
| 15 | +import { |
| 16 | + NodeDependency, |
| 17 | + NodeDependencyType, |
| 18 | + addPackageJsonDependency, |
| 19 | +} from '../../utility/dependencies'; |
| 20 | +import { findPropertyInAstObject } from '../../utility/json-utils'; |
| 21 | + |
| 22 | +const ruleMapping: {[key: string]: string} = { |
| 23 | + 'contextual-life-cycle': 'contextual-lifecycle', |
| 24 | + 'no-conflicting-life-cycle-hooks': 'no-conflicting-lifecycle', |
| 25 | + 'no-life-cycle-call': 'no-lifecycle-call', |
| 26 | + 'use-life-cycle-interface': 'use-lifecycle-interface', |
| 27 | + 'decorator-not-allowed': 'contextual-decorator', |
| 28 | + 'enforce-component-selector': 'use-component-selector', |
| 29 | + 'no-output-named-after-standard-event': 'no-output-native', |
| 30 | + 'use-host-property-decorator': 'no-host-metadata-property', |
| 31 | + 'use-input-property-decorator': 'no-inputs-metadata-property', |
| 32 | + 'use-output-property-decorator': 'no-outputs-metadata-property', |
| 33 | + 'no-queries-parameter': 'no-queries-metadata-property', |
| 34 | + 'pipe-impure': 'no-pipe-impure', |
| 35 | + 'use-view-encapsulation': 'use-component-view-encapsulation', |
| 36 | + i18n: 'template-i18n', |
| 37 | + 'banana-in-box': 'template-banana-in-box', |
| 38 | + 'no-template-call-expression': 'template-no-call-expression', |
| 39 | + 'templates-no-negated-async': 'template-no-negated-async', |
| 40 | + 'trackBy-function': 'template-use-track-by-function', |
| 41 | + 'no-attribute-parameter-decorator': 'no-attribute-decorator', |
| 42 | + 'max-inline-declarations': 'component-max-inline-declarations', |
| 43 | +}; |
| 44 | + |
| 45 | +function updateTsLintConfig(): Rule { |
| 46 | + return (host: Tree) => { |
| 47 | + const tsLintPath = '/tslint.json'; |
| 48 | + const buffer = host.read(tsLintPath); |
| 49 | + if (!buffer) { |
| 50 | + return host; |
| 51 | + } |
| 52 | + const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose); |
| 53 | + |
| 54 | + if (tsCfgAst.kind != 'object') { |
| 55 | + return host; |
| 56 | + } |
| 57 | + |
| 58 | + const rulesNode = findPropertyInAstObject(tsCfgAst, 'rules'); |
| 59 | + if (!rulesNode || rulesNode.kind != 'object') { |
| 60 | + return host; |
| 61 | + } |
| 62 | + |
| 63 | + const recorder = host.beginUpdate(tsLintPath); |
| 64 | + |
| 65 | + rulesNode.properties.forEach(prop => { |
| 66 | + const mapping = ruleMapping[prop.key.value]; |
| 67 | + if (mapping) { |
| 68 | + recorder.remove(prop.key.start.offset + 1, prop.key.value.length); |
| 69 | + recorder.insertLeft(prop.key.start.offset + 1, mapping); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + host.commitUpdate(recorder); |
| 74 | + |
| 75 | + return host; |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function updatePackageJson() { |
| 80 | + return (host: Tree) => { |
| 81 | + const dependency: NodeDependency = { |
| 82 | + type: NodeDependencyType.Dev, |
| 83 | + name: 'codelyzer', |
| 84 | + version: '^5.0.0', |
| 85 | + overwrite: true, |
| 86 | + }; |
| 87 | + |
| 88 | + addPackageJsonDependency(host, dependency); |
| 89 | + |
| 90 | + return host; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +function getConfigPath(tree: Tree): Path { |
| 95 | + const possiblePath = normalize('angular.json'); |
| 96 | + if (tree.exists(possiblePath)) { |
| 97 | + return possiblePath; |
| 98 | + } |
| 99 | + |
| 100 | + throw new SchematicsException('Could not find configuration file'); |
| 101 | +} |
| 102 | + |
| 103 | +export default function(): Rule { |
| 104 | + return () => { |
| 105 | + return chain([ |
| 106 | + updateTsLintConfig(), |
| 107 | + updatePackageJson(), |
| 108 | + ]); |
| 109 | + }; |
| 110 | +} |
0 commit comments