Created
April 6, 2018 12:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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