@@ -15,76 +15,69 @@ import {ComponentWalker} from '../tslint/component-walker';
15
15
import { findAllSubstringIndices } from '../typescript/literal' ;
16
16
17
17
/**
18
- * Rule that walks through every component decorator and updates their inline or external
19
- * templates .
18
+ * Rule that walks through every inline or external template and reports if there are outdated
19
+ * usages of the Angular Material API that need to be updated manually .
20
20
*/
21
21
export class Rule extends Rules . AbstractRule {
22
22
apply ( sourceFile : ts . SourceFile ) : RuleFailure [ ] {
23
- return this . applyWithWalker ( new CheckTemplateMiscWalker ( sourceFile , this . getOptions ( ) ) ) ;
23
+ return this . applyWithWalker ( new Walker ( sourceFile , this . getOptions ( ) ) ) ;
24
24
}
25
25
}
26
26
27
- export class CheckTemplateMiscWalker extends ComponentWalker {
27
+ export class Walker extends ComponentWalker {
28
+
28
29
visitInlineTemplate ( template : ts . StringLiteral ) {
29
- this . checkTemplate ( template . getText ( ) ) . forEach ( failure => {
30
- const ruleFailure = new RuleFailure ( template . getSourceFile ( ) , failure . start , failure . end ,
31
- failure . message , this . getRuleName ( ) ) ;
32
- this . addFailure ( ruleFailure ) ;
33
- } ) ;
30
+ this . _createFailuresForContent ( template , template . getText ( ) )
31
+ . forEach ( data => this . addFailureFromStartToEnd ( data . start , data . end , data . message ) ) ;
34
32
}
35
33
36
34
visitExternalTemplate ( template : ExternalResource ) {
37
- this . checkTemplate ( template . getFullText ( ) ) . forEach ( failure => {
38
- const ruleFailure = new RuleFailure ( template , failure . start + 1 , failure . end + 1 ,
39
- failure . message , this . getRuleName ( ) ) ;
40
- this . addFailure ( ruleFailure ) ;
35
+ this . _createFailuresForContent ( template , template . getFullText ( ) ) . forEach ( data => {
36
+ this . addFailure ( new RuleFailure (
37
+ template , data . start , data . end , data . message , this . getRuleName ( ) ) ) ;
41
38
} ) ;
42
39
}
43
40
44
- /**
45
- * Replaces the outdated name in the template with the new one and returns an updated template.
46
- */
47
- private checkTemplate ( templateContent : string ) :
48
- { start : number , end : number , message : string } [ ] {
49
- let failures : { message : string , start : number , end : number } [ ] = [ ] ;
41
+ private _createFailuresForContent ( node : ts . Node , content : string ) {
42
+ const failures : { message : string , start : number , end : number } [ ] = [ ] ;
50
43
51
- failures = failures . concat (
52
- findAllSubstringIndices ( templateContent , 'cdk-focus-trap' ) . map ( offset => ( {
53
- start : offset ,
54
- end : offset + 'cdk-focus-trap' . length ,
55
- message : `Found deprecated element selector "${ red ( 'cdk-focus-trap' ) } " which has been` +
56
- ` changed to an attribute selector "${ green ( '[cdkTrapFocus]' ) } "`
57
- } ) )
58
- ) ;
44
+ findAllSubstringIndices ( content , 'cdk-focus-trap' ) . forEach ( offset => {
45
+ failures . push ( {
46
+ start : node . getStart ( ) + offset ,
47
+ end : node . getStart ( ) + offset + 'cdk-focus-trap' . length ,
48
+ message : `Found deprecated element selector "${ red ( 'cdk-focus-trap' ) } " which has been ` +
49
+ `changed to an attribute selector "${ green ( '[cdkTrapFocus]' ) } ". `
50
+ } ) ;
51
+ } ) ;
59
52
60
- failures = failures . concat (
61
- findOutputsOnElementWithTag ( templateContent , 'selectionChange' , [ 'mat-list-option' ] )
62
- . map ( offset => ( {
63
- start : offset ,
64
- end : offset + 'selectionChange' . length ,
65
- message : `Found deprecated @Output() "${ red ( 'selectionChange' ) } " on` +
66
- ` "${ bold ( 'mat-list-option ' ) } ". Use " ${ green ( 'selectionChange' ) } " on` +
67
- ` " ${ bold ( 'mat-selection-list' ) } " instead`
68
- } ) ) ) ;
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
+ } ) ;
69
62
70
- failures = failures . concat (
71
- findOutputsOnElementWithTag ( templateContent , 'selectedChanged' , [ 'mat-datepicker' ] )
72
- . map ( offset => ( {
73
- start : offset ,
74
- end : offset + 'selectionChange' . length ,
75
- message : `Found deprecated @Output() "${ red ( 'selectedChanged ') } " on ` +
76
- ` "${ bold ( 'mat-datepicker ') } ". Use "${ green ( 'dateChange ') } " or` +
77
- ` " ${ green ( 'dateInput' ) } " on " ${ bold ( '<input [matDatepicker]>' ) } " instead`
78
- } ) ) ) ;
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
+ } ) ;
79
72
80
- failures = failures . concat (
81
- findInputsOnElementWithTag ( templateContent , 'selected' , [ 'mat-button-toggle-group' ] )
82
- . map ( offset => ( {
83
- start : offset ,
84
- end : offset + 'selected' . length ,
85
- message : `Found deprecated @Input() "${ red ( 'selected ') } " on` +
86
- ` " ${ bold ( 'mat-radio-button-group' ) } ". Use " ${ green ( 'value' ) } " instead`
87
- } ) ) ) ;
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
+ } ) ;
88
81
89
82
return failures ;
90
83
}
0 commit comments