-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 mgechev/codelyzer#754.
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
packages/schematics/angular/migrations/update-8/index_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
}); | ||
}); | ||
}); |