|
1 |
| -import { assertSuccess, assertAnnotated } from './testHelper'; |
| 1 | +import { getFailureMessage, Rule } from '../src/noInputPrefixRule'; |
| 2 | +import { assertAnnotated, assertSuccess } from './testHelper'; |
2 | 3 |
|
3 |
| -describe('no-input-prefix', () => { |
4 |
| - describe('invalid directive input property', () => { |
5 |
| - it('should fail, when a component input property is named with is prefix', () => { |
6 |
| - const source = ` |
7 |
| - @Component() |
8 |
| - class ButtonComponent { |
9 |
| - @Input() isDisabled: boolean; |
10 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
11 |
| - } |
12 |
| - `; |
13 |
| - assertAnnotated({ |
14 |
| - ruleName: 'no-input-prefix', |
15 |
| - options: ['is'], |
16 |
| - message: 'In the class "ButtonComponent", the input property "isDisabled" should not be prefixed with is', |
17 |
| - source |
18 |
| - }); |
19 |
| - }); |
| 4 | +const { |
| 5 | + FAILURE_STRING, |
| 6 | + metadata: { ruleName } |
| 7 | +} = Rule; |
| 8 | +const className = 'Test'; |
| 9 | + |
| 10 | +const getFailureAnnotations = (num: number): string => { |
| 11 | + return '~'.repeat(num); |
| 12 | +}; |
| 13 | + |
| 14 | +const getComposedOptions = (prefixes: string[]): (boolean | string)[] => { |
| 15 | + return [true, ...prefixes]; |
| 16 | +}; |
20 | 17 |
|
21 |
| - it('should fail, when a directive input property is named with is prefix', () => { |
| 18 | +describe(ruleName, () => { |
| 19 | + describe('failure', () => { |
| 20 | + it('should fail when an input property is prefixed by a blacklisted prefix and blacklist is composed by one prefix', () => { |
| 21 | + const prefixes = ['is']; |
| 22 | + const propertyName = `${prefixes[0]}Disabled`; |
| 23 | + const inputExpression = `@Input() ${propertyName}: boolean;`; |
22 | 24 | const source = `
|
23 | 25 | @Directive()
|
24 |
| - class ButtonDirective { |
25 |
| - @Input() isDisabled: boolean; |
26 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 | + class ${className} { |
| 27 | + ${inputExpression} |
| 28 | + ${getFailureAnnotations(inputExpression.length)} |
27 | 29 | }
|
28 | 30 | `;
|
29 | 31 | assertAnnotated({
|
30 |
| - ruleName: 'no-input-prefix', |
31 |
| - options: ['is'], |
32 |
| - message: 'In the class "ButtonDirective", the input property "isDisabled" should not be prefixed with is', |
| 32 | + message: getFailureMessage(className, propertyName, prefixes), |
| 33 | + options: getComposedOptions(prefixes), |
| 34 | + ruleName, |
33 | 35 | source
|
34 | 36 | });
|
35 | 37 | });
|
36 | 38 |
|
37 |
| - it('should fail, when a directive input property is named with is prefix', () => { |
| 39 | + it('should fail when an input property is prefixed by a blacklisted prefix and blacklist is composed by two prefixes', () => { |
| 40 | + const prefixes = ['can', 'is']; |
| 41 | + const propertyName = `${prefixes[0]}Enable`; |
| 42 | + const inputExpression = `@Input() ${propertyName}: boolean;`; |
38 | 43 | const source = `
|
39 |
| - @Directive() |
40 |
| - class ButtonDirective { |
41 |
| - @Input() mustDisable: string; |
42 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 44 | + @Component() |
| 45 | + class ${className} { |
| 46 | + ${inputExpression} |
| 47 | + ${getFailureAnnotations(inputExpression.length)} |
43 | 48 | }
|
44 | 49 | `;
|
45 | 50 | assertAnnotated({
|
46 |
| - ruleName: 'no-input-prefix', |
47 |
| - options: ['must'], |
48 |
| - message: 'In the class "ButtonDirective", the input property "mustDisable" should not be prefixed with must', |
| 51 | + message: getFailureMessage(className, propertyName, prefixes), |
| 52 | + options: getComposedOptions(prefixes), |
| 53 | + ruleName, |
49 | 54 | source
|
50 | 55 | });
|
51 | 56 | });
|
52 | 57 |
|
53 |
| - it('should fail, when a directive input property is named with is prefix', () => { |
| 58 | + it('should fail when an input property is prefixed by a blacklisted prefix and blacklist is composed by two concurrent prefixes', () => { |
| 59 | + const prefixes = ['is', 'isc']; |
| 60 | + const propertyName = `${prefixes[1]}Hange`; |
| 61 | + const inputExpression = `@Input() ${propertyName}: boolean;`; |
54 | 62 | const source = `
|
55 |
| - @Directive() |
56 |
| - class ButtonDirective { |
57 |
| - @Input() is = true; |
58 |
| - ~~~~~~~~~~~~~~~~~~~ |
| 63 | + @Component() |
| 64 | + class ${className} { |
| 65 | + ${inputExpression} |
| 66 | + ${getFailureAnnotations(inputExpression.length)} |
59 | 67 | }
|
60 | 68 | `;
|
61 | 69 | assertAnnotated({
|
62 |
| - ruleName: 'no-input-prefix', |
63 |
| - options: ['is'], |
64 |
| - message: 'In the class "ButtonDirective", the input property "is" should not be prefixed with is', |
| 70 | + message: getFailureMessage(className, propertyName, prefixes), |
| 71 | + options: getComposedOptions(prefixes), |
| 72 | + ruleName, |
65 | 73 | source
|
66 | 74 | });
|
67 | 75 | });
|
68 | 76 |
|
69 |
| - it('should fail, when a directive input property is named with can prefix', () => { |
| 77 | + it('should fail when an input property is snakecased and contains a blacklisted prefix', () => { |
| 78 | + const prefixes = ['do']; |
| 79 | + const propertyName = `${prefixes[0]}_it`; |
| 80 | + const inputExpression = `@Input() ${propertyName}: number;`; |
70 | 81 | const source = `
|
71 | 82 | @Directive()
|
72 |
| - class ButtonDirective { |
73 |
| - @Input() canEnable = true; |
74 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 83 | + class ${className} { |
| 84 | + ${inputExpression} |
| 85 | + ${getFailureAnnotations(inputExpression.length)} |
75 | 86 | }
|
76 | 87 | `;
|
77 | 88 | assertAnnotated({
|
78 |
| - ruleName: 'no-input-prefix', |
79 |
| - options: ['can', 'is'], |
80 |
| - message: 'In the class "ButtonDirective", the input property "canEnable" should not be prefixed with can, is', |
| 89 | + message: getFailureMessage(className, propertyName, prefixes), |
| 90 | + options: getComposedOptions(prefixes), |
| 91 | + ruleName, |
81 | 92 | source
|
82 | 93 | });
|
83 | 94 | });
|
84 | 95 | });
|
85 | 96 |
|
86 |
| - describe('valid directive input property', () => { |
87 |
| - it('should succeed, when a directive input property is properly named', () => { |
88 |
| - const source = ` |
89 |
| - @Directive() |
90 |
| - class ButtonComponent { |
91 |
| - @Input() disabled = true; |
92 |
| - } |
93 |
| - `; |
94 |
| - assertSuccess('no-input-prefix', source); |
95 |
| - }); |
96 |
| - |
97 |
| - it('should succeed, when a directive input property is properly named', () => { |
| 97 | + describe('success', () => { |
| 98 | + it('should succeed when an input property is not prefixed', () => { |
98 | 99 | const source = `
|
99 | 100 | @Directive()
|
100 |
| - class ButtonComponent { |
101 |
| - @Input() disabled = "yes"; |
| 101 | + class ${className} { |
| 102 | + @Input() mustmust = true; |
102 | 103 | }
|
103 | 104 | `;
|
104 |
| - assertSuccess('no-input-prefix', source); |
| 105 | + assertSuccess(ruleName, source, getComposedOptions(['must'])); |
105 | 106 | });
|
106 | 107 |
|
107 |
| - it('should succeed, when a component input property is properly named with is', () => { |
| 108 | + it('should succeed when multiple input properties are prefixed by something not present in the blacklist', () => { |
108 | 109 | const source = `
|
109 | 110 | @Component()
|
110 |
| - class ButtonComponent { |
111 |
| - @Input() isometric: boolean; |
| 111 | + class ${className} { |
| 112 | + @Input() cana: string; |
| 113 | + @Input() disabledThing: boolean; |
| 114 | + @Input() isFoo = 'yes'; |
| 115 | + @Input() shoulddoit: boolean; |
112 | 116 | }
|
113 | 117 | `;
|
114 |
| - assertSuccess('no-input-prefix', source); |
| 118 | + assertSuccess(ruleName, source, getComposedOptions(['can', 'should', 'dis', 'disable'])); |
115 | 119 | });
|
116 | 120 | });
|
117 | 121 | });
|
0 commit comments