11import * as Lint from 'tslint' ;
2- import { IOptions , Replacement } from 'tslint' ;
32import * as ts from 'typescript' ;
43
5- export const ruleName = 'ion-action-sheet-method-create-parameters-renamed' ;
6-
7- /**
8- * This rule helps with the conversion of the ActionSheetController API.
9- */
10- class ActionSheetMethodCreateParametersRenamedWalker extends Lint . RuleWalker {
11- //actionControllerVariableName = undefined;
12- foundPropertyArray = [ ] ;
13-
14- // TODO: Not sure if we need to track the name of the ActionSheetController variable.
15- // visitConstructorDeclaration(node: ts.ConstructorDeclaration) {
16- // debugger;
17- // for (let element of node.parameters) {
18- // const typeName = (element.type as any).typeName.text;
19- // if (typeName === 'ActionSheetController') {
20- // this.actionControllerVariableName = (element.name as any).text;
21- // this.tryAddFailure();
22- // break;
23- // }
24- // }
25-
26- // super.visitConstructorDeclaration(node);
27- // }
28-
29- visitCallExpression ( node : ts . CallExpression ) {
30- const expression = node . expression as any ;
31-
32- if ( expression . name && expression . name . text === 'create' ) {
33- for ( let argument of ( node . arguments [ 0 ] as any ) . properties ) {
34- const name = argument . name . text ;
35-
36- switch ( name ) {
37- case 'title' :
38- case 'subTitle' :
39- argument . parentVariableName = ( node . expression as any ) . expression . text ;
40- this . foundPropertyArray . push ( argument ) ;
41- this . tryAddFailure ( ) ;
42- break ;
43- }
44- }
45- }
46- }
4+ import { createParametersRenamedClass } from './helpers/parametersRenamed' ;
475
48- private tryAddFailure ( ) {
49- for ( let i = this . foundPropertyArray . length - 1 ; i >= 0 ; i -- ) {
50- let argument = this . foundPropertyArray [ i ] ;
51-
52- const replacementParam = argument . name . text === 'title' ? 'header' : 'subHeader' ;
53-
54- // TODO: Determine if this needs to be added in later.
55- //if (this.actionControllerVariableName && this.actionControllerVariableName === argument.parentVariableName) {
56- const errorMessage = `The ${ argument . name . text } field has been replaced by ${ replacementParam } .` ;
57-
58- const replacement = new Replacement ( argument . name . getStart ( ) , argument . name . getWidth ( ) , replacementParam ) ;
59-
60- this . addFailure ( this . createFailure ( argument . name . getStart ( ) , argument . name . getWidth ( ) , errorMessage , [ replacement ] ) ) ;
61- this . foundPropertyArray . splice ( i , 1 ) ;
6+ export const ruleName = 'ion-action-sheet-method-create-parameters-renamed' ;
627
63- //}
64- }
65- }
66- }
8+ const parameterMap = new Map ( [ [ 'title' , 'header' ] , [ 'subTitle' , 'subHeader' ] ] ) ;
9+ const Walker = createParametersRenamedClass ( 'create' , 'ActionSheetController' , parameterMap ) ;
6710
6811export class Rule extends Lint . Rules . AbstractRule {
6912 public static metadata : Lint . IRuleMetadata = {
@@ -77,6 +20,6 @@ export class Rule extends Lint.Rules.AbstractRule {
7720 } ;
7821
7922 public apply ( sourceFile : ts . SourceFile ) : Lint . RuleFailure [ ] {
80- return this . applyWithWalker ( new ActionSheetMethodCreateParametersRenamedWalker ( sourceFile , this . getOptions ( ) ) ) ;
23+ return this . applyWithWalker ( new Walker ( sourceFile , this . getOptions ( ) ) ) ;
8124 }
8225}
0 commit comments