Skip to content

Commit

Permalink
feat: noDupActionsRule
Browse files Browse the repository at this point in the history
  • Loading branch information
paibamboo committed Mar 14, 2019
1 parent 6b528be commit ceed2ee
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/noDupActionsRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as Lint from "tslint";
import * as ts from "typescript";

class NoDupActionsRule extends Lint.RuleWalker {
public actionNames: string[] = [];

constructor(
sourceFile: ts.SourceFile,
options: Lint.IOptions
) {
super(sourceFile, options);
}

public visitCallExpression(node: ts.CallExpression): void {
super.visitCallExpression(node);
if (ts.isIdentifier(node.expression)) {
if (node.expression.escapedText === "createAsyncActions") {
for (const argument of node.arguments) {
if (this.actionNames.indexOf(argument.getText()) !== -1) {
this.addFailureAtNode(argument, "Duplicate redux action");
return;
}
this.actionNames.push(argument.getText());
}
}
if (node.expression.escapedText === "createAction") {
if (this.actionNames.indexOf(node.arguments[0].getText()) !== -1) {
this.addFailureAtNode(node.arguments[0], "Duplicate redux action");
return;
}
this.actionNames.push(node.arguments[0].getText());
}
}
}
}

// tslint:disable-next-line:export-name max-classes-per-file
export class Rule extends Lint.Rules.TypedRule {
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDupActionsRule(sourceFile, this.getOptions()));
}
}
22 changes: 22 additions & 0 deletions test/rules/no-dup-actions/create-action-dup/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"CART/ADD_TO_CART_PENDING",
"CART/ADD_TO_CART_FULFILLED",
"CART/ADD_TO_CART_REJECTED"
)<IAddToCartPayload, null, null, null>();

export const setFabricDataWithUrls = createAction(
"FABRIC/SET_DATA_WITH_URLS",
(resolve) => (data: IFabricData) => resolve(data)
);

export const addImage = createAction(
"FABRIC/ADD_IMAGE_TO_EDITOR",
(resolve) => (url: string) => resolve(url)
);

export const addShape = createAction(
"FABRIC/ADD_IMAGE_TO_EDITOR",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Duplicate redux action]
(resolve) => (url: string) => resolve(url)
);
7 changes: 7 additions & 0 deletions test/rules/no-dup-actions/create-action-dup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6"]
}
}
8 changes: 8 additions & 0 deletions test/rules/no-dup-actions/create-action-dup/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"no-dup-actions": true
}
}
14 changes: 14 additions & 0 deletions test/rules/no-dup-actions/create-async-actions-dup/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"CART/ADD_TO_CART_PENDING",
"CART/ADD_TO_CART_FULFILLED",
"CART/ADD_TO_CART_REJECTED"
)<IAddToCartPayload, null, null, null>();

export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
~~~~~~~~~~~~~~~~~~ [Duplicate redux action]
"CART/ADD_TO_CART_PENDING",
"CART/ADD_TO_CART_FULFILLED",
"CART/ADD_TO_CART_REJECTED"
)<IAddToCartPayload, null, null, null>();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"no-dup-actions": true
}
}
21 changes: 21 additions & 0 deletions test/rules/no-dup-actions/no-dup/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"CART/ADD_TO_CART_PENDING",
"CART/ADD_TO_CART_FULFILLED",
"CART/ADD_TO_CART_REJECTED"
)<IAddToCartPayload, null, null, null>();

export const setFabricDataWithUrls = createAction(
"FABRIC/SET_DATA_WITH_URLS",
(resolve) => (data: IFabricData) => resolve(data)
);

export const addImage = createAction(
"FABRIC/ADD_IMAGE_TO_EDITOR",
(resolve) => (url: string) => resolve(url)
);

export const addShape = createAction(
"FABRIC/ADD_SHAPE_TO_EDITOR",
(resolve) => (url: string) => resolve(url)
);
7 changes: 7 additions & 0 deletions test/rules/no-dup-actions/no-dup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6"]
}
}
8 changes: 8 additions & 0 deletions test/rules/no-dup-actions/no-dup/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"no-dup-actions": true
}
}

0 comments on commit ceed2ee

Please sign in to comment.