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

convert mocha-avoid-only rule to use walk function #688

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally went with the switch statement over the if/elseif for clarity. The forced inclusion of default mars it, admittedly. I can switch to the if/else if if required.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for getting rid of continuous .getText() calls!

}
}
}
}
}
}
super.visitCallExpression(node);
return ts.forEachChild(node, cb);
}

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