Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
fix(flux-action-dispatch): fixed reference and prettier (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
jukben authored Jun 6, 2019
1 parent 2a6da91 commit 8dbae6f
Showing 1 changed file with 67 additions and 51 deletions.
118 changes: 67 additions & 51 deletions src/rules/fluxActionDispatchRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,90 @@
* All rights reserved.
*/

import * as Lint from 'tslint';
import * as ts from 'typescript';
import * as Lint from "tslint";
import * as ts from "typescript";

export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Array<Lint.RuleFailure> {
return this.applyWithWalker(
new FluxActionDispatchRule(sourceFile, this.getOptions()),
);
}
public apply(sourceFile: ts.SourceFile): Array<Lint.RuleFailure> {
return this.applyWithWalker(
new FluxActionDispatchRule(sourceFile, this.getOptions())
);
}
}

type Configuration = {
reference: string;
reference: string;
};

class FluxActionDispatchRule extends Lint.RuleWalker {
private configuration: Configuration;
private configuration: Configuration;

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

const configuration = {
reference: '',
} as Configuration;
const configuration = {
reference: "",
...options.ruleArguments[0]
} as Configuration;

this.configuration = configuration;
}
this.configuration = configuration;
}

private getFormattedError(error: string): string {
return (
error +
(this.configuration.reference ? ` ${this.configuration.reference}` : '')
);
}

private static isAppDispatcherHandleViewActionCall(node: ts.CallExpression): boolean {
return ts.isPropertyAccessExpression(node.expression)
&& node.expression.name.text === 'handleViewAction'
&& ts.isIdentifier(node.expression.expression)
&& node.expression.expression.text === 'AppDispatcher'
}
private getFormattedError(error: string): string {
return (
error +
(this.configuration.reference ? ` ${this.configuration.reference}` : "")
);
}

private static hasTypedAction(node: ts.CallExpression): boolean {
return Boolean(node.typeArguments && node.typeArguments.length === 1)
}
private static isAppDispatcherHandleViewActionCall(
node: ts.CallExpression
): boolean {
return (
ts.isPropertyAccessExpression(node.expression) &&
node.expression.name.text === "handleViewAction" &&
ts.isIdentifier(node.expression.expression) &&
node.expression.expression.text === "AppDispatcher"
);
}

private static isSimpleAction(argument: ts.Expression): boolean {
return ts.isObjectLiteralExpression(argument) && argument.properties.length === 1
}
private static hasTypedAction(node: ts.CallExpression): boolean {
return Boolean(node.typeArguments && node.typeArguments.length === 1);
}

private static hasSimpleAction(node: ts.CallExpression): boolean {
return node.arguments.length === 1 && FluxActionDispatchRule.isSimpleAction(node.arguments[0])
}
private static isSimpleAction(argument: ts.Expression): boolean {
return (
ts.isObjectLiteralExpression(argument) && argument.properties.length === 1
);
}

private static breaksRule(node: ts.CallExpression): boolean {
return !FluxActionDispatchRule.hasTypedAction(node)
&& !FluxActionDispatchRule.hasSimpleAction(node);
}
private static hasSimpleAction(node: ts.CallExpression): boolean {
return (
node.arguments.length === 1 &&
FluxActionDispatchRule.isSimpleAction(node.arguments[0])
);
}

public visitCallExpression(node: ts.CallExpression) {
if (FluxActionDispatchRule.isAppDispatcherHandleViewActionCall(node)
&& FluxActionDispatchRule.breaksRule(node)) {
return this.addFailureAtNode(
node,
this.getFormattedError('handleViewAction must be typed or called with action object containing "type" property only.'),
);
}
private static breaksRule(node: ts.CallExpression): boolean {
return (
!FluxActionDispatchRule.hasTypedAction(node) &&
!FluxActionDispatchRule.hasSimpleAction(node)
);
}

super.visitCallExpression(node);
public visitCallExpression(node: ts.CallExpression) {
if (
FluxActionDispatchRule.isAppDispatcherHandleViewActionCall(node) &&
FluxActionDispatchRule.breaksRule(node)
) {
return this.addFailureAtNode(
node,
this.getFormattedError(
'handleViewAction must be typed or called with action object containing "type" property only.'
)
);
}

super.visitCallExpression(node);
}
}

0 comments on commit 8dbae6f

Please sign in to comment.