Debian Codegraph @

src/com/google/javascript/jscomp/GenerateExports.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.base.Preconditions;
20 import com.google.javascript.jscomp.FindExportableNodes.GenerateNodeContext;
21 import com.google.javascript.rhino.IR;
22 import com.google.javascript.rhino.Node;
23 import java.util.Map;
24
25 /**
26 * Generates goog.exportSymbol/goog.exportProperty for the @export annotation.
27 *
28 */
29 class GenerateExports implements CompilerPass {
30
31 private static final String PROTOTYPE_PROPERTY = "prototype";
32
33 private final AbstractCompiler compiler;
34
35 private final String exportSymbolFunction;
36
37 private final String exportPropertyFunction;
38
39 /**
40 * Creates a new generate exports compiler pass.
41 * @param compiler JS compiler.
42 * @param exportSymbolFunction function used for exporting symbols.
43 * @param exportPropertyFunction function used for exporting property names.
44 */
45 GenerateExports(AbstractCompiler compiler, String exportSymbolFunction,
46 String exportPropertyFunction) {
47 Preconditions.checkNotNull(compiler);
48 Preconditions.checkNotNull(exportSymbolFunction);
49 Preconditions.checkNotNull(exportPropertyFunction);
50
51 this.compiler = compiler;
52 this.exportSymbolFunction = exportSymbolFunction;
53 this.exportPropertyFunction = exportPropertyFunction;
54 }
55
56 @Override
57 public void process(Node externs, Node root) {
58 FindExportableNodes findExportableNodes = new FindExportableNodes(compiler);
59 NodeTraversal.traverse(compiler, root, findExportableNodes);
60 Map<String, GenerateNodeContext> exports = findExportableNodes
61 .getExports();
62
63 CodingConvention convention = compiler.getCodingConvention();
64 for (Map.Entry<String, GenerateNodeContext> entry : exports.entrySet()) {
65 String export = entry.getKey();
66 GenerateNodeContext context = entry.getValue();
67
68 // Emit the proper CALL expression.
69 // This is an optimization to avoid exporting everything as a symbol
70 // because exporting a property is significantly simpler/faster.
71 // Only export the property if the parent is being exported or
72 // if the parent is "prototype" and the grandparent is being exported.
73 String parent = null;
74 String grandparent = null;
75
76 Node node = context.getNode().getFirstChild();
77 if (node.isGetProp()) {
78 parent = node.getFirstChild().getQualifiedName();
79 if (node.getFirstChild().isGetProp() &&
80 getPropertyName(node.getFirstChild()).equals(PROTOTYPE_PROPERTY)) {
81 grandparent = node.getFirstChild().getFirstChild().getQualifiedName();
82 }
83 }
84
85 boolean useExportSymbol = true;
86 if (grandparent != null && exports.containsKey(grandparent)) {
87 useExportSymbol = false;
88 } else if (parent != null && exports.containsKey(parent)) {
89 useExportSymbol = false;
90 }
91
92 Node call;
93 if (useExportSymbol) {
94 // exportSymbol(publicPath, object);
95 call = IR.call(
96 NodeUtil.newQualifiedNameNode(
97 convention, exportSymbolFunction,
98 context.getNode(), export),
99 IR.string(export),
100 NodeUtil.newQualifiedNameNode(
101 convention, export,
102 context.getNode(), export));
103 } else {
104 // exportProperty(object, publicName, symbol);
105 String property = getPropertyName(node);
106 call = IR.call(
107 NodeUtil.newQualifiedNameNode(
108 convention, exportPropertyFunction,
109 context.getNode(), exportPropertyFunction),
110 NodeUtil.newQualifiedNameNode(
111 convention, parent,
112 context.getNode(), exportPropertyFunction),
113 IR.string(property),
114 NodeUtil.newQualifiedNameNode(
115 convention, export,
116 context.getNode(), exportPropertyFunction));
117 }
118
119 Node expression = IR.exprResult(call);
120 annotate(expression);
121
122 // It's important that any class-building calls (goog.inherits)
123 // come right after the class definition, so move the export after that.
124 Node insertionPoint = context.getContextNode().getNext();
125 while (insertionPoint != null &&
126 NodeUtil.isExprCall(insertionPoint) &&
127 convention.getClassesDefinedByCall(
128 insertionPoint.getFirstChild()) != null) {
129 insertionPoint = insertionPoint.getNext();
130 }
131
132 if (insertionPoint == null) {
133 context.getScriptNode().addChildToBack(expression);
134 } else {
135 context.getScriptNode().addChildBefore(expression, insertionPoint);
136 }
137 compiler.reportCodeChange();
138 }
139 }
140
141 private void annotate(Node node) {
142 NodeTraversal.traverse(
143 compiler, node, new PrepareAst.PrepareAnnotations());
144 }
145
146 /**
147 * Assumes the node type is correct and returns the property name
148 * (not fully qualified).
149 * @param node node
150 * @return property name.
151 */
152 private String getPropertyName(Node node) {
153 Preconditions.checkArgument(node.isGetProp());
154 return node.getLastChild().getString();
155 }
156 }