Skip to content

Commit 774f90d

Browse files
committed
feat: tweak rule error message
1 parent 8eca2fd commit 774f90d

File tree

2 files changed

+192
-4
lines changed

2 files changed

+192
-4
lines changed

src/rules/destroy-service-provider.test.ts

Lines changed: 190 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,47 @@ tester.run("destroy-service-provider", rule, {
2323
},
2424
{
2525
code: `
26-
@Component()
26+
@Component({
27+
selector: 'my-orgs',
28+
templateUrl: './welcome.component.html',
29+
styleUrls: ['./welcome.component.scss'],
30+
providers: [DestroyService]
31+
})
2732
export class WelcomeComponent implements OnInit {
2833
constructor(
29-
private destroy$: DestroyService,
34+
@Inject(DestroyService) private destroy$: DestroyService,
3035
) {}
3136
}`,
3237
},
3338
{
3439
code: `
35-
@Component({})
40+
@Component({
41+
selector: 'my-orgs',
42+
templateUrl: './welcome.component.html',
43+
styleUrls: ['./welcome.component.scss'],
44+
providers: [DestroyService]
45+
})
3646
export class WelcomeComponent implements OnInit {
3747
constructor(
3848
private destroy$: DestroyService,
3949
) {}
4050
}`,
4151
},
52+
{
53+
code: `
54+
@Component({
55+
selector: 'my-orgs',
56+
templateUrl: './welcome.component.html',
57+
styleUrls: ['./welcome.component.scss'],
58+
providers: [Destroy]
59+
})
60+
export class WelcomeComponent implements OnInit {
61+
constructor(
62+
private destroy$: Destroy,
63+
) {}
64+
}`,
65+
options: [{ destroyServiceName: "Destroy" }],
66+
},
4267
{
4368
code: `
4469
@Directive({
@@ -51,6 +76,53 @@ tester.run("destroy-service-provider", rule, {
5176
) {}
5277
}`,
5378
},
79+
{
80+
code: `
81+
@Directive({
82+
selector: 'my-directive',
83+
providers: [Destroy]
84+
})
85+
export class MyDirective implements OnInit {
86+
constructor(
87+
private destroy$: Destroy,
88+
) {}
89+
}`,
90+
options: [{ destroyServiceName: "Destroy" }],
91+
},
92+
// developer forgot to config custom DestroyService name to `Destroy`
93+
// so the rule doesn't recognize `Destroy` is a DestroyService
94+
{
95+
code: `
96+
@Directive({
97+
selector: 'my-directive',
98+
providers: []
99+
})
100+
export class MyDirective implements OnInit {
101+
constructor(
102+
private destroy$: Destroy,
103+
) {}
104+
}`,
105+
},
106+
// test cases below will verify when syntax of Component/Directive is invalid
107+
// the rule won't check until Component/Directive syntax error is fixed
108+
{
109+
code: `
110+
@Component()
111+
export class WelcomeComponent implements OnInit {
112+
constructor(
113+
private destroy$: DestroyService,
114+
) {}
115+
}`,
116+
},
117+
{
118+
code: `
119+
@Component({})
120+
export class WelcomeComponent implements OnInit {
121+
constructor(
122+
private destroy$: DestroyService,
123+
) {}
124+
}`,
125+
},
54126
{
55127
code: `
56128
@Directive()
@@ -89,6 +161,97 @@ tester.run("destroy-service-provider", rule, {
89161
messageId: "missing",
90162
data: {
91163
className: "Component",
164+
destroyServiceName: "DestroyService",
165+
},
166+
},
167+
],
168+
},
169+
{
170+
code: `
171+
@Component({
172+
selector: 'my-orgs',
173+
templateUrl: './welcome.component.html',
174+
styleUrls: ['./welcome.component.scss'],
175+
})
176+
export class WelcomeComponent implements OnInit {
177+
constructor(
178+
private destroy$: DestroyService,
179+
) {}
180+
}`,
181+
errors: [
182+
{
183+
messageId: "missing",
184+
data: {
185+
className: "Component",
186+
destroyServiceName: "DestroyService",
187+
},
188+
},
189+
],
190+
},
191+
{
192+
code: `
193+
@Component({
194+
selector: 'my-orgs',
195+
templateUrl: './welcome.component.html',
196+
styleUrls: ['./welcome.component.scss'],
197+
providers: []
198+
})
199+
export class WelcomeComponent implements OnInit {
200+
constructor(
201+
private destroy$: Destroy,
202+
) {}
203+
}`,
204+
options: [{ destroyServiceName: "Destroy" }],
205+
errors: [
206+
{
207+
messageId: "missing",
208+
data: {
209+
className: "Component",
210+
destroyServiceName: "Destroy",
211+
},
212+
},
213+
],
214+
},
215+
{
216+
code: `
217+
@Component({
218+
selector: 'my-orgs',
219+
templateUrl: './welcome.component.html',
220+
styleUrls: ['./welcome.component.scss'],
221+
})
222+
export class WelcomeComponent implements OnInit {
223+
constructor(
224+
private destroy$: Destroy,
225+
) {}
226+
}`,
227+
options: [{ destroyServiceName: "Destroy" }],
228+
errors: [
229+
{
230+
messageId: "missing",
231+
data: {
232+
className: "Component",
233+
destroyServiceName: "Destroy",
234+
},
235+
},
236+
],
237+
},
238+
{
239+
code: `
240+
@Directive({
241+
selector: 'my-directive',
242+
providers: [],
243+
})
244+
export class MyDirective implements OnInit {
245+
constructor(
246+
private destroy$: DestroyService,
247+
) {}
248+
}`,
249+
errors: [
250+
{
251+
messageId: "missing",
252+
data: {
253+
className: "Directive",
254+
destroyServiceName: "DestroyService",
92255
},
93256
},
94257
],
@@ -108,6 +271,29 @@ tester.run("destroy-service-provider", rule, {
108271
messageId: "missing",
109272
data: {
110273
className: "Directive",
274+
destroyServiceName: "DestroyService",
275+
},
276+
},
277+
],
278+
},
279+
{
280+
code: `
281+
@Directive({
282+
selector: 'my-directive',
283+
providers: []
284+
})
285+
export class MyDirective implements OnInit {
286+
constructor(
287+
private destroy$: Destroy,
288+
) {}
289+
}`,
290+
options: [{ destroyServiceName: "Destroy" }],
291+
errors: [
292+
{
293+
messageId: "missing",
294+
data: {
295+
className: "Directive",
296+
destroyServiceName: "Destroy",
111297
},
112298
},
113299
],
@@ -128,6 +314,7 @@ tester.run("destroy-service-provider", rule, {
128314
messageId: "missing",
129315
data: {
130316
className: "Directive",
317+
destroyServiceName: "Destroy",
131318
},
132319
},
133320
],

src/rules/destroy-service-provider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const rule = createRule<Options, MessageIds>({
102102
messageId: "missing",
103103
data: {
104104
className: (nodeExpression as any).callee.name,
105+
destroyServiceName: options.destroyServiceName,
105106
},
106107
});
107108
}
@@ -119,7 +120,7 @@ export const rule = createRule<Options, MessageIds>({
119120
},
120121
messages: {
121122
missing:
122-
"Please provide DestroyService in {{className}} class providers.",
123+
"Please provide {{destroyServiceName}} in {{className}} class providers.",
123124
},
124125
schema: [
125126
{

0 commit comments

Comments
 (0)