Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): support inject for use-consistent-global-store-name rule #3983

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]],
timdeschryver marked this conversation as resolved.
Show resolved Hide resolved
storeName
),
},
Expand Down