From 8ce7c3c8413767e150390738fcfee5a5139e165e Mon Sep 17 00:00:00 2001 From: Minko Gechev Date: Fri, 1 Mar 2019 13:56:34 -0800 Subject: [PATCH] feat(@schematics/angular): tslint migration for 8 Migration of the `tslint.json` file required by the refactoring of codelyzer. For more information check this PR https://github.com/mgechev/codelyzer/pull/754. --- .../migrations/migration-collection.json | 5 ++ .../angular/migrations/update-8/index.ts | 85 +++++++++++++++++++ .../angular/migrations/update-8/index_spec.ts | 78 +++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 packages/schematics/angular/migrations/update-8/index.ts create mode 100644 packages/schematics/angular/migrations/update-8/index_spec.ts diff --git a/packages/schematics/angular/migrations/migration-collection.json b/packages/schematics/angular/migrations/migration-collection.json index 0e03d24ae78f..28a1abb74079 100644 --- a/packages/schematics/angular/migrations/migration-collection.json +++ b/packages/schematics/angular/migrations/migration-collection.json @@ -29,6 +29,11 @@ "version": "7.0.3", "factory": "./update-7/index#updateDevkitBuildNgPackagr", "description": "Update an Angular CLI project to version 7." + }, + "migration-07": { + "version": "8.0.0", + "factory": "./update-8", + "description": "Update an Angular CLI project to version 8." } } } diff --git a/packages/schematics/angular/migrations/update-8/index.ts b/packages/schematics/angular/migrations/update-8/index.ts new file mode 100644 index 000000000000..b956b9a91b5f --- /dev/null +++ b/packages/schematics/angular/migrations/update-8/index.ts @@ -0,0 +1,85 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +import { JsonParseMode, parseJsonAst, tags } from '@angular-devkit/core'; +import { + Rule, + SchematicContext, + Tree, + chain, +} from '@angular-devkit/schematics'; +import { findPropertyInAstObject } from '../../utility/json-utils'; + +const ruleMapping: {[key: string]: string} = { + 'contextual-life-cycle': 'contextual-lifecycle', + 'no-conflicting-life-cycle-hooks': 'no-conflicting-lifecycle', + 'no-life-cycle-call': 'no-lifecycle-call', + 'use-life-cycle-interface': 'use-lifecycle-interface', + 'decorator-not-allowed': 'contextual-decorator', + 'enforce-component-selector': 'use-component-selector', + 'no-output-named-after-standard-event': 'no-output-native', + 'use-host-property-decorator': 'no-host-metadata-property', + 'use-input-property-decorator': 'no-inputs-metadata-property', + 'use-output-property-decorator': 'no-outputs-metadata-property', + 'no-queries-parameter': 'no-queries-metadata-property', + 'pipe-impure': 'no-pipe-impure', + 'use-view-encapsulation': 'use-component-view-encapsulation', + i18n: 'template-i18n', + 'banana-in-box': 'template-banana-in-box', + 'no-template-call-expression': 'template-no-call-expression', + 'templates-no-negated-async': 'template-no-negated-async', + 'trackBy-function': 'template-use-track-by-function', +}; + +function updateTsLintConfig(): Rule { + return (host: Tree) => { + const tsLintPath = '/tslint.json'; + const buffer = host.read(tsLintPath); + if (!buffer) { + return host; + } + const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose); + + if (tsCfgAst.kind != 'object') { + return host; + } + + const rulesNode = findPropertyInAstObject(tsCfgAst, 'rules'); + if (!rulesNode || rulesNode.kind != 'object') { + return host; + } + + const recorder = host.beginUpdate(tsLintPath); + + rulesNode.properties.forEach(prop => { + const mapping = ruleMapping[prop.key.value]; + if (mapping) { + recorder.remove(prop.key.start.offset + 1, prop.key.value.length); + recorder.insertLeft(prop.key.start.offset + 1, mapping); + } + }); + + host.commitUpdate(recorder); + + return host; + }; +} + +export default function(): Rule { + return () => { + return chain([ + updateTsLintConfig(), + (host: Tree, context: SchematicContext) => { + context.logger + .warn(tags.oneLine`Some configuration options have been changed, + please make sure to update any npm scripts which you may have modified.`); + + return host; + }, + ]); + }; +} diff --git a/packages/schematics/angular/migrations/update-8/index_spec.ts b/packages/schematics/angular/migrations/update-8/index_spec.ts new file mode 100644 index 000000000000..127db02f9f7b --- /dev/null +++ b/packages/schematics/angular/migrations/update-8/index_spec.ts @@ -0,0 +1,78 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +// tslint:disable:no-big-function +import { EmptyTree } from '@angular-devkit/schematics'; +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; + +const renames = [ + 'use-lifecycle-interface', + 'no-host-metadata-property', + 'no-outputs-metadata-property', + 'no-inputs-metadata-property', +]; + +describe('Migration to version 8', () => { + const schematicRunner = new SchematicTestRunner( + 'migrations', + require.resolve('../migration-collection.json'), + ); + + let tree: UnitTestTree; + const baseConfig = {}; + const defaultOptions = {}; + const configPath = `/angular-cli.json`; + + describe('tslint.json', () => { + const tslintPath = '/tslint.json'; + // tslint:disable-next-line:no-any + let tslintConfig: any; + beforeEach(() => { + tslintConfig = { + rules: { + 'directive-selector': [ + true, + 'attribute', + 'app', + 'camelCase', + ], + 'component-selector': [ + true, + 'element', + 'app', + 'kebab-case', + ], + 'no-output-on-prefix': true, + 'use-input-property-decorator': true, + 'use-output-property-decorator': true, + 'use-host-property-decorator': true, + 'no-input-rename': true, + 'no-output-rename': true, + 'use-life-cycle-interface': true, + 'use-pipe-transform-interface': true, + 'component-class-suffix': true, + 'directive-class-suffix': true, + }, + }; + tree = new UnitTestTree(new EmptyTree()); + const packageJson = { + devDependencies: {}, + }; + tree.create('/package.json', JSON.stringify(packageJson, null, 2)); + }); + + it('should rename all previous rules', () => { + tree.create(configPath, JSON.stringify(baseConfig, null, 2)); + tree.create(tslintPath, JSON.stringify(tslintConfig, null, 2)); + tree = schematicRunner.runSchematic('migration-07', defaultOptions, tree); + const tslint = JSON.parse(tree.readContent(tslintPath)); + for (const rule of renames) { + expect(rule in tslint.rules).toBeTruthy(`Rule ${rule} not renamed`); + } + }); + }); +});