Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

Commit

Permalink
fix: ngrx-action-hygiene should only verify action name in createActi…
Browse files Browse the repository at this point in the history
…on (#23)
  • Loading branch information
timdeschryver authored Dec 27, 2019
1 parent 985f24d commit e74dd69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"copy:schematics": "cp ./src/schematics/collection.json ./dist/schematics & cp ./src/schematics/ng-add/schema.json ./dist/schematics/ng-add",
"generate-schema": "ts-node ./src/scripts/generate-schema.ts",
"lint": "tslint -p .",
"build-and-test": "npm run build && npm run test:rules",
"test": "npm run test:schematics && npm run test:rules",
"test:schematics": "jest --config ./test/jest.config.json",
"test:rules": "tslint --test ./test/rules/**/tslint.json",
Expand Down
24 changes: 17 additions & 7 deletions src/rules/ngrxActionHygieneRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ export class Rule extends Lint.Rules.TypedRule {
public static FAILURE_STRING =
'Action type does not follow the good action hygiene practice, use "[Source] Event" to define action types'

private static ACTION_CREATOR_QUERY =
'CallExpression > Identifier[name=/createAction.*/]'

public applyWithProgram(
sourceFile: ts.SourceFile,
program: ts.Program,
): Lint.RuleFailure[] {
const creators = tsquery(
sourceFile,
`CallExpression:has(Identifier[name=/createAction.*/]) > StringLiteral`,
)
const hits = creators.filter(
(node): boolean => !node.getText().match(/[\[].*[\]]\s.*/),
)
const creators = tsquery(sourceFile, Rule.ACTION_CREATOR_QUERY)
const hits = creators
.map(({ parent }) => {
if (!ts.isCallExpression(parent)) {
return undefined
}

return tsquery(parent, 'StringLiteral')[0]
})
.filter(
actionTypeNode =>
actionTypeNode && !/[\[].*[\]]\s.*/.test(actionTypeNode.getText()),
)

const failures = hits.map(
(node): Lint.RuleFailure =>
new Lint.RuleFailure(
Expand Down
14 changes: 14 additions & 0 deletions test/rules/ngrx-action-hygiene/fixture.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ const action = createAction(

const action = createAction('[Customers] load', ( page: number ) => ({ page }))


// https://github.com/timdeschryver/ngrx-tslint-rules/issues/22
describe("application reducers", () => {
describe("appInit reducers", () => {
it("should return the initial state", () => {
let newState = initializationStateReducer(undefined, createAction("[App Tests] Init"));
expect(newState).toEqual(appInitInitialState);

newState = initializationStateReducer(undefined, initializeApplication);
expect(newState).toEqual(appInitInitialState);
});
});
});

[action-hygiene]: Action type does not follow the good action hygiene practice, use "[Source] Event" to define action types

0 comments on commit e74dd69

Please sign in to comment.