Skip to content

Commit caa74ff

Browse files
authored
fix(eslint-plugin): support inject for use-consistent-global-store-name rule (#3983)
1 parent f371d9e commit caa74ff

File tree

2 files changed

+158
-5
lines changed

2 files changed

+158
-5
lines changed

modules/eslint-plugin/spec/rules/use-consistent-global-store-name.spec.ts

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
1414
type Options = readonly ESLintUtils.InferOptionsTypeFromRule<typeof rule>[0][];
1515
type RunTests = TSESLint.RunTests<MessageIds, Options>;
1616

17-
const valid: () => RunTests['valid'] = () => [
17+
const validConstructor: () => RunTests['valid'] = () => [
1818
`
1919
class Ok {}`,
2020
`
@@ -34,7 +34,29 @@ class Ok2 {
3434
},
3535
];
3636

37-
const invalid: () => RunTests['invalid'] = () => [
37+
const validInject: () => RunTests['valid'] = () => [
38+
`
39+
class Ok3 {}`,
40+
`
41+
import { Store } from '@ngrx/store'
42+
import { inject } from '@angular/core'
43+
44+
class Ok4 {
45+
readonly store = inject(Store)
46+
}`,
47+
{
48+
code: `
49+
import { Store } from '@ngrx/store'
50+
import { inject } from '@angular/core'
51+
52+
class Ok5 {
53+
readonly customName = inject(Store)
54+
}`,
55+
options: ['customName'],
56+
},
57+
];
58+
59+
const invalidConstructor: () => RunTests['invalid'] = () => [
3860
fromFixture(
3961
`
4062
import { Store } from '@ngrx/store'
@@ -151,7 +173,138 @@ class NotOk3 {
151173
),
152174
];
153175

176+
const invalidInject: () => RunTests['invalid'] = () => [
177+
fromFixture(
178+
`
179+
import { Store } from '@ngrx/store'
180+
import { inject } from '@angular/core'
181+
182+
class NotOk4 {
183+
readonly somethingElse$: Store = inject(Store)
184+
~~~~~~~~~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest]
185+
}`,
186+
{
187+
suggestions: [
188+
{
189+
messageId: useConsistentGlobalStoreNameSuggest,
190+
data: {
191+
storeName: 'store',
192+
},
193+
output: `
194+
import { Store } from '@ngrx/store'
195+
import { inject } from '@angular/core'
196+
197+
class NotOk4 {
198+
readonly store: Store = inject(Store)
199+
}`,
200+
},
201+
],
202+
}
203+
),
204+
fromFixture(
205+
`
206+
import { Store } from '@ngrx/store'
207+
import { inject } from '@angular/core'
208+
209+
class NotOk5 {
210+
private readonly store1 = inject(Store)
211+
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest]
212+
private readonly store = inject(Store)
213+
}`,
214+
{
215+
suggestions: [
216+
{
217+
messageId: useConsistentGlobalStoreNameSuggest,
218+
data: {
219+
storeName: 'store',
220+
},
221+
output: `
222+
import { Store } from '@ngrx/store'
223+
import { inject } from '@angular/core'
224+
225+
class NotOk5 {
226+
private readonly store = inject(Store)
227+
private readonly store = inject(Store)
228+
}`,
229+
},
230+
],
231+
}
232+
),
233+
fromFixture(
234+
`
235+
import { Store } from '@ngrx/store'
236+
import { inject } from '@angular/core'
237+
238+
class NotOk6 {
239+
private readonly store1 = inject(Store)
240+
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest 0]
241+
private readonly store2 = inject(Store)
242+
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest 1]
243+
}`,
244+
{
245+
suggestions: [
246+
{
247+
messageId: useConsistentGlobalStoreNameSuggest,
248+
data: {
249+
storeName: 'store',
250+
},
251+
output: `
252+
import { Store } from '@ngrx/store'
253+
import { inject } from '@angular/core'
254+
255+
class NotOk6 {
256+
private readonly store = inject(Store)
257+
private readonly store2 = inject(Store)
258+
}`,
259+
},
260+
{
261+
messageId: useConsistentGlobalStoreNameSuggest,
262+
data: {
263+
storeName: 'store',
264+
},
265+
output: `
266+
import { Store } from '@ngrx/store'
267+
import { inject } from '@angular/core'
268+
269+
class NotOk6 {
270+
private readonly store1 = inject(Store)
271+
private readonly store = inject(Store)
272+
}`,
273+
},
274+
],
275+
}
276+
),
277+
fromFixture(
278+
`
279+
import { Store } from '@ngrx/store'
280+
import { inject } from '@angular/core'
281+
282+
class NotOk7 {
283+
private store = inject(Store)
284+
~~~~~ [${useConsistentGlobalStoreName} { "storeName": "customName" } suggest]
285+
}`,
286+
{
287+
options: ['customName'],
288+
suggestions: [
289+
{
290+
messageId: useConsistentGlobalStoreNameSuggest,
291+
data: {
292+
storeName: 'customName',
293+
},
294+
output: `
295+
import { Store } from '@ngrx/store'
296+
import { inject } from '@angular/core'
297+
298+
class NotOk7 {
299+
private customName = inject(Store)
300+
}`,
301+
},
302+
],
303+
}
304+
),
305+
];
306+
154307
ruleTester().run(path.parse(__filename).name, rule, {
155-
valid: valid(),
156-
invalid: invalid(),
308+
valid: [...validConstructor(), ...validInject()],
309+
invalid: [...invalidConstructor(), ...invalidInject()],
157310
});

modules/eslint-plugin/src/rules/store/use-consistent-global-store-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default createRule<Options, MessageIds>({
6262
data,
6363
fix: (fixer) =>
6464
fixer.replaceTextRange(
65-
[range[0], typeAnnotation.range[0]],
65+
[range[0], typeAnnotation?.range[0] ?? range[1]],
6666
storeName
6767
),
6868
},

0 commit comments

Comments
 (0)