Skip to content

Commit 39d2311

Browse files
devversionmmalerba
authored andcommitted
refactor(schematics): separate constructor checks data (#13160)
* Resolves a `TODO` for the constructor checks data. To be consistent with the other upgrade data, the constructor checks data should be separated based on the target version.
1 parent 4a96539 commit 39d2311

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

src/lib/schematics/update/material/data/constructor-checks.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,46 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {TargetVersion} from '../../index';
10+
import {VersionChanges} from '../transform-change-data';
11+
912
/**
1013
* List of class names for which the constructor signature has been changed. The new constructor
1114
* signature types don't need to be stored here because the signature will be determined
1215
* automatically through type checking.
1316
*/
14-
export const constructorChecks = [
15-
// https://github.com/angular/material2/pull/9190
16-
'NativeDateAdapter',
17-
18-
// https://github.com/angular/material2/pull/10319
19-
'MatAutocomplete',
20-
21-
// https://github.com/angular/material2/pull/10344
22-
'MatTooltip',
23-
24-
// https://github.com/angular/material2/pull/10389
25-
'MatIconRegistry',
26-
27-
// https://github.com/angular/material2/pull/9775
28-
'MatCalendar',
29-
30-
// TODO(devversion): The constructor check rule doesn't distinguish data based on the target
31-
// TODO(devversion): version. Though it would be more readable to structure the data.
32-
33-
// https://github.com/angular/material2/pull/11706
34-
'MatDrawerContent',
35-
36-
// https://github.com/angular/material2/pull/11706
37-
'MatSidenavContent',
38-
];
17+
export const constructorChecks: VersionChanges<string> = {
18+
[TargetVersion.V7]: [
19+
{
20+
pr: 'https://github.com/angular/material2/pull/11706',
21+
changes: ['MatDrawerContent'],
22+
},
23+
{
24+
pr: 'https://github.com/angular/material2/pull/11706',
25+
changes: ['MatSidenavContent']
26+
}
27+
],
28+
29+
[TargetVersion.V6]: [
30+
{
31+
pr: 'https://github.com/angular/material2/pull/9190',
32+
changes: ['NativeDateAdapter'],
33+
},
34+
{
35+
pr: 'https://github.com/angular/material2/pull/10319',
36+
changes: ['MatAutocomplete'],
37+
},
38+
{
39+
pr: 'https://github.com/angular/material2/pull/10344',
40+
changes: ['MatTooltip'],
41+
},
42+
{
43+
pr: 'https://github.com/angular/material2/pull/10389',
44+
changes: ['MatIconRegistry'],
45+
},
46+
{
47+
pr: 'https://github.com/angular/material2/pull/9775',
48+
changes: ['MatCalendar'],
49+
},
50+
]
51+
};

src/lib/schematics/update/material/transform-change-data.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ export function getChangesForTarget<T>(target: TargetVersion, data: VersionChang
3535

3636
return data[target]!.reduce((result, prData) => result.concat(prData.changes), [] as T[]);
3737
}
38+
39+
/**
40+
* Gets all changes from the specified version changes object. This is helpful in case a migration
41+
* rule does not distinguish data based on the target version, but for readability the
42+
* upgrade data is separated for each target version.
43+
*/
44+
export function getAllChanges<T>(data: VersionChanges<T>): T[] {
45+
return Object.keys(data)
46+
.map(targetVersion => getChangesForTarget(parseInt(targetVersion), data))
47+
.reduce((result, versionData) => result.concat(versionData), []);
48+
}

src/lib/schematics/update/rules/signature-check/constructorSignatureCheckRule.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {bold, green} from 'chalk';
1010
import {RuleFailure, Rules, WalkContext} from 'tslint';
1111
import * as ts from 'typescript';
1212
import {constructorChecks} from '../../material/data/constructor-checks';
13+
import {getAllChanges} from '../../material/transform-change-data';
1314

1415
/**
1516
* List of diagnostic codes that refer to pre-emit diagnostics which indicate invalid
@@ -24,6 +25,9 @@ const signatureErrorDiagnostics = [
2425
2554, 2555, 2556, 2557,
2526
];
2627

28+
/** List of classes of which the constructor signature has changed. */
29+
const signatureChangedClasses = getAllChanges(constructorChecks);
30+
2731
/**
2832
* Rule that visits every TypeScript new expression or super call and checks if the parameter
2933
* type signature is invalid and needs to be updated manually.
@@ -62,7 +66,7 @@ function visitSourceFile(context: WalkContext<null>, program: ts.Program) {
6266

6367
// TODO(devversion): Consider handling pass-through classes better.
6468
// TODO(devversion): e.g. `export class CustomCalendar extends MatCalendar {}`
65-
if (!constructorChecks.includes(className)) {
69+
if (!signatureChangedClasses.includes(className)) {
6670
continue;
6771
}
6872

0 commit comments

Comments
 (0)