|
| 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 | +} |
0 commit comments