@@ -2,6 +2,7 @@ import { IRuleMetadata, RuleFailure, WalkContext } from 'tslint';
2
2
import { AbstractRule } from 'tslint/lib/rules' ;
3
3
import {
4
4
ConstructorDeclaration ,
5
+ createNodeArray ,
5
6
Decorator ,
6
7
forEachChild ,
7
8
isConstructorDeclaration ,
@@ -11,48 +12,50 @@ import {
11
12
} from 'typescript' ;
12
13
import { getDecoratorName } from './util/utils' ;
13
14
15
+ const ATTRIBUTE = 'Attribute' ;
16
+
14
17
export class Rule extends AbstractRule {
15
18
static readonly metadata : IRuleMetadata = {
16
- description : ' Disallows usage of @Attribute decorator.' ,
19
+ description : ` Disallows usage of @${ ATTRIBUTE } decorator.` ,
17
20
options : null ,
18
21
optionsDescription : 'Not configurable.' ,
19
- rationale : '@Attribute is considered bad practice. Use @Input instead.' ,
20
22
ruleName : 'no-attribute-decorator' ,
21
23
type : 'functionality' ,
22
24
typescriptOnly : true
23
25
} ;
24
26
25
- static readonly FAILURE_STRING = '@Attribute is considered bad practice. Use @Input instead.' ;
27
+ static readonly FAILURE_STRING = `@ ${ ATTRIBUTE } is considered bad practice. Use @Input instead.` ;
26
28
27
29
apply ( sourceFile : SourceFile ) : RuleFailure [ ] {
28
30
return this . applyWithFunction ( sourceFile , walk ) ;
29
31
}
30
32
}
31
33
32
- const isAttributeDecorator = ( decorator : Decorator ) : boolean => getDecoratorName ( decorator ) === 'Attribute' ;
33
-
34
- const validateConstructor = ( context : WalkContext < void > , node : ConstructorDeclaration ) : void =>
35
- node . parameters . forEach ( parameter => validateParameter ( context , parameter ) ) ;
34
+ const callbackHandler = ( walkContext : WalkContext < void > , node : Node ) : void => {
35
+ if ( isConstructorDeclaration ( node ) ) validateConstructor ( walkContext , node ) ;
36
+ } ;
36
37
37
- const validateDecorator = ( context : WalkContext < void > , decorator : Decorator ) : void => {
38
- if ( ! isAttributeDecorator ( decorator ) ) return ;
38
+ const isAttributeDecorator = ( decorator : Decorator ) : boolean => getDecoratorName ( decorator ) === ATTRIBUTE ;
39
39
40
- context . addFailureAtNode ( decorator , Rule . FAILURE_STRING ) ;
40
+ const validateConstructor = ( walkContext : WalkContext < void > , node : ConstructorDeclaration ) : void => {
41
+ node . parameters . forEach ( parameter => validateParameter ( walkContext , parameter ) ) ;
41
42
} ;
42
43
43
- const validateParameter = ( context : WalkContext < void > , parameter : ParameterDeclaration ) : void => {
44
- const { decorators } = parameter ;
44
+ const validateDecorator = ( walkContext : WalkContext < void > , decorator : Decorator ) : void => {
45
+ if ( ! isAttributeDecorator ( decorator ) ) return ;
45
46
46
- if ( ! decorators ) return ;
47
+ walkContext . addFailureAtNode ( decorator , Rule . FAILURE_STRING ) ;
48
+ } ;
47
49
48
- decorators . forEach ( decorator => validateDecorator ( context , decorator ) ) ;
50
+ const validateParameter = ( walkContext : WalkContext < void > , node : ParameterDeclaration ) : void => {
51
+ createNodeArray ( node . decorators ) . forEach ( decorator => validateDecorator ( walkContext , decorator ) ) ;
49
52
} ;
50
53
51
- const walk = ( context : WalkContext < void > ) : void => {
52
- const { sourceFile } = context ;
54
+ const walk = ( walkContext : WalkContext < void > ) : void => {
55
+ const { sourceFile } = walkContext ;
53
56
54
57
const callback = ( node : Node ) : void => {
55
- if ( isConstructorDeclaration ( node ) ) validateConstructor ( context , node ) ;
58
+ callbackHandler ( walkContext , node ) ;
56
59
57
60
forEachChild ( node , callback ) ;
58
61
} ;
0 commit comments