Skip to content

Commit

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

class CreateAsyncActionsRule extends Lint.RuleWalker {
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") {
if (node.arguments.length !== 4) {
this.addFailureAtNode(node, "Number of arguments of createAsyncActions must be 4");
return;
}

const firstArg = node.arguments[0].getText();
const secondArg = node.arguments[1].getText();
if (secondArg.substring(1, secondArg.length - 1) !== firstArg.substring(1, firstArg.length - 1) + "_PENDING") {
this.addFailureAtNode(
node.arguments[1],
"Second argument of createAsyncActions must be the first argument followed by _PENDING"
);
return;
}

const thirdArg = node.arguments[2].getText();
if (thirdArg.substring(1, thirdArg.length - 1) !== firstArg.substring(1, firstArg.length - 1) + "_FULFILLED") {
this.addFailureAtNode(
node.arguments[2],
"Third argument of createAsyncActions must be the first argument followed by _FULFILLED"
);
return;
}

const fourthArg = node.arguments[3].getText();
if (fourthArg.substring(1, fourthArg.length - 1) !== firstArg.substring(1, firstArg.length - 1) + "_REJECTED") {
this.addFailureAtNode(
node.arguments[3],
"Fourth argument of createAsyncActions must be the first argument followed by _REJECTED"
);
return;
}
}
}
}
}

// 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 CreateAsyncActionsRule(sourceFile, this.getOptions()));
}
}
33 changes: 33 additions & 0 deletions test/rules/create-async-actions/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const addToCart = createAsyncActions("CART/ADD_TO_CART")<IAddToCartPayload, null, null, null>();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Number of arguments of createAsyncActions must be 4]

export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"",
~~ [Second argument of createAsyncActions must be the first argument followed by _PENDING]
"",
""
)<IAddToCartPayload, null, null, null>();

export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"CART/ADD_TO_CART_PENDING",
"",
~~ [Third argument of createAsyncActions must be the first argument followed by _FULFILLED]
""
)<IAddToCartPayload, null, null, null>();

export const addToCart = createAsyncActions(
"CART/ADD_TO_CART",
"CART/ADD_TO_CART_PENDING",
"CART/ADD_TO_CART_FULFILLED",
""
~~ [Fourth argument of createAsyncActions must be the first argument followed by _REJECTED]
)<IAddToCartPayload, null, null, null>();

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>();
7 changes: 7 additions & 0 deletions test/rules/create-async-actions/default/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/create-async-actions/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"create-async-actions": true
}
}
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"no-console": [
true,
"debug",
"info",
"log",
"time",
"timeEnd",
Expand Down

0 comments on commit 6b528be

Please sign in to comment.