-
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.
fix(@schematics/angular): add migration to ensure tslint deprecation …
…rule This ensures that projects will be warned when deprecated APIs are used when linted.
- Loading branch information
1 parent
4ea0efd
commit 8cc566d
Showing
3 changed files
with
152 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
64 changes: 64 additions & 0 deletions
64
packages/schematics/angular/migrations/update-10/add-deprecation-rule-tslint.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,64 @@ | ||
/** | ||
* @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 { JsonAstObject, JsonValue } from '@angular-devkit/core'; | ||
import { Rule } from '@angular-devkit/schematics'; | ||
import { getPackageJsonDependency } from '../../utility/dependencies'; | ||
import { findPropertyInAstObject, insertPropertyInAstObjectInOrder, removePropertyInAstObject } from '../../utility/json-utils'; | ||
import { readJsonFileAsAstObject } from '../update-9/utils'; | ||
|
||
const TSLINT_CONFIG_PATH = '/tslint.json'; | ||
const RULES_TO_ADD: Record<string, JsonValue> = { | ||
deprecation: { | ||
severity: 'warning', | ||
}, | ||
}; | ||
|
||
export default function (): Rule { | ||
return (tree, context) => { | ||
const logger = context.logger; | ||
|
||
// Update tslint dependency | ||
const current = getPackageJsonDependency(tree, 'tslint'); | ||
|
||
if (!current) { | ||
logger.info('Skipping: "tslint" in not a dependency of this workspace.'); | ||
|
||
return; | ||
} | ||
|
||
// Update tslint config. | ||
const tslintJsonAst = readJsonFileAsAstObject(tree, TSLINT_CONFIG_PATH); | ||
if (!tslintJsonAst) { | ||
const config = ['tslint.js', 'tslint.yaml'].find(c => tree.exists(c)); | ||
if (config) { | ||
logger.warn(`Skipping: Expected a JSON configuration file but found "${config}".`); | ||
} else { | ||
logger.warn('Skipping: Cannot find "tslint.json" configuration file.'); | ||
} | ||
|
||
return; | ||
} | ||
|
||
for (const [name, value] of Object.entries(RULES_TO_ADD)) { | ||
const tslintJsonAst = readJsonFileAsAstObject(tree, TSLINT_CONFIG_PATH) as JsonAstObject; | ||
const rulesAst = findPropertyInAstObject(tslintJsonAst, 'rules'); | ||
if (rulesAst?.kind !== 'object') { | ||
break; | ||
} | ||
|
||
if (findPropertyInAstObject(rulesAst, name)) { | ||
// Skip as rule already exists. | ||
continue; | ||
} | ||
|
||
const recorder = tree.beginUpdate(TSLINT_CONFIG_PATH); | ||
insertPropertyInAstObjectInOrder(recorder, rulesAst, name, value, 4); | ||
tree.commitUpdate(recorder); | ||
} | ||
}; | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/schematics/angular/migrations/update-10/add-deprecation-rule-tslint_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,83 @@ | ||
/** | ||
* @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 { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
||
describe('Migration of tslint to version 6', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
const TSLINT_PATH = '/tslint.json'; | ||
const PACKAGE_JSON_PATH = '/package.json'; | ||
|
||
const PACKAGE_JSON = { | ||
devDependencies: { | ||
tslint: '~6.1.0', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
tree.create(PACKAGE_JSON_PATH, JSON.stringify(PACKAGE_JSON, null, 2)); | ||
}); | ||
|
||
it('should add new rules', async () => { | ||
const config = { | ||
extends: 'tslint:recommended', | ||
rules: { | ||
'no-use-before-declare': true, | ||
'arrow-return-shorthand': false, | ||
'label-position': true, | ||
}, | ||
}; | ||
tree.create(TSLINT_PATH, JSON.stringify(config, null, 2)); | ||
|
||
const newTree = await schematicRunner.runSchematicAsync('tslint-add-deprecation-rule', {}, tree).toPromise(); | ||
const { rules } = JSON.parse(newTree.readContent(TSLINT_PATH)); | ||
expect(rules['deprecation'].severity).toBe('warning'); | ||
}); | ||
|
||
it('should not update already present rules', async () => { | ||
const config = { | ||
extends: 'tslint:recommended', | ||
rules: { | ||
'no-use-before-declare': true, | ||
'arrow-return-shorthand': false, | ||
'label-position': true, | ||
deprecation: false, | ||
}, | ||
}; | ||
tree.create(TSLINT_PATH, JSON.stringify(config, null, 2)); | ||
|
||
const newTree = await schematicRunner.runSchematicAsync('tslint-add-deprecation-rule', {}, tree).toPromise(); | ||
const { rules } = JSON.parse(newTree.readContent(TSLINT_PATH)); | ||
expect(rules['deprecation']).toBe(false); | ||
}); | ||
|
||
it('should not update already present rules with different severity', async () => { | ||
const config = { | ||
extends: 'tslint:recommended', | ||
rules: { | ||
'no-use-before-declare': true, | ||
'arrow-return-shorthand': false, | ||
'label-position': true, | ||
deprecation: { | ||
severity: 'error', | ||
}, | ||
}, | ||
}; | ||
tree.create(TSLINT_PATH, JSON.stringify(config, null, 2)); | ||
|
||
const newTree = await schematicRunner.runSchematicAsync('tslint-add-deprecation-rule', {}, tree).toPromise(); | ||
const { rules } = JSON.parse(newTree.readContent(TSLINT_PATH)); | ||
expect(rules['deprecation'].severity).toBe('error'); | ||
}); | ||
}); |