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-multiple-stores (#20)
- Loading branch information
1 parent
4f1ad92
commit 985f24d
Showing
7 changed files
with
93 additions
and
1 deletion.
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,46 @@ | ||
import { tsquery } from '@phenomnomnominal/tsquery' | ||
import * as Lint from 'tslint' | ||
import * as ts from 'typescript' | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: 'The should only be one story injected', | ||
descriptionDetails: 'There is only one global store', | ||
options: null, | ||
optionsDescription: 'Not configurable', | ||
requiresTypeInfo: true, | ||
ruleName: 'ngrx-no-multiple-stores', | ||
type: 'maintainability', | ||
typescriptOnly: true, | ||
} | ||
|
||
public static FAILURE_STRING = 'Store should at most be one time injected' | ||
private CONSTRUCTOR_QUERY = `ClassDeclaration > Constructor` | ||
private PARAMETER_QUERY = `Parameter > TypeReference > Identifier[name="Store"]` | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const constructors = tsquery(sourceFile, this.CONSTRUCTOR_QUERY) | ||
const failures = constructors | ||
.map(ctor => tsquery(ctor, this.PARAMETER_QUERY)) | ||
.reduce( | ||
(hits, parameters) => { | ||
if (parameters.length <= 1) { | ||
return hits | ||
} | ||
const { parent: typeRefNode } = parameters.pop() | ||
const { parent: parameterNode } = typeRefNode | ||
const failure = new Lint.RuleFailure( | ||
sourceFile, | ||
parameterNode.getStart(), | ||
parameterNode.getStart() + parameterNode.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName, | ||
) | ||
return [...hits, failure] | ||
}, | ||
[] as Lint.RuleFailure[], | ||
) | ||
|
||
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
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,23 @@ | ||
import { Store } from '@ngrx/store' | ||
|
||
export class NotOk { | ||
constructor(store: Store<{}>, store2: Store<{}>){} | ||
~~~~~~~~~~~~~~~~~ [error] | ||
} | ||
|
||
export class NoCtorOK { | ||
} | ||
|
||
export class EmptyOk { | ||
constructor(){} | ||
} | ||
|
||
export class OneOk { | ||
constructor(private store: Store<{}>){} | ||
} | ||
|
||
export class OnePlusExtraOk { | ||
constructor(private store: Store<{}>, data: object){} | ||
} | ||
|
||
[error]: Store should at most be one time injected |
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/store": ["../../../node_modules/@ngrx/store"] | ||
}, | ||
"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-multiple-stores": true | ||
}, | ||
"rulesDirectory": ["../../../dist/rules"] | ||
} |