Skip to content

Commit ceed2ee

Browse files
committed
feat: noDupActionsRule
1 parent 6b528be commit ceed2ee

10 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/noDupActionsRule.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as Lint from "tslint";
2+
import * as ts from "typescript";
3+
4+
class NoDupActionsRule extends Lint.RuleWalker {
5+
public actionNames: string[] = [];
6+
7+
constructor(
8+
sourceFile: ts.SourceFile,
9+
options: Lint.IOptions
10+
) {
11+
super(sourceFile, options);
12+
}
13+
14+
public visitCallExpression(node: ts.CallExpression): void {
15+
super.visitCallExpression(node);
16+
if (ts.isIdentifier(node.expression)) {
17+
if (node.expression.escapedText === "createAsyncActions") {
18+
for (const argument of node.arguments) {
19+
if (this.actionNames.indexOf(argument.getText()) !== -1) {
20+
this.addFailureAtNode(argument, "Duplicate redux action");
21+
return;
22+
}
23+
this.actionNames.push(argument.getText());
24+
}
25+
}
26+
if (node.expression.escapedText === "createAction") {
27+
if (this.actionNames.indexOf(node.arguments[0].getText()) !== -1) {
28+
this.addFailureAtNode(node.arguments[0], "Duplicate redux action");
29+
return;
30+
}
31+
this.actionNames.push(node.arguments[0].getText());
32+
}
33+
}
34+
}
35+
}
36+
37+
// tslint:disable-next-line:export-name max-classes-per-file
38+
export class Rule extends Lint.Rules.TypedRule {
39+
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
40+
return this.applyWithWalker(new NoDupActionsRule(sourceFile, this.getOptions()));
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const addToCart = createAsyncActions(
2+
"CART/ADD_TO_CART",
3+
"CART/ADD_TO_CART_PENDING",
4+
"CART/ADD_TO_CART_FULFILLED",
5+
"CART/ADD_TO_CART_REJECTED"
6+
)<IAddToCartPayload, null, null, null>();
7+
8+
export const setFabricDataWithUrls = createAction(
9+
"FABRIC/SET_DATA_WITH_URLS",
10+
(resolve) => (data: IFabricData) => resolve(data)
11+
);
12+
13+
export const addImage = createAction(
14+
"FABRIC/ADD_IMAGE_TO_EDITOR",
15+
(resolve) => (url: string) => resolve(url)
16+
);
17+
18+
export const addShape = createAction(
19+
"FABRIC/ADD_IMAGE_TO_EDITOR",
20+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Duplicate redux action]
21+
(resolve) => (url: string) => resolve(url)
22+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"lib": ["es6"]
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rulesDirectory": [
3+
"../../../../lib"
4+
],
5+
"rules": {
6+
"no-dup-actions": true
7+
}
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const addToCart = createAsyncActions(
2+
"CART/ADD_TO_CART",
3+
"CART/ADD_TO_CART_PENDING",
4+
"CART/ADD_TO_CART_FULFILLED",
5+
"CART/ADD_TO_CART_REJECTED"
6+
)<IAddToCartPayload, null, null, null>();
7+
8+
export const addToCart = createAsyncActions(
9+
"CART/ADD_TO_CART",
10+
~~~~~~~~~~~~~~~~~~ [Duplicate redux action]
11+
"CART/ADD_TO_CART_PENDING",
12+
"CART/ADD_TO_CART_FULFILLED",
13+
"CART/ADD_TO_CART_REJECTED"
14+
)<IAddToCartPayload, null, null, null>();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"lib": ["es6"]
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rulesDirectory": [
3+
"../../../../lib"
4+
],
5+
"rules": {
6+
"no-dup-actions": true
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const addToCart = createAsyncActions(
2+
"CART/ADD_TO_CART",
3+
"CART/ADD_TO_CART_PENDING",
4+
"CART/ADD_TO_CART_FULFILLED",
5+
"CART/ADD_TO_CART_REJECTED"
6+
)<IAddToCartPayload, null, null, null>();
7+
8+
export const setFabricDataWithUrls = createAction(
9+
"FABRIC/SET_DATA_WITH_URLS",
10+
(resolve) => (data: IFabricData) => resolve(data)
11+
);
12+
13+
export const addImage = createAction(
14+
"FABRIC/ADD_IMAGE_TO_EDITOR",
15+
(resolve) => (url: string) => resolve(url)
16+
);
17+
18+
export const addShape = createAction(
19+
"FABRIC/ADD_SHAPE_TO_EDITOR",
20+
(resolve) => (url: string) => resolve(url)
21+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"lib": ["es6"]
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rulesDirectory": [
3+
"../../../../lib"
4+
],
5+
"rules": {
6+
"no-dup-actions": true
7+
}
8+
}

0 commit comments

Comments
 (0)