Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2004 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.common.base.Preconditions;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 import java.util.*;
25
26 /**
27 * Verifies that constants are only assigned a value once.
28 * e.g. var XX = 5;
29 * XX = 3; // error!
30 * XX++; // error!
31 *
32 */
33 class ConstCheck extends AbstractPostOrderCallback
34 implements CompilerPass {
35
36 static final DiagnosticType CONST_REASSIGNED_VALUE_ERROR =
37 DiagnosticType.error(
38 "JSC_CONSTANT_REASSIGNED_VALUE_ERROR",
39 "constant {0} assigned a value more than once");
40
41 private final AbstractCompiler compiler;
42 private final Set<Scope.Var> initializedConstants;
43
44 /**
45 * Creates an instance.
46 */
47 public ConstCheck(AbstractCompiler compiler) {
48 this.compiler = compiler;
49 this.initializedConstants = new HashSet<Scope.Var>();
50 }
51
52 @Override
53 public void process(Node externs, Node root) {
54 Preconditions.checkState(compiler.getLifeCycleStage().isNormalized());
55 NodeTraversal.traverse(compiler, root, this);
56 }
57
58 @Override
59 public void visit(NodeTraversal t, Node n, Node parent) {
60 switch (n.getType()) {
61 case Token.NAME:
62 if (parent != null &&
63 parent.isVar() &&
64 n.hasChildren()) {
65 String name = n.getString();
66 Scope.Var var = t.getScope().getVar(name);
67 if (isConstant(var)) {
68 if (initializedConstants.contains(var)) {
69 reportError(t, n, name);
70 } else {
71 initializedConstants.add(var);
72 }
73 }
74 }
75 break;
76
77 case Token.ASSIGN:
78 case Token.ASSIGN_BITOR:
79 case Token.ASSIGN_BITXOR:
80 case Token.ASSIGN_BITAND:
81 case Token.ASSIGN_LSH:
82 case Token.ASSIGN_RSH:
83 case Token.ASSIGN_URSH:
84 case Token.ASSIGN_ADD:
85 case Token.ASSIGN_SUB:
86 case Token.ASSIGN_MUL:
87 case Token.ASSIGN_DIV:
88 case Token.ASSIGN_MOD: {
89 Node lhs = n.getFirstChild();
90 if (lhs.isName()) {
91 String name = lhs.getString();
92 Scope.Var var = t.getScope().getVar(name);
93 if (isConstant(var)) {
94 if (initializedConstants.contains(var)) {
95 reportError(t, n, name);
96 } else {
97 initializedConstants.add(var);
98 }
99 }
100 }
101 break;
102 }
103
104 case Token.INC:
105 case Token.DEC: {
106 Node lhs = n.getFirstChild();
107 if (lhs.isName()) {
108 String name = lhs.getString();
109 Scope.Var var = t.getScope().getVar(name);
110 if (isConstant(var)) {
111 reportError(t, n, name);
112 }
113 }
114 break;
115 }
116 }
117 }
118
119 /**
120 * Gets whether a variable is a constant initialized to a literal value at
121 * the point where it is declared.
122 */
123 private boolean isConstant(Scope.Var var) {
124 return var != null && var.isConst();
125 }
126
127 /**
128 * Reports a reassigned constant error.
129 */
130 void reportError(NodeTraversal t, Node n, String name) {
131 compiler.report(t.makeError(n, CONST_REASSIGNED_VALUE_ERROR, name));
132 }
133 }