-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom closure compiler pass (#2593)
* changes to third_party/closure-compiler * add custom closure compiler pass
- Loading branch information
1 parent
91c973c
commit 656c3ec
Showing
15 changed files
with
929 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry including="**/*.java" kind="src" path="src"/> | ||
<classpathentry including="**/*.java" kind="src" output="build/test-classes" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="lib" path="lib/compiler.jar" sourcepath="/Users/erwinm/dev/java/closure-compiler"/> | ||
<classpathentry kind="lib" path="./../../third_party/closure-compiler/compiler.jar"/> | ||
<classpathentry kind="lib" path="./../../third_party/closure-compiler/compiler-and-tests.jar"/> | ||
<classpathentry kind="output" path="build/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file renamed
BIN
+5.6 MB
build-system/runner/build/runner.jar → build-system/runner/dist/runner.jar
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> | ||
|
19 changes: 17 additions & 2 deletions
19
build-system/runner/src/org/ampproject/AmpCommandLineRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.ampproject; | ||
|
||
import java.util.Set; | ||
|
||
import com.google.javascript.jscomp.AbstractCompiler; | ||
import com.google.javascript.jscomp.HotSwapCompilerPass; | ||
import com.google.javascript.jscomp.NodeTraversal; | ||
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; | ||
import com.google.javascript.rhino.Node; | ||
|
||
/** | ||
* Does a `stripTypeSuffix` which currently can't be done through | ||
* the normal `strip` mechanisms provided by closure compiler. | ||
* Some of the known mechanisms we tried before writing our own compiler pass | ||
* are setStripTypes, setStripTypePrefixes, setStripNameSuffixes, setStripNamePrefixes. | ||
* The normal mechanisms found in closure compiler can't strip the expressions we want because | ||
* they are either prefix based and/or operate on the es6 translated code which would mean they | ||
* operate on a qualifier string name that looks like | ||
* "module$__$__$__$extensions$amp_test$0_1$log.dev.fine". | ||
* | ||
* Other custom pass examples found inside closure compiler src: | ||
* https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PolymerPass.java | ||
* https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/AngularPass.java | ||
*/ | ||
class AmpPass extends AbstractPostOrderCallback implements HotSwapCompilerPass { | ||
|
||
final AbstractCompiler compiler; | ||
private final Set<String> stripTypeSuffixes; | ||
|
||
public AmpPass(AbstractCompiler compiler, Set<String> stripTypeSuffixes) { | ||
this.compiler = compiler; | ||
this.stripTypeSuffixes = stripTypeSuffixes; | ||
} | ||
|
||
@Override public void process(Node externs, Node root) { | ||
hotSwapScript(root, null); | ||
} | ||
|
||
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { | ||
NodeTraversal.traverseEs6(compiler, scriptRoot, this); | ||
} | ||
|
||
@Override public void visit(NodeTraversal t, Node n, Node parent) { | ||
if (isDevAssertCall(n)) { | ||
maybeEliminateCallExceptFirstParam(n, parent); | ||
} else if (n.isExprResult()) { | ||
maybeEliminateExpressionBySuffixName(n, parent); | ||
} | ||
} | ||
|
||
private boolean isDevAssertCall(Node n) { | ||
if (n.isCall()) { | ||
Node expression = n.getFirstChild(); | ||
if (expression == null) { | ||
return false; | ||
} | ||
|
||
String name = expression.getQualifiedName(); | ||
if (name == null) { | ||
return false; | ||
} | ||
|
||
if (name.endsWith("dev.assert")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Checks if expression is a GETPROP() (method invocation) and the property | ||
* name ends with one of the items in stripTypeSuffixes. | ||
*/ | ||
private void maybeEliminateExpressionBySuffixName(Node n, Node parent) { | ||
// n = EXPRESSION_RESULT > CALL > GETPROP | ||
Node call = n.getFirstChild(); | ||
if (call == null) { | ||
return; | ||
} | ||
Node expression = call.getFirstChild(); | ||
if (expression == null) { | ||
return; | ||
} | ||
|
||
if (qualifiedNameEndsWithStripType(expression)) { | ||
if (parent.isExprResult()) { | ||
Node grandparent = parent.getParent(); | ||
grandparent.removeChild(parent); | ||
} else { | ||
parent.removeChild(n); | ||
} | ||
compiler.reportCodeChange(); | ||
} | ||
} | ||
|
||
private void maybeEliminateCallExceptFirstParam(Node n, Node p) { | ||
Node functionName = n.getFirstChild(); | ||
if (functionName == null) { | ||
return; | ||
} | ||
Node firstArg = functionName.getNext(); | ||
if (firstArg == null) { | ||
return; | ||
} | ||
firstArg.detachFromParent(); | ||
p.replaceChild(n, firstArg); | ||
compiler.reportCodeChange(); | ||
} | ||
|
||
/** | ||
* Checks the nodes qualified name if it ends with one of the items in | ||
* stripTypeSuffixes | ||
*/ | ||
boolean qualifiedNameEndsWithStripType(Node n) { | ||
String name = n.getQualifiedName(); | ||
return qualifiedNameEndsWithStripType(name); | ||
} | ||
|
||
/** | ||
* Checks if the string ends with one of the items in stripTypeSuffixes | ||
*/ | ||
boolean qualifiedNameEndsWithStripType(String name) { | ||
if (name != null) { | ||
for (String suffix : stripTypeSuffixes) { | ||
if (name.endsWith(suffix)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.