Skip to content

Commit b33518b

Browse files
committed
Material misc rules should not be part of the CDK
1 parent 57cd10f commit b33518b

18 files changed

+133
-63
lines changed

src/cdk/schematics/ng-update/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,30 @@
88

99
import {Rule} from '@angular-devkit/schematics';
1010
import {TargetVersion} from './target-version';
11+
import {cdkUpgradeData} from './upgrade-data';
1112
import {createUpgradeRule} from './upgrade-rules';
13+
import {UpgradeTSLintConfig} from './upgrade-rules/tslint-config';
14+
15+
/** List of additional upgrade rules which are specifically for the CDK. */
16+
const extraUpgradeRules = [
17+
// Misc check rules
18+
'check-template-misc',
19+
];
20+
21+
/** TSLint upgrade configuration that will be passed to the CDK ng-update rule. */
22+
const tslintUpgradeConfig: UpgradeTSLintConfig = {
23+
upgradeData: cdkUpgradeData,
24+
extraUpgradeRules,
25+
};
1226

1327
/** Entry point for the migration schematics with target of Angular Material 6.0.0 */
1428
export function updateToV6(): Rule {
15-
return createUpgradeRule(TargetVersion.V6);
29+
return createUpgradeRule(TargetVersion.V6, tslintUpgradeConfig);
1630
}
1731

1832
/** Entry point for the migration schematics with target of Angular Material 7.0.0 */
1933
export function updateToV7(): Rule {
20-
return createUpgradeRule(TargetVersion.V7);
34+
return createUpgradeRule(TargetVersion.V7, tslintUpgradeConfig);
2135
}
2236

2337
/** Post-update schematic to be called when update is finished. */

src/cdk/schematics/ng-update/public-api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
export * from './target-version';
1010
export * from './upgrade-data';
1111
export * from './data/index';
12+
export * from './html-parsing/angular';
13+
export * from './html-parsing/elements';
1214
export * from './upgrade-rules/index';
1315
export * from './upgrade-rules/tslint-config';
1416
export * from './tslint/component-walker';
@@ -17,4 +19,5 @@ export * from './tslint/external-failure-walker';
1719
export * from './typescript/base-types';
1820
export * from './typescript/imports';
1921
export * from './typescript/literal';
22+
export * from './typescript/module-specifiers';
2023

src/cdk/schematics/ng-update/upgrade-rules/class-names/classNamesIdentifierRule.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
import {green, red} from 'chalk';
1010
import {RuleFailure, Rules, RuleWalker} from 'tslint';
1111
import * as ts from 'typescript';
12-
import {
13-
isMaterialExportDeclaration,
14-
isMaterialImportDeclaration,
15-
} from '../../material/typescript-specifiers';
1612
import {
1713
isExportSpecifierNode,
1814
isImportSpecifierNode,
1915
isNamespaceImportNode,
2016
} from '../../typescript/imports';
17+
import {
18+
isMaterialExportDeclaration,
19+
isMaterialImportDeclaration,
20+
} from '../../typescript/module-specifiers';
2121
import {getUpgradeDataFromWalker} from '../../upgrade-data';
2222

