Debian Codegraph @

src/com/google/javascript/jscomp/LineNumberCheck.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * A simple pass to ensure that all AST nodes have line numbers,
24 * an that the line numbers are monotonically increasing.
25 *
26 * @author [email protected] (Nick Santos)
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 // Each JavaScript file is rooted in a script node, so we'll only
60 // have line number information inside the script node.
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 // The tree version of the node is really the best diagnostic
74 // info we have to offer here.
75 compiler.report(
76 t.makeError(n, MISSING_LINE_INFO,
77 n.toStringTree()));
78 }
79 }
80 }
81 }