-
Notifications
You must be signed in to change notification settings - Fork 234
/
preferInlineDecoratorRule.spec.ts
201 lines (187 loc) · 5.69 KB
/
preferInlineDecoratorRule.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { expect } from 'chai';
import { Replacement } from 'tslint/lib';
import { decoratorKeys, Rule } from '../src/preferInlineDecoratorRule';
import { assertFailure, assertFailures, assertSuccess, IExpectedFailure } from './testHelper';
const {
FAILURE_STRING,
metadata: { ruleName }
} = Rule;
const className = 'Test';
describe(ruleName, () => {
describe('failure', () => {
const expectedFailure: IExpectedFailure = {
endPosition: {
character: 35,
line: 3
},
message: FAILURE_STRING,
startPosition: {
character: 14,
line: 2
}
};
decoratorKeys.forEach(decoratorKey => {
describe(decoratorKey, () => {
it('should fail when a property does not start on the same line as the decoratorKey', () => {
const source = `
class ${className} {
@${decoratorKey}('childTest')
childTest: ChildTest;
}
`;
assertFailure(ruleName, source, expectedFailure);
});
it('should fail and apply proper replacements when a property does not start on the same line as the decoratorKey', () => {
const source = `
class ${className} {
@${decoratorKey}('childTest')
childTest: ChildTest;
}
`;
const failures = assertFailure(ruleName, source, expectedFailure);
const replacement = Replacement.applyFixes(source, failures.map(f => f.getFix()));
expect(replacement).to.eq(`
class ${className} {
@${decoratorKey}('childTest') childTest: ChildTest;
}
`);
});
});
});
describe('blacklist', () => {
it('should fail when a property does not start on the same line as the decoratorKey and is not present on blacklist options', () => {
const [firstDecorator, ...restDecorators] = Array.from(decoratorKeys);
const source = `
class ${className} {
@${firstDecorator}()
test = new EventEmitter<void>();
}
`;
assertFailure(
ruleName,
source,
{
endPosition: {
character: 44,
line: 3
},
message: FAILURE_STRING,
startPosition: {
character: 12,
line: 2
}
},
[true, restDecorators]
);
});
});
it('should fail when there are multiple properties that does not start on the same line as the decoratorKey', () => {
const source = `
class ${className} {
@Input('childTest')
childTest: ChildTest;
@Input('foo')
foo: Foo;
}
`;
assertFailures(ruleName, source, [
{
endPosition: {
character: 31,
line: 3
},
message: FAILURE_STRING,
startPosition: {
character: 10,
line: 2
}
},
{
endPosition: {
character: 19,
line: 5
},
message: FAILURE_STRING,
startPosition: {
character: 10,
line: 4
}
}
]);
});
});
describe('success', () => {
decoratorKeys.forEach(decoratorKey => {
describe(decoratorKey, () => {
it('should succeed when a property starts and ends on the same line as the decoratorKey', () => {
const source = `
class ${className} {
@${decoratorKey}('childTest') childTest123: ChildTest;
}
`;
assertSuccess(ruleName, source);
});
it('should succeed when a property starts on the same line as the decoratorKey and ends on another line', () => {
const source = `
class ${className} {
@${decoratorKey}('childTest') childTest123: ChildTest =
veryVeryVeryVeryVeryVeryVeryLongDefaultVariable;
}
`;
assertSuccess(ruleName, source);
});
});
});
describe('blacklist', () => {
it('should succeed when a property starts on another line and is present on blacklist options', () => {
const [firstDecorator] = Array.from(decoratorKeys);
const source = `
class ${className} {
@${firstDecorator}()
test = new EventEmitter<void>();
}
`;
assertSuccess(ruleName, source, [true, firstDecorator]);
});
});
describe('special cases', () => {
it('should succeed when getters starts on the same line as the decoratorKey and ends on another line', () => {
const source = `
class ${className} {
@Input() get test(): string {
return this._test;
}
private _test: string;
}
`;
assertSuccess(ruleName, source);
});
it('should succeed when setters starts on the same line as the decoratorKey and ends on another line', () => {
const source = `
class ${className} {
@Input() set test(value: string) {
this._test = value;
}
private _test: string;
}
`;
assertSuccess(ruleName, source);
});
it('should succeed for getters and setters on another line', () => {
const source = `
class ${className} {
@Input()
get test(): string {
return this._test;
}
set test(value: string) {
this._test = value;
}
private _test: string;
}
`;
assertSuccess(ruleName, source);
});
});
});
});