Skip to content

Commit

Permalink
feat(@schematics/angular): tslint migration for 8
Browse files Browse the repository at this point in the history
Migration of the `tslint.json` file required by the refactoring of
codelyzer. For more information check this PR
mgechev/codelyzer#754.
  • Loading branch information
mgechev committed Mar 1, 2019
1 parent 4a093e5 commit 8ce7c3c
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
85 changes: 85 additions & 0 deletions packages/schematics/angular/migrations/update-8/index.ts
Original file line number Diff line number Diff line change
@@ -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;
},
]);
};
}
78 changes: 78 additions & 0 deletions packages/schematics/angular/migrations/update-8/index_spec.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
});
});
});

0 comments on commit 8ce7c3c

Please sign in to comment.