Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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
23 /**
24 * Caja is a system that rewrites web content (JavaScript, CSS, HTML)
25 * into web content that is safe to inline directly into a page.
26 * The rewritten ("cajoled") code runs in the presence of a JS library
27 * that adds some properties to Object.prototype. Because JS does not
28 * yet (until ES5) allow programmers to mark properties as DontEnum,
29 * for..in loops will see unexpected properties.
30 *
31 * This pass adds a conditional to for..in loops that filters out these
32 * properties.
33 *
34 */
35
36 class IgnoreCajaProperties implements CompilerPass {
37
38 final AbstractCompiler compiler;
39
40 // Counts the number of temporary variables introduced.
41 int counter;
42
43 public IgnoreCajaProperties(AbstractCompiler compiler) {
44 this.compiler = compiler;
45 this.counter = 0;
46 }
47
48 @Override
49 public void process(Node externs, Node root) {
50 NodeTraversal.traverse(compiler, root, new Traversal());
51 }
52
53 private class Traversal extends AbstractPostOrderCallback {
54 @Override
55 public void visit(NodeTraversal t, Node n, Node parent) {
56 // Look for a for..in loop.
57 if (n.isFor() && n.getChildCount() == 3) {
58 Node body = n.getLastChild();
59 n.removeChild(body);
60 Node key = n.getFirstChild();
61 n.removeChild(key);
62 Node tmp = IR.name(
63 "JSCompiler_IgnoreCajaProperties_" + counter++);
64 n.addChildToFront(IR.var(tmp));
65 Node ifBody;
66
67 // Construct the body of the if statement.
68 if (key.isVar()) {
69 // for (var key in x) { body; }
70 // =>
71 // for (var tmp in x) {
72 // if (!tmp.match(/___$/)) {
73 // var key;
74 // key = tmp;
75 // body;
76 // }
77 // }
78 ifBody = IR.block(
79 key,
80 IR.exprResult(
81 IR.assign(
82 key.getFirstChild().cloneNode(),
83 tmp.cloneTree())),
84 body);
85 } else {
86 // for (key in x) { body; }
87 // =>
88 // for (var tmp in x) {
89 // if (!tmp.match(/___$/)) {
90 // key = tmp;
91 // body;
92 // }
93 // }
94 ifBody = IR.block(
95 IR.exprResult(
96 IR.assign(
97 key,
98 tmp.cloneTree())),
99 body);
100 }
101
102 // Construct the new body of the for loop.
103 Node newBody = IR.block(
104 IR.ifNode(
105 IR.not(
106 IR.call(
107 IR.getprop(
108 tmp.cloneTree(),
109 IR.string("match")),
110 IR.regexp(
111 IR.string("___$")))),
112 ifBody));
113 n.addChildToBack(newBody);
114 compiler.reportCodeChange();
115 }
116 }
117 }
118 }