Debian Codegraph @

src/com/google/javascript/jscomp/GatherRawExports.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.common.base.Preconditions;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
22 import com.google.javascript.rhino.Node;
23 import java.util.Set;
24
25 /**
26 * External references of the form: "window['xx']" indicate names that must
27 * be reserved when variable renaming to avoid conflicts.
28 *
29 * @author [email protected] (John Lenz)
30 */
31 class GatherRawExports extends AbstractPostOrderCallback
32 implements CompilerPass {
33
34 private final AbstractCompiler compiler;
35
36 private static final String GLOBAL_THIS_NAMES[] = { "window", "top" };
37
38 private final Set<String> exportedVariables = Sets.newHashSet();
39
40 GatherRawExports(AbstractCompiler compiler) {
41 this.compiler = compiler;
42 }
43
44 @Override
45 public void process(Node externs, Node root) {
46 Preconditions.checkState(compiler.getLifeCycleStage().isNormalized());
47 NodeTraversal.traverse(compiler, root, this);
48 }
49
50 @Override
51 public void visit(NodeTraversal t, Node n, Node parent) {
52 Node sibling = n.getNext();
53 if (sibling != null
54 && sibling.isString()
55 && NodeUtil.isGet(parent)) {
56 // TODO(johnlenz): Should we warn if we see a property name that
57 // hasn't been exported?
58 if (isGlobalThisObject(t, n)) {
59 exportedVariables.add(sibling.getString());
60 }
61 }
62 }
63
64 private boolean isGlobalThisObject(NodeTraversal t, Node n) {
65 if (n.isThis()) {
66 return t.inGlobalScope();
67 } else if (n.isName()) {
68 String varName = n.getString();
69 int items = GLOBAL_THIS_NAMES.length;
70 for (int i = 0; i < items; i++) {
71 if (varName.equals(GLOBAL_THIS_NAMES[i])) {
72 return true;
73 }
74 }
75 }
76 return false;
77 }
78
79 public Set<String> getExportedVariableNames() {
80 return exportedVariables;
81 }
82 }