Debian Codegraph @

src/com/google/javascript/jscomp/FindExportableNodes.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.common.collect.Maps;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.JSDocInfo;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import java.util.LinkedHashMap;
26
27 /**
28 * Records all of the symbols and properties that should be exported.
29 *
30 * Currently applies to:
31 * - function foo() {}
32 * - var foo = function() {}
33 * - foo.bar = function() {}
34 * - var FOO = ...;
35 * - foo.BAR = ...;
36 *
37 * FOO = BAR = 5;
38 * and
39 * var FOO = BAR = 5;
40 * are not supported because the annotation is ambiguous to whether it applies
41 * to all the variables or only the first one.
42 *
43 */
44 public class FindExportableNodes extends AbstractPostOrderCallback {
45
46 static final DiagnosticType NON_GLOBAL_ERROR =
47 DiagnosticType.error("JSC_NON_GLOBAL_ERROR",
48 "@export only applies to symbols/properties defined in the " +
49 "global scope.");
50
51 /**
52 * It's convenient to be able to iterate over exports in the order in which
53 * they are encountered.
54 */
55 private final LinkedHashMap<String, GenerateNodeContext> exports;
56
57 private final AbstractCompiler compiler;
58
59 public FindExportableNodes(AbstractCompiler compiler) {
60 this.compiler = compiler;
61 this.exports = Maps.newLinkedHashMap();
62 }
63
64 @Override
65 public void visit(NodeTraversal t, Node n, Node parent) {
66 JSDocInfo docInfo = n.getJSDocInfo();
67 if (docInfo != null && docInfo.isExport()) {
68 String export = null;
69 GenerateNodeContext context = null;
70
71 switch (n.getType()) {
72 case Token.FUNCTION:
73 if (parent.isScript()) {
74 export = NodeUtil.getFunctionName(n);
75 context = new GenerateNodeContext(n, parent, n);
76 }
77 break;
78 case Token.ASSIGN:
79 Node grandparent = parent.getParent();
80 if (grandparent != null && grandparent.isScript() &&
81 parent.isExprResult() &&
82 !n.getLastChild().isAssign()) {
83 export = n.getFirstChild().getQualifiedName();
84 context = new GenerateNodeContext(n, grandparent, parent);
85 }
86 break;
87 case Token.VAR:
88 if (parent.isScript()) {
89 if (n.getFirstChild().hasChildren() &&
90 !n.getFirstChild().getFirstChild().isAssign()) {
91 export = n.getFirstChild().getString();
92 context = new GenerateNodeContext(n, parent, n);
93 }
94 }
95 }
96
97 if (export != null) {
98 exports.put(export, context);
99 } else {
100 compiler.report(t.makeError(n, NON_GLOBAL_ERROR));
101 }
102 }
103 }
104
105 public LinkedHashMap<String, GenerateNodeContext> getExports() {
106 return exports;
107 }
108
109 /**
110 * Context holding the node references required for generating the export
111 * calls.
112 */
113 public static class GenerateNodeContext {
114 private final Node scriptNode;
115 private final Node contextNode;
116 private final Node node;
117
118 public GenerateNodeContext(Node node, Node scriptNode, Node contextNode) {
119 this.node = node;
120 this.scriptNode = scriptNode;
121 this.contextNode = contextNode;
122 }
123
124 public Node getNode() {
125 return node;
126 }
127
128 public Node getScriptNode() {
129 return scriptNode;
130 }
131
132 public Node getContextNode() {
133 return contextNode;
134 }
135 }
136 }