Skip to content

Commit

Permalink
fix(eslint-plugin): support inject for use-consistent-global-store-na…
Browse files Browse the repository at this point in the history
…me rule (#3983)
  • Loading branch information
suke authored Aug 3, 2023
1 parent f371d9e commit caa74ff
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
type Options = readonly ESLintUtils.InferOptionsTypeFromRule<typeof rule>[0][];
type RunTests = TSESLint.RunTests<MessageIds, Options>;

const valid: () => RunTests['valid'] = () => [
const validConstructor: () => RunTests['valid'] = () => [
`
class Ok {}`,
`
Expand All @@ -34,7 +34,29 @@ class Ok2 {
},
];

const invalid: () => RunTests['invalid'] = () => [
const validInject: () => RunTests['valid'] = () => [
`
class Ok3 {}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok4 {
readonly store = inject(Store)
}`,
{
code: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok5 {
readonly customName = inject(Store)
}`,
options: ['customName'],
},
];

const invalidConstructor: () => RunTests['invalid'] = () => [
fromFixture(
`
import { Store } from '@ngrx/store'
Expand Down Expand Up @@ -151,7 +173,138 @@ class NotOk3 {
),
];

const invalidInject: () => RunTests['invalid'] = () => [
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk4 {
readonly somethingElse$: Store = inject(Store)
~~~~~~~~~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest]
}`,
{
suggestions: [
{
messageId: useConsistentGlobalStoreNameSuggest,
data: {
storeName: 'store',
},
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk4 {
readonly store: Store = inject(Store)
}`,
},
],
}
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk5 {
private readonly store1 = inject(Store)
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest]
private readonly store = inject(Store)
}`,
{
suggestions: [
{
messageId: useConsistentGlobalStoreNameSuggest,
data: {
storeName: 'store',
},
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk5 {
private readonly store = inject(Store)
private readonly store = inject(Store)
}`,
},
],
}
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk6 {
private readonly store1 = inject(Store)
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest 0]
private readonly store2 = inject(Store)
~~~~~~ [${useConsistentGlobalStoreName} { "storeName": "store" } suggest 1]
}`,
{
suggestions: [
{
messageId: useConsistentGlobalStoreNameSuggest,
data: {
storeName: 'store',
},
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk6 {
private readonly store = inject(Store)
private readonly store2 = inject(Store)
}`,
},
{
messageId: useConsistentGlobalStoreNameSuggest,
data: {
storeName: 'store',
},
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk6 {
private readonly store1 = inject(Store)
private readonly store = inject(Store)
}`,
},
],
}
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk7 {
private store = inject(Store)
~~~~~ [${useConsistentGlobalStoreName} { "storeName": "customName" } suggest]
}`,
{
options: ['customName'],
suggestions: [
{
messageId: useConsistentGlobalStoreNameSuggest,
data: {
storeName: 'customName',
},
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk7 {
private customName = inject(Store)
}`,
},
],
}
),
];

ruleTester().run(path.parse(__filename).name, rule, {
valid: valid(),
invalid: invalid(),
valid: [...validConstructor(), ...validInject()],
invalid: [...invalidConstructor(), ...invalidInject()],
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default createRule<Options, MessageIds>({
data,
fix: (fixer) =>
fixer.replaceTextRange(
[range[0], typeAnnotation.range[0]],
[range[0], typeAnnotation?.range[0] ?? range[1]],
storeName
),
},
Expand Down

0 comments on commit caa74ff

Please sign in to comment.