Skip to content

Instantly share code, notes, and snippets.

@danreeves
Created April 6, 2018 12:29
Show Gist options
  • Select an option

  • Save danreeves/cd50ae91ffc46bfeb76b013e2cc40c4e to your computer and use it in GitHub Desktop.

Select an option

Save danreeves/cd50ae91ffc46bfeb76b013e2cc40c4e to your computer and use it in GitHub Desktop.

Revisions

  1. danreeves created this gist Apr 6, 2018.
    23 changes: 23 additions & 0 deletions maybe-like-this.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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);
    }
    }