Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.AbstractPostOrderCallback;
20 import com.google.javascript.rhino.IR;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 /**
25 * Converts property accesses from quoted string syntax to dot syntax, where
26 * possible. Dot syntax is more compact and avoids an object allocation in
27 * IE 6.
28 *
29 */
30 class ConvertToDottedProperties extends AbstractPostOrderCallback
31 implements CompilerPass {
32
33 private final AbstractCompiler compiler;
34
35 ConvertToDottedProperties(AbstractCompiler compiler) {
36 this.compiler = compiler;
37 }
38
39 @Override
40 public void process(Node externs, Node root) {
41 NodeTraversal.traverse(compiler, root, this);
42 }
43
44 @Override
45 public void visit(NodeTraversal t, Node n, Node parent) {
46 switch (n.getType()) {
47 case Token.GETTER_DEF:
48 case Token.SETTER_DEF:
49 case Token.STRING_KEY:
50 if (NodeUtil.isValidPropertyName(n.getString())) {
51 n.putBooleanProp(Node.QUOTED_PROP, false);
52 compiler.reportCodeChange();
53 }
54 break;
55
56 case Token.GETELEM:
57 Node left = n.getFirstChild();
58 Node right = left.getNext();
59 if (right.isString() &&
60 NodeUtil.isValidPropertyName(right.getString())) {
61 n.removeChild(left);
62 n.removeChild(right);
63 parent.replaceChild(n, IR.getprop(left, right));
64 compiler.reportCodeChange();
65 }
66 break;
67 }
68 }
69 }