UNPKG

1.54 kBPlain TextView Raw
1import * as ts from 'typescript';
2import * as Lint from 'tslint';
3
4/**
5 * A base walker class that gracefully handles unexpected errors.
6 * Errors are often thrown when the TypeChecker is invoked.
7 */
8export class ErrorTolerantWalker extends Lint.RuleWalker {
9 public static DEBUG: boolean = false;
10
11 protected visitNode(node: ts.Node): void {
12 try {
13 super.visitNode(node);
14 } catch (error) {
15 // turn this on when trying out new rules on foreign codebases
16 if (ErrorTolerantWalker.DEBUG) {
17 const msg: string =
18 'An error occurred visiting a node.' +
19 '\nWalker: ' +
20 this.getClassName() +
21 '\nNode: ' +
22 (node.getFullText ? node.getFullText() : '<unknown>') +
23 '\n' +
24 error;
25
26 this.addFailureAt(node.getStart ? node.getStart() : 0, node.getWidth ? node.getWidth() : 0, msg);
27 }
28 }
29 }
30
31 private getClassName(): string {
32 // Some versions of IE have the word "function" in the constructor name and
33 // have the function body there as well. This rips out and returns the function name.
34 const result: string = this.constructor.toString().match(/function\s+([\w\$]+)\s*\(/)[1] || '';
35 if (result == null || result.length === 0) {
36 throw new Error('Could not determine class name from input: ' + this.constructor.toString());
37 }
38 return result;
39 }
40}