diff --git a/src/createAsyncActionsRule.ts b/src/createAsyncActionsRule.ts new file mode 100644 index 0000000..3db7450 --- /dev/null +++ b/src/createAsyncActionsRule.ts @@ -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())); + } +} diff --git a/test/rules/create-async-actions/default/test.ts.lint b/test/rules/create-async-actions/default/test.ts.lint new file mode 100644 index 0000000..baeea48 --- /dev/null +++ b/test/rules/create-async-actions/default/test.ts.lint @@ -0,0 +1,33 @@ +export const addToCart = createAsyncActions("CART/ADD_TO_CART")(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [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] + "", + "" +)(); + +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] + "" +)(); + +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] +)(); + +export const addToCart = createAsyncActions( + "CART/ADD_TO_CART", + "CART/ADD_TO_CART_PENDING", + "CART/ADD_TO_CART_FULFILLED", + "CART/ADD_TO_CART_REJECTED" +)(); diff --git a/test/rules/create-async-actions/default/tsconfig.json b/test/rules/create-async-actions/default/tsconfig.json new file mode 100644 index 0000000..9d7ea05 --- /dev/null +++ b/test/rules/create-async-actions/default/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "lib": ["es6"] + } +} diff --git a/test/rules/create-async-actions/default/tslint.json b/test/rules/create-async-actions/default/tslint.json new file mode 100644 index 0000000..e82964c --- /dev/null +++ b/test/rules/create-async-actions/default/tslint.json @@ -0,0 +1,8 @@ +{ + "rulesDirectory": [ + "../../../../lib" + ], + "rules": { + "create-async-actions": true + } +} diff --git a/tslint.json b/tslint.json index 762c579..fe79a55 100644 --- a/tslint.json +++ b/tslint.json @@ -41,7 +41,6 @@ "no-console": [ true, "debug", - "info", "log", "time", "timeEnd",