| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.base.Preconditions; |
| 19 |
import com.google.javascript.rhino.IR; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
import java.util.regex.Pattern; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class ExportTestFunctions implements CompilerPass { |
| 29 |
|
| 30 |
private static final Pattern TEST_FUNCTIONS_NAME_PATTERN = |
| 31 |
Pattern.compile("^(?:((\\w+\\.)+prototype\\.)*" + |
| 32 |
"(setUpPage|setUp|tearDown|tearDownPage|test\\w+))$"); |
| 33 |
|
| 34 |
private AbstractCompiler compiler; |
| 35 |
private final String exportSymbolFunction; |
| 36 |
private final String exportPropertyFunction; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ExportTestFunctions(AbstractCompiler compiler, |
| 46 |
String exportSymbolFunction, String exportPropertyFunction) { |
| 47 |
|
| 48 |
Preconditions.checkNotNull(compiler); |
| 49 |
this.compiler = compiler; |
| 50 |
this.exportSymbolFunction = exportSymbolFunction; |
| 51 |
this.exportPropertyFunction = exportPropertyFunction; |
| 52 |
} |
| 53 |
|
| 54 |
private class ExportTestFunctionsNodes extends |
| 55 |
NodeTraversal.AbstractShallowCallback { |
| 56 |
|
| 57 |
@Override |
| 58 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 59 |
|
| 60 |
if (parent == null) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if (parent.isScript()) { |
| 65 |
if (NodeUtil.isFunctionDeclaration(n)) { |
| 66 |
|
| 67 |
String functionName = NodeUtil.getFunctionName(n); |
| 68 |
if (isTestFunction(n, functionName)) { |
| 69 |
exportTestFunctionAsSymbol(functionName, n, parent); |
| 70 |
} |
| 71 |
} else if (isVarDeclaredFunction(n)) { |
| 72 |
|
| 73 |
Node functionNode = n.getFirstChild().getFirstChild(); |
| 74 |
String functionName = NodeUtil.getFunctionName(functionNode); |
| 75 |
if (isTestFunction(functionNode, functionName)) { |
| 76 |
exportTestFunctionAsSymbol(functionName, n, parent); |
| 77 |
} |
| 78 |
} |
| 79 |
} else if (NodeUtil.isExprAssign(parent) && |
| 80 |
!n.getLastChild().isAssign()) { |
| 81 |
|
| 82 |
Node grandparent = parent.getParent(); |
| 83 |
if (grandparent != null && grandparent.isScript()) { |
| 84 |
String functionName = n.getFirstChild().getQualifiedName(); |
| 85 |
if (isTestFunction(n, functionName)) { |
| 86 |
exportTestFunctionAsProperty(functionName, parent, n, grandparent); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private boolean isVarDeclaredFunction(Node node) { |
| 104 |
if (!node.isVar()) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
Node grandchild = node.getFirstChild().getFirstChild(); |
| 108 |
return grandchild != null && grandchild.isFunction(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void process(Node externs, Node root) { |
| 114 |
NodeTraversal.traverse(compiler, root, new ExportTestFunctionsNodes()); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
private void exportTestFunctionAsSymbol(String testFunctionName, Node node, |
| 119 |
Node scriptNode) { |
| 120 |
|
| 121 |
Node exportCallTarget = NodeUtil.newQualifiedNameNode( |
| 122 |
compiler.getCodingConvention(), |
| 123 |
exportSymbolFunction, node, testFunctionName); |
| 124 |
Node call = IR.call( exportCallTarget); |
| 125 |
if (exportCallTarget.isName()) { |
| 126 |
call.putBooleanProp(Node.FREE_CALL, true); |
| 127 |
} |
| 128 |
call.addChildToBack(IR.string(testFunctionName)); |
| 129 |
call.addChildToBack(NodeUtil.newQualifiedNameNode( |
| 130 |
compiler.getCodingConvention(), |
| 131 |
testFunctionName, node, testFunctionName)); |
| 132 |
|
| 133 |
Node expression = IR.exprResult(call); |
| 134 |
|
| 135 |
scriptNode.addChildAfter(expression, node); |
| 136 |
compiler.reportCodeChange(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private void exportTestFunctionAsProperty(String fullyQualifiedFunctionName, |
| 142 |
Node parent, Node node, Node scriptNode) { |
| 143 |
|
| 144 |
String testFunctionName = |
| 145 |
NodeUtil.getPrototypePropertyName(node.getFirstChild()); |
| 146 |
String objectName = fullyQualifiedFunctionName.substring(0, |
| 147 |
fullyQualifiedFunctionName.lastIndexOf('.')); |
| 148 |
String exportCallStr = String.format("%s(%s, '%s', %s);", |
| 149 |
exportPropertyFunction, objectName, testFunctionName, |
| 150 |
fullyQualifiedFunctionName); |
| 151 |
|
| 152 |
Node exportCall = this.compiler.parseSyntheticCode(exportCallStr) |
| 153 |
.removeChildren(); |
| 154 |
exportCall.useSourceInfoFromForTree(scriptNode); |
| 155 |
|
| 156 |
scriptNode.addChildAfter(exportCall, parent); |
| 157 |
compiler.reportCodeChange(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private boolean isTestFunction(Node n, String functionName) { |
| 171 |
return !(functionName == null |
| 172 |
|| !TEST_FUNCTIONS_NAME_PATTERN.matcher(functionName).matches()); |
| 173 |
} |
| 174 |
} |