| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.jscomp.NodeTraversal.Callback; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class LineNumberCheck implements Callback, CompilerPass { |
| 29 |
|
| 30 |
static final DiagnosticType MISSING_LINE_INFO = DiagnosticType.error( |
| 31 |
"JSC_MISSING_LINE_INFO", |
| 32 |
"No source location information associated with {0}.\n" + |
| 33 |
"Most likely a Node has been created with settings the source file " + |
| 34 |
"and line/column location. Usually this is done using " + |
| 35 |
"Node.copyInformationFrom and supplying a Node from the source AST."); |
| 36 |
|
| 37 |
private final AbstractCompiler compiler; |
| 38 |
private boolean requiresLineNumbers = false; |
| 39 |
|
| 40 |
LineNumberCheck(AbstractCompiler compiler) { |
| 41 |
this.compiler = compiler; |
| 42 |
} |
| 43 |
|
| 44 |
public void setCheckSubTree(Node root) { |
| 45 |
requiresLineNumbers = true; |
| 46 |
|
| 47 |
NodeTraversal.traverse(compiler, root, this); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void process(Node externs, Node root) { |
| 52 |
requiresLineNumbers = false; |
| 53 |
|
| 54 |
NodeTraversal.traverse(compiler, root, this); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 59 |
|
| 60 |
|
| 61 |
if (n.isScript()) { |
| 62 |
requiresLineNumbers = true; |
| 63 |
} |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 69 |
if (n.isScript()) { |
| 70 |
requiresLineNumbers = false; |
| 71 |
} else if (requiresLineNumbers) { |
| 72 |
if (n.getLineno() == -1) { |
| 73 |
|
| 74 |
|
| 75 |
compiler.report( |
| 76 |
t.makeError(n, MISSING_LINE_INFO, |
| 77 |
n.toStringTree())); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |