Skip to content

Commit

Permalink
fix(eslint): fix inject function based injection not detecting store (#…
Browse files Browse the repository at this point in the history
…3936)

Closes #3834
  • Loading branch information
Flusinerd authored Jul 3, 2023
1 parent 0ecf0d3 commit 8a5884d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
60 changes: 58 additions & 2 deletions modules/eslint-plugin/spec/rules/no-store-subscription.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ class Ok4 {
this.items$ = store.pipe(select(selectItems))
this.metrics$ = store.select(selectMetrics)
}
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok5 {
readonly items$: Observable<readonly Item[]>
readonly metrics$: Observable<Metric>
readonly store = inject(Store)
constructor() {
this.items$ = store.pipe(select(selectItems))
this.metrics$ = store.select(selectMetrics)
}
}`,
`
import { Store } from '@ngrx/store'
import { inject } from 'some-other-package'
class Ok6 {
readonly items$: Observable<readonly Item[]>
readonly metrics$: Observable<Metric>
readonly store = inject(Store)
constructor() {
store.pipe(select(selectItems)).subscribe()
}
}`,
];

Expand Down Expand Up @@ -136,7 +163,7 @@ class NotOk6 {
fromFixture(`
import { Store } from '@ngrx/store'
class NotOk6 {
class NotOk7 {
readonly items: readonly Item[]
constructor(store: Store) {
Expand All @@ -147,7 +174,7 @@ class NotOk6 {
fromFixture(`
import { Store } from '@ngrx/store'
class NotOk7 {
class NotOk8 {
readonly control = new FormControl()
constructor(store: Store) {
Expand All @@ -156,6 +183,35 @@ class NotOk7 {
~~~~~~~~~ [${messageId}]
})
}
}`),
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk9 {
readonly control = new FormControl()
store = inject(Store)
constructor() {
this.control.valueChanges.subscribe(() => {
store.pipe(select(selectItems)).subscribe()
~~~~~~~~~ [${messageId}]
})
}
}`),
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk10 {
readonly items$: Observable<readonly Item[]>
readonly metrics$: Observable<Metric>
readonly store = inject(Store)
constructor() {
store.pipe(select(selectItems)).subscribe()
~~~~~~~~~ [${messageId}]
}
}`),
];

Expand Down
26 changes: 23 additions & 3 deletions modules/eslint-plugin/src/utils/helper-functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
isProgram,
isProperty,
isPropertyDefinition,
isTemplateElement,
isTemplateLiteral,
isTSTypeAnnotation,
isTSTypeReference,
isTemplateElement,
isTemplateLiteral,
} from './guards';
import { NGRX_MODULE_PATHS } from './ngrx-modules';

Expand All @@ -30,7 +30,8 @@ type InjectedParameter =
| ConstructorFunctionExpression
| (TSESTree.TSParameterProperty & {
parent: ConstructorFunctionExpression;
});
})
| TSESTree.PropertyDefinition;
};
type InjectedParameterWithSourceCode = Readonly<{
identifiers?: readonly InjectedParameter[];
Expand Down Expand Up @@ -315,6 +316,12 @@ function getInjectedParametersWithSourceCode(
const { importSpecifier } =
getImportDeclarationSpecifier(importDeclarations, importName) ?? {};

const injectImportDeclarations =
getImportDeclarations(sourceCode.ast, '@angular/core') ?? [];

const { importSpecifier: injectImportSpecifier } =
getImportDeclarationSpecifier(injectImportDeclarations, 'inject') ?? {};

if (!importSpecifier) {
return { sourceCode };
}
Expand All @@ -335,6 +342,19 @@ function getInjectedParametersWithSourceCode(
return identifiers.concat(parent.parent.parent as InjectedParameter);
}

if (
parent &&
isCallExpression(parent) &&
isIdentifier(parent.callee) &&
parent.callee.name == 'inject' &&
parent.parent &&
isPropertyDefinition(parent.parent) &&
isIdentifier(parent.parent.key) &&
injectImportSpecifier
) {
return identifiers.concat(parent.parent.key as InjectedParameter);
}

return identifiers;
}, []);
return { identifiers, sourceCode };
Expand Down

0 comments on commit 8a5884d

Please sign in to comment.