Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
convert mocha-avoid-only rule to use walk function (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
drexler authored and Josh Goldberg committed Dec 19, 2018
1 parent 9b25025 commit 71cf21d
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/mochaAvoidOnlyRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { MochaUtils } from './utils/MochaUtils';
Expand All @@ -25,38 +26,46 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_CONTEXT: string = 'Do not commit Mocha context.only function call';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new MochaAvoidOnlyRuleWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk);
}
}

class MochaAvoidOnlyRuleWalker extends Lint.RuleWalker {
protected visitSourceFile(node: ts.SourceFile): void {
if (MochaUtils.isMochaTest(node)) {
super.visitSourceFile(node);
}
}
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (tsutils.isCallExpression(node)) {
if (tsutils.isPropertyAccessExpression(node.expression)) {
if (node.arguments.length === 2) {
if (tsutils.isStringLiteral(node.arguments[0])) {
if (tsutils.isFunctionExpression(node.arguments[1]) || tsutils.isArrowFunction(node.arguments[1])) {
const text = node.expression.getText();
switch (text) {
case 'it.only':
ctx.addFailureAt(node.getStart(), text.length, Rule.FAILURE_STRING_IT);
break;

case 'specify.only':
ctx.addFailureAt(node.getStart(), text.length, Rule.FAILURE_STRING_SPECIFY);
break;

case 'describe.only':
ctx.addFailureAt(node.getStart(), text.length, Rule.FAILURE_STRING_DESCRIBE);
break;

protected visitCallExpression(node: ts.CallExpression): void {
if (node.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
if (node.arguments.length === 2) {
if (node.arguments[0].kind === ts.SyntaxKind.StringLiteral) {
if (
node.arguments[1].kind === ts.SyntaxKind.FunctionExpression ||
node.arguments[1].kind === ts.SyntaxKind.ArrowFunction
) {
if (node.expression.getText() === 'it.only') {
this.addFailureAt(node.getStart(), node.expression.getText().length, Rule.FAILURE_STRING_IT);
} else if (node.expression.getText() === 'specify.only') {
this.addFailureAt(node.getStart(), node.expression.getText().length, Rule.FAILURE_STRING_SPECIFY);
} else if (node.expression.getText() === 'describe.only') {
this.addFailureAt(node.getStart(), node.expression.getText().length, Rule.FAILURE_STRING_DESCRIBE);
} else if (node.expression.getText() === 'context.only') {
this.addFailureAt(node.getStart(), node.expression.getText().length, Rule.FAILURE_STRING_CONTEXT);
case 'context.only':
ctx.addFailureAt(node.getStart(), text.length, Rule.FAILURE_STRING_CONTEXT);
break;

default: // required per tslint rules for switch statements.
}
}
}
}
}
}
super.visitCallExpression(node);
return ts.forEachChild(node, cb);
}

if (MochaUtils.isMochaTest(ctx.sourceFile)) {
return ts.forEachChild(ctx.sourceFile, cb);
}
}

0 comments on commit 71cf21d

Please sign in to comment.