2323
/**
24-
* Rule that walks through every identifier that is part of Angular Material and replaces the
25-
* outdated name with the new one.
24+
* Rule that walks through every identifier that is part of Angular Material or thr CDK
25+
* and replaces the outdated name with the new one if specified in the upgrade data.
2626
*/
2727
export class Rule extends Rules.AbstractRule {
2828

src/cdk/schematics/ng-update/upgrade-rules/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import {Rule, SchematicContext, TaskId, Tree} from '@angular-devkit/schematics';
1010
import {RunSchematicTask, TslintFixTask} from '@angular-devkit/schematics/tasks';
1111
import {sync as globSync} from 'glob';
12-
import {cdkUpgradeData} from '../upgrade-data';
1312
import {getProjectTsConfigPaths} from './project-tsconfig-paths';
1413
import {TargetVersion} from '../target-version';
1514
import {createTslintConfig, UpgradeTSLintConfig} from './tslint-config';
1615

1716
/** Creates a Angular schematic rule that runs the upgrade for the specified target version. */
1817
export function createUpgradeRule(targetVersion: TargetVersion,
19-
upgradeConfig?: UpgradeTSLintConfig): Rule {
18+
upgradeConfig: UpgradeTSLintConfig): Rule {
2019
return (tree: Tree, context: SchematicContext) => {
2120

2221
const projectTsConfigPaths = getProjectTsConfigPaths(tree);
@@ -33,7 +32,6 @@ export function createUpgradeRule(targetVersion: TargetVersion,
3332
const tslintConfig = createTslintConfig(targetVersion, {
3433
// Default options that can be overwritten if specified explicitly. e.g. if the
3534
// Material update schematic wants to specify a different upgrade data.
36-
upgradeData: cdkUpgradeData,
3735
extraStyleFiles: extraStyleFiles,
3836
// Custom upgrade configuration options.
3937
...upgradeConfig,

src/cdk/schematics/ng-update/upgrade-rules/misc-checks/checkTemplateMiscRule.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {bold, green, red} from 'chalk';
9+
import {green, red} from 'chalk';
1010
import {RuleFailure, Rules} from 'tslint';
1111
import * as ts from 'typescript';
12-
import {findInputsOnElementWithTag, findOutputsOnElementWithTag} from '../../html-parsing/angular';
1312
import {ExternalResource} from '../../tslint/component-file';
1413
import {ComponentWalker} from '../../tslint/component-walker';
1514
import {findAllSubstringIndices} from '../../typescript/literal';
@@ -50,35 +49,6 @@ export class Walker extends ComponentWalker {
5049
});
5150
});
5251

53-
findOutputsOnElementWithTag(content, 'selectionChange', ['mat-list-option']).forEach(offset => {
54-
failures.push({
55-
start: node.getStart() + offset,
56-
end: node.getStart() + offset + 'selectionChange'.length,
57-
message: `Found deprecated @Output() "${red('selectionChange')}" on ` +
58-
`"${bold('mat-list-option')}". Use "${green('selectionChange')}" on ` +
59-
`"${bold('mat-selection-list')}" instead.`
60-
});
61-
});
62-
63-
findOutputsOnElementWithTag(content, 'selectedChanged', ['mat-datepicker']).forEach(offset => {
64-
failures.push({
65-
start: node.getStart() + offset,
66-
end: node.getStart() + offset + 'selectionChange'.length,
67-
message: `Found deprecated @Output() "${red('selectedChanged')}" on ` +
68-
`"${bold('mat-datepicker')}". Use "${green('dateChange')}" or ` +
69-
`"${green('dateInput')}" on "${bold('<input [matDatepicker]>')}" instead.`
70-
});
71-
});
72-
73-
findInputsOnElementWithTag(content, 'selected', ['mat-button-toggle-group']).forEach(offset => {
74-
failures.push({
75-
start: node.getStart() + offset,
76-
end: node.getStart() + offset + 'selected'.length,
77-
message: `Found deprecated @Input() "${red('selected')}" on ` +
78-
`"${bold('mat-radio-button-group')}". Use "${green('value')}" instead.`
79-
});
80-
});
81-
8252
return failures;
8353
}
8454
}

src/cdk/schematics/ng-update/upgrade-rules/tslint-config.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ export interface UpgradeTSLintConfig {
2121
/** Type for the configuration list of upgrade rules. */
2222
export type UpgradeRules = (string | (string | TargetVersion)[])[];
2323

24-
/** List of rules that need to be enabled when running the TSLint fix task. */
25-
const upgradeRules: UpgradeRules = [
24+
/**
25+
* List of upgrade rules that will be always enabled because the upgrade data for these rules
26+
* can be swapped out dynamically.
27+
*
28+
* Rules which are specific to CDK breaking changes and are not based on the upgrade data,
29+
* shouldn't be listed here because those cannot be disabled if Angular Material runs its
30+
* update schematic using these base rules.
31+
*/
32+
const baseUpgradeRules: UpgradeRules = [
2633
// Attribute selector update rules.
2734
'attribute-selectors-string-literal',
2835
'attribute-selectors-stylesheet',
2936
'attribute-selectors-template',
3037

38+
// Class inheritance
39+
'class-inheritance-check',
40+
3141
// Class name update rules
3242
'class-names-identifier',
33-
'class-names-identifier-misc',
3443

3544
// CSS selectors update rules
3645
'css-selectors-string-literal',
@@ -51,19 +60,10 @@ const upgradeRules: UpgradeRules = [
5160

5261
// Property name update rules
5362
'property-names-access',
54-
'property-names-misc',
5563

56-
// Class inheritance
57-
'class-inheritance-check',
58-
'class-inheritance-misc',
59-
60-
// signature checks
64+
// Signature and method call checks
6165
'constructor-signature-check',
6266
'method-calls-check',
63-
64-
// Additional misc rules.
65-
'check-import-misc',
66-
'check-template-misc',
6767
];
6868

6969
/** List of absolute paths that refer to directories that contain the upgrade rules. */
@@ -80,7 +80,7 @@ const ruleDirectories = globSync('./**/', {cwd: __dirname, absolute: true});
8080
* specify additional external stylesheets which are not referenced by Angular.
8181
*/
8282
export function createTslintConfig(target: TargetVersion, config: UpgradeTSLintConfig) {
83-
const configuredRules = upgradeRules.concat(config.extraUpgradeRules || []);
83+
const configuredRules = baseUpgradeRules.concat(config.extraUpgradeRules || []);
8484
const configuredRuleDirs = ruleDirectories.concat(config.extraRuleDirectories || []);
8585

8686
const enabledRules = configuredRules.reduce((result, data) => {

src/lib/schematics/ng-update/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ import {materialUpgradeData} from './upgrade-data';
1313

1414
/** List of additional upgrade rules for Angular Material. */
1515
const upgradeRules = [
16+
// Misc check rules
17+
'check-class-inheritance-misc',
18+
'check-class-names-misc',
19+
'check-imports-misc',
20+
'check-property-names-misc',
21+
'check-template-misc',
22+
1623
// Ripple misc V7
1724
['ripple-speed-factor-assignment', TargetVersion.V7],
1825
['ripple-speed-factor-template', TargetVersion.V7],
1926
];
2027

2128
/** List of absolute paths that refer to directories that contain the Material upgrade rules. */
22-
const ruleDirectories = globSync('rules/**/', {cwd: __dirname, absolute: true});
29+
const ruleDirectories = globSync('upgrade-rules/**/', {cwd: __dirname, absolute: true});
2330

2431
/** TSLint upgrade configuration that will be passed to the CDK ng-update rule. */
2532
const tslintUpgradeConfig: UpgradeTSLintConfig = {

src/lib/schematics/ng-update/test-cases/misc/method-call-checks.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ describe('v6 method call checks', () => {
77
'method-call-checks': resolveBazelDataFile(`misc/method-call-checks_input.ts`)
88
});
99

10-
// TODO: move to CDK
10+
// TODO(devversion): Move this test case to the CDK and assert that this failure has been
11+
// reported by TSLint.
1112
expect(logOutput)
1213
.not.toMatch(/\[15,.*Found call to "FocusMonitor\.monitor".*renderer.*has been removed/);
1314
expect(logOutput)

src/cdk/schematics/ng-update/upgrade-rules/class-inheritance/classInheritanceMiscRule.ts renamed to src/lib/schematics/ng-update/upgrade-rules/misc-checks/checkClassInheritanceMiscRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import {bold, green} from 'chalk';
1010
import {ProgramAwareRuleWalker, RuleFailure, Rules} from 'tslint';
1111
import * as ts from 'typescript';
12-
import {determineBaseTypes} from '../../typescript/base-types';
12+
import {determineBaseTypes} from '@angular/cdk/schematics';
1313

1414
/**
15-
* Rule that checks for classes that extend Angular Material or CDK classes which have
16-
* changed their API.
15+
* Rule that checks for classes that extend Angular Material classes which have changed
16+
* their API.
1717
*/
1818
export class Rule extends Rules.TypedRule {
1919
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {

src/cdk/schematics/ng-update/upgrade-rules/misc-checks/checkImportMiscRule.ts renamed to src/lib/schematics/ng-update/upgrade-rules/misc-checks/checkImportsMiscRule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {isMaterialImportDeclaration} from '@angular/cdk/schematics';
910
import {red} from 'chalk';
1011
import {ProgramAwareRuleWalker, RuleFailure, Rules} from 'tslint';
1112
import * as ts from 'typescript';
12-
import {isMaterialImportDeclaration} from '../../material/typescript-specifiers';
1313

1414
/**
1515
* Rule that detects import declarations that refer to outdated identifiers from Angular Material
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC 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+
9+
import {
10+
ComponentWalker,
11+
ExternalResource,
12+
findInputsOnElementWithTag,
13+
findOutputsOnElementWithTag,
14+
} from '@angular/cdk/schematics';
15+
import {bold, green, red} from 'chalk';
16+
import {RuleFailure, Rules} from 'tslint';
17+
import * as ts from 'typescript';
18+
19+
/**
20+
* Rule that walks through every inline or external template and reports if there are outdated
21+
* usages of the Angular Material API that needs to be updated manually.
22+
*/
23+
export class Rule extends Rules.AbstractRule {
24+
apply(sourceFile: ts.SourceFile): RuleFailure[] {
25+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
26+
}
27+
}
28+
29+
export class Walker extends ComponentWalker {
30+
31+
visitInlineTemplate(node: ts.StringLiteralLike) {
32+
this._createFailuresForContent(node, node.getText()).forEach(data => {
33+
this.addFailureFromStartToEnd(data.start, data.end, data.message);
34+
});
35+
}
36+
37+
visitExternalTemplate(node: ExternalResource) {
38+
this._createFailuresForContent(node, node.getText()).forEach(data => {
39+
this.addExternalFailureFromStartToEnd(node, data.start, data.end, data.message);
40+
});
41+
}
42+
43+
private _createFailuresForContent(node: ts.Node, content: string) {
44+
const failures: {message: string, start: number, end: number}[] = [];
45+
46+
findOutputsOnElementWithTag(content, 'selectionChange', ['mat-list-option']).forEach(offset => {
47+
failures.push({
48+
start: node.getStart() + offset,
49+
end: node.getStart() + offset + 'selectionChange'.length,
50+
message: `Found deprecated @Output() "${red('selectionChange')}" on ` +
51+
`"${bold('mat-list-option')}". Use "${green('selectionChange')}" on ` +
52+
`"${bold('mat-selection-list')}" instead.`
53+
});
54+
});
55+
56+
findOutputsOnElementWithTag(content, 'selectedChanged', ['mat-datepicker']).forEach(offset => {
57+
failures.push({
58+
start: node.getStart() + offset,
59+
end: node.getStart() + offset + 'selectionChange'.length,
60+
message: `Found deprecated @Output() "${red('selectedChanged')}" on ` +
61+
`"${bold('mat-datepicker')}". Use "${green('dateChange')}" or ` +
62+
`"${green('dateInput')}" on "${bold('<input [matDatepicker]>')}" instead.`
63+
});
64+
});
65+
66+
findInputsOnElementWithTag(content, 'selected', ['mat-button-toggle-group']).forEach(offset => {
67+
failures.push({
68+
start: node.getStart() + offset,
69+
end: node.getStart() + offset + 'selected'.length,
70+
message: `Found deprecated @Input() "${red('selected')}" on ` +
71+
`"${bold('mat-radio-button-group')}". Use "${green('value')}" instead.`
72+
});
73+
});
74+
75+
return failures;
76+
}
77+
}

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"exclude": [
136136
// Exclude schematic template files and test cases that can't be linted.
137137
"src/lib/schematics/**/files/**/*",
138-
"src/lib/schematics/update/test-cases/**/*"
138+
"src/lib/schematics/ng-update/test-cases/**/*"
139139
]
140140
}
141141
}

0 commit comments

Comments
 (0)