Debian Codegraph @

src/com/google/javascript/jscomp/ExportTestFunctions.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 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 * Generates goog.exportSymbol for test functions, so they can be recognized
25 * by the test runner, even if the code is compiled.
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 * Creates a new export test functions compiler pass.
40 * @param compiler
41 * @param exportSymbolFunction The function name used to export symbols in JS.
42 * @param exportPropertyFunction The function name used to export properties
43 * in JS.
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 // Check for a test function statement.
67 String functionName = NodeUtil.getFunctionName(n);
68 if (isTestFunction(n, functionName)) {
69 exportTestFunctionAsSymbol(functionName, n, parent);
70 }
71 } else if (isVarDeclaredFunction(n)) {
72 // Check for a test function expression.
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 // Check for a test method assignment.
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 * Whether node corresponds to a function expression declared with var,
94 * which is of the form:
95 * <pre>
96 * var functionName = function() {
97 * // Implementation
98 * };
99 * </pre>
100 * This has the AST structure VAR -> NAME -> FUNCTION
101 * @param node
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 // Adds exportSymbol(testFunctionName, testFunction);
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 // Adds exportProperty() of the test function name on the prototype object
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 * Whether a function is recognized as a test function. We follow the JsUnit
163 * convention for naming (functions should start with "test"), and we also
164 * check if it has no parameters declared.
165 *
166 * @param n The function node
167 * @param functionName The name of the function
168 * @return {@code true} if the function is recognized as a test function.
169 */
170 private boolean isTestFunction(Node n, String functionName) {
171 return !(functionName == null
172 || !TEST_FUNCTIONS_NAME_PATTERN.matcher(functionName).matches());
173 }
174 }