This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ngrx-no-effects-in-providers (#26)
- Loading branch information
1 parent
fb2137c
commit 39f735a
Showing
7 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { tsquery } from '@phenomnomnominal/tsquery' | ||
import * as Lint from 'tslint' | ||
import * as ts from 'typescript' | ||
import { NG_MODULE_IMPORTS, NG_MODULE_PROVIDERS } from '../utils/queries' | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: 'The Effect should not be listed as a provider', | ||
descriptionDetails: | ||
'If an Effect is registered to the EffectsModule and is added as a provider, it will be registered twice', | ||
options: null, | ||
optionsDescription: 'Not configurable', | ||
requiresTypeInfo: false, | ||
ruleName: 'ngrx-no-effects-in-providers', | ||
type: 'functionality', | ||
typescriptOnly: true, | ||
} | ||
|
||
public static FAILURE_STRING = | ||
'The Effect should not be listed as a provider if it is added to the EffectsModule' | ||
|
||
private static QUERY = `${NG_MODULE_IMPORTS} > CallExpression[expression.expression.text="EffectsModule"][expression.name.text=/forFeature|forRoot/] > ArrayLiteralExpression > Identifier` | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const effectIdentifierNodes = tsquery( | ||
sourceFile, | ||
Rule.QUERY, | ||
) as ts.Identifier[] | ||
|
||
const effectNames = effectIdentifierNodes.map(node => node.text).join('|') | ||
const hits = tsquery( | ||
sourceFile, | ||
`${NG_MODULE_PROVIDERS} > Identifier[name=/${effectNames}/]`, | ||
) | ||
|
||
const failures = hits.map( | ||
(node): Lint.RuleFailure => | ||
new Lint.RuleFailure( | ||
sourceFile, | ||
node.getStart(), | ||
node.getStart() + node.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName, | ||
), | ||
) | ||
|
||
return failures | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const queryNgModuleProperty = (property: string) => | ||
`${NG_MODULE_QUERY} > ObjectLiteralExpression PropertyAssignment[name.text="${property}"] > ArrayLiteralExpression` | ||
|
||
export const NG_MODULE_QUERY = | ||
'ClassDeclaration > Decorator > CallExpression[expression.text="NgModule"]' | ||
export const NG_MODULE_IMPORTS = queryNgModuleProperty('imports') | ||
export const NG_MODULE_PROVIDERS = queryNgModuleProperty('providers') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { StoreModule } from '@ngrx/store' | ||
import { EffectsModule } from '@ngrx/effects' | ||
|
||
@NgModule({ | ||
imports: [ | ||
StoreModule.forFeature('persons', {"foo": "bar"}), | ||
EffectsModule.forRoot([RootEffectOne, RootEffectTwo]), | ||
EffectsModule.forFeature([FeatEffectOne, FeatEffectTwo]), | ||
EffectsModule.forFeature([FeatEffectThree]), | ||
], | ||
providers: [FeatEffectTwo, UnRegisteredEffect, FeatEffectThree, RootEffectTwo], | ||
~~~~~~~~~~~~~ [error] | ||
~~~~~~~~~~~~~~~ [error] | ||
~~~~~~~~~~~~~ [error] | ||
|
||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
|
||
[error]: The Effect should not be listed as a provider if it is added to the EffectsModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"lib": ["es2015"], | ||
"noEmit": true, | ||
"paths": { | ||
"@ngrx/effects": ["../../../node_modules/@ngrx/effects"] | ||
}, | ||
"skipLibCheck": true, | ||
"target": "es5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"rules": { | ||
"ngrx-no-effects-in-providers": true | ||
}, | ||
"rulesDirectory": ["../../../dist/rules"] | ||
} |