Skip to content

Instantly share code, notes, and snippets.

@phenomnomnominal
Last active August 18, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phenomnomnominal/54d90ab84f69b9edd102f663d204576a to your computer and use it in GitHub Desktop.
Save phenomnomnominal/54d90ab84f69b9edd102f663d204576a to your computer and use it in GitHub Desktop.
TSLint rule to avoid accidental `fdescribe` and `fit` calls in test code
// Dependencies:
import { tsquery } from '@phenomnomnominal/tsquery';
import { Fix, Replacement, RuleFailure, Rules } from 'tslint';
import { SourceFile } from 'typescript';
// Constants:
const FDESCRIBE_FIT_QUERY = 'CallExpression > Identifier[name=/^f(describe|it)$/]';
const FAILURE_MESSAGE = (filter: string) => `
Remember to remove "${filter}" once you have finished working on tests.
`;
export class Rule extends Rules.AbstractRule {
public apply (sourceFile: SourceFile): Array<RuleFailure> {
const options = this.getOptions();
const [fileFilter] = options.ruleArguments;
if (fileFilter && !sourceFile.fileName.includes(fileFilter)) {
return [];
}
return tsquery(sourceFile, FDESCRIBE_FIT_QUERY).map(result => {
const replacement = new Replacement(result.getStart(), result.getWidth(), result.name.replace(/^f/, ''));
return new RuleFailure(result.getSourceFile(), result.getStart(), result.getEnd(), FAILURE_MESSAGE(result.name as string), this.ruleName, replacement);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment