1
+ import * as Lint from 'tslint/lib/lint' ;
2
+ import * as ts from 'typescript' ;
3
+
4
+ const getDecoratorName = ( decorator : ts . Decorator ) => {
5
+ let baseExpr = < any > decorator . expression || { } ;
6
+ let expr = baseExpr . expression || { } ;
7
+ return expr . text ;
8
+ } ;
9
+
10
+ const getDecoratorStringArgs = ( decorator : ts . Decorator ) => {
11
+ let baseExpr = < any > decorator . expression || { } ;
12
+ let expr = baseExpr . expression || { } ;
13
+ let args = baseExpr . arguments || [ ] ;
14
+ return args . map ( a => ( a . kind === ts . SyntaxKind . StringLiteral ) ? a . text : null ) ;
15
+ } ;
16
+
17
+ export class Ng2Walker extends Lint . RuleWalker {
18
+ visitClassDeclaration ( declaration : ts . ClassDeclaration ) {
19
+ ( declaration . decorators || [ ] ) . forEach ( this . visitClassDecorator . bind ( this ) ) ;
20
+ super . visitClassDeclaration ( declaration ) ;
21
+ }
22
+ visitMethodDeclaration ( method : ts . MethodDeclaration ) {
23
+ ( method . decorators || [ ] ) . forEach ( this . visitMethodDecorator . bind ( this ) ) ;
24
+ super . visitMethodDeclaration ( method ) ;
25
+ }
26
+ protected visitMethodDecorator ( decorator : ts . Decorator ) {
27
+ let name = getDecoratorName ( decorator ) ;
28
+ if ( name === 'HostListener' ) {
29
+ this . visitNg2HostListener ( < ts . MethodDeclaration > decorator . parent , decorator , getDecoratorStringArgs ( decorator ) ) ;
30
+ }
31
+ }
32
+ visitPropertyDeclaration ( prop : ts . PropertyDeclaration ) {
33
+ ( prop . decorators || [ ] ) . forEach ( this . visitPropertyDecorator . bind ( this ) ) ;
34
+ super . visitPropertyDeclaration ( prop ) ;
35
+ }
36
+ protected visitPropertyDecorator ( decorator : ts . Decorator ) {
37
+ let name = getDecoratorName ( decorator ) ;
38
+ switch ( name ) {
39
+ case 'Input' :
40
+ this . visitNg2Input ( < ts . PropertyDeclaration > decorator . parent , decorator , getDecoratorStringArgs ( decorator ) ) ;
41
+ break ;
42
+ case 'Output' :
43
+ this . visitNg2Output ( < ts . PropertyDeclaration > decorator . parent , decorator , getDecoratorStringArgs ( decorator ) ) ;
44
+ break ;
45
+ case 'HostBinding' :
46
+ this . visitNg2HostBinding ( < ts . PropertyDeclaration > decorator . parent , decorator , getDecoratorStringArgs ( decorator ) ) ;
47
+ break ;
48
+ }
49
+ }
50
+ protected visitClassDecorator ( decorator : ts . Decorator ) {
51
+ let name = getDecoratorName ( decorator ) ;
52
+ if ( name === 'Component' ) {
53
+ this . visitNg2Component ( < ts . ClassDeclaration > decorator . parent , decorator ) ;
54
+ } else if ( name === 'Directive' ) {
55
+ this . visitNg2Directive ( < ts . ClassDeclaration > decorator . parent , decorator ) ;
56
+ }
57
+ }
58
+ protected visitNg2Component ( controller : ts . ClassDeclaration , decorator : ts . Decorator ) { }
59
+ protected visitNg2Directive ( controller : ts . ClassDeclaration , decorator : ts . Decorator ) { }
60
+ protected visitNg2Input ( property : ts . PropertyDeclaration , input : ts . Decorator , args : string [ ] ) { }
61
+ protected visitNg2Output ( property : ts . PropertyDeclaration , output : ts . Decorator , args : string [ ] ) { }
62
+ protected visitNg2HostBinding ( property : ts . PropertyDeclaration , decorator : ts . Decorator , args : string [ ] ) { }
63
+ protected visitNg2HostListener ( method : ts . MethodDeclaration , decorator : ts . Decorator , args : string [ ] ) { }
64
+ }
0 commit comments