diff --git a/modules/eslint-plugin/spec/rules/use-consistent-global-store-name.spec.ts b/modules/eslint-plugin/spec/rules/use-consistent-global-store-name.spec.ts index e4479135bd..56263cc29a 100644 --- a/modules/eslint-plugin/spec/rules/use-consistent-global-store-name.spec.ts +++ b/modules/eslint-plugin/spec/rules/use-consistent-global-store-name.spec.ts @@ -14,7 +14,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule; type Options = readonly ESLintUtils.InferOptionsTypeFromRule[0][]; type RunTests = TSESLint.RunTests; -const valid: () => RunTests['valid'] = () => [ +const validConstructor: () => RunTests['valid'] = () => [ ` class Ok {}`, ` @@ -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' @@ -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()], }); diff --git a/modules/eslint-plugin/src/rules/store/use-consistent-global-store-name.ts b/modules/eslint-plugin/src/rules/store/use-consistent-global-store-name.ts index 8047e22805..4028d14ca6 100644 --- a/modules/eslint-plugin/src/rules/store/use-consistent-global-store-name.ts +++ b/modules/eslint-plugin/src/rules/store/use-consistent-global-store-name.ts @@ -62,7 +62,7 @@ export default createRule({ data, fix: (fixer) => fixer.replaceTextRange( - [range[0], typeAnnotation.range[0]], + [range[0], typeAnnotation?.range[0] ?? range[1]], storeName ), },