| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 123 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private String getPropertyName(Node node) { |
| 153 |
Preconditions.checkArgument(node.isGetProp()); |
| 154 |
return node.getLastChild().getString(); |
| 155 |
} |
| 156 |
} |