Skip to content

Instantly share code, notes, and snippets.

@danreeves
Created April 6, 2018 12:29
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 danreeves/cd50ae91ffc46bfeb76b013e2cc40c4e to your computer and use it in GitHub Desktop.
Save danreeves/cd50ae91ffc46bfeb76b013e2cc40c4e to your computer and use it in GitHub Desktop.
Based on the example (https://palantir.github.io/tslint/develop/custom-rules/) and totally untested. Also I don't know typescript.
import * as ts from "typescript";
import * as Lint from "tslint";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Prefer keyCode to key or code";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new PreferKeyCodeWalker(sourceFile, this.getOptions()));
}
}
// The walker takes care of all the work.
class PreferKeyCodeWalker extends Lint.RuleWalker {
public visitPropertyAccessExpression(node: ts.ImportDeclaration) {
if (node.expression.text === 'KeyboardEvent' && node.name.text === 'code') {
// create a failure at the current position
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
// call the base version of this visitor to actually parse this node
super.visitPropertyAccessExpression(node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment