Skip to content

Commit

Permalink
✨🏗 Upgrade closure compiler to v20190301 (#21618)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha authored Apr 10, 2019
1 parent 7b41908 commit 680dd18
Show file tree
Hide file tree
Showing 244 changed files with 1,468 additions and 793 deletions.
4 changes: 0 additions & 4 deletions 3p/3d-gltf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,19 @@ export function gltfViewer(global) {
loadThree(global, () => {
const viewer = new GltfViewer(dataReceived, {
onload: () => {
/** @suppress {deprecated} */
nonSensitiveDataPostMessage('loaded');
},
onprogress: e => {
if (!e.lengthComputable) {
return;
}
/** @suppress {deprecated} */
nonSensitiveDataPostMessage('progress', dict({
'total': e.total,
'loaded': e.loaded,
}));
},
onerror: err => {
user().error('3DGLTF', err);
/** @suppress {deprecated} */
nonSensitiveDataPostMessage('error', dict({
'error': (err || '').toString(),
}));
Expand All @@ -83,7 +80,6 @@ export function gltfViewer(global) {
listenParent(global, 'action', msg => {
viewer.actions[msg['action']](msg['args']);
});
/** @suppress {deprecated} */
nonSensitiveDataPostMessage('ready');
});
}
Binary file modified build-system/runner/dist/runner.jar
Binary file not shown.
29 changes: 19 additions & 10 deletions build-system/runner/src/org/ampproject/AmpCodingConvention.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.ampproject;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.ClosureCodingConvention.AssertFunctionByTypeName;
import com.google.javascript.jscomp.ClosureCodingConvention;
import com.google.javascript.jscomp.CodingConvention;
import com.google.javascript.jscomp.CodingConvention.AssertionFunctionSpec;
import com.google.javascript.jscomp.CodingConventions;
import com.google.javascript.jscomp.ClosureCodingConvention;
import com.google.javascript.jscomp.newtypes.JSType;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative;

import java.util.ArrayList;
Expand Down Expand Up @@ -51,15 +52,23 @@ public AmpCodingConvention(CodingConvention convention) {
super(convention);
}

@Override public Collection<AssertionFunctionSpec> getAssertionFunctions() {
@Override
public ImmutableCollection<AssertionFunctionSpec> getAssertionFunctions() {
return ImmutableList.of(
new AssertionFunctionSpec("module$src$log.devAssert", null),
new AssertionFunctionSpec("devAssert$$module$src$log", null),
new AssertionFunctionSpec("module$src$log.userAssert", null),
new AssertionFunctionSpec("userAssert$$module$src$log", null),
new AssertionFunctionSpec("assertService$$module$src$element_service", null),
new AssertFunctionByTypeName("module$src$layout.assertLength", "string"),
new AssertFunctionByTypeName("assertLength$$module$src$layout", "string")
AssertionFunctionSpec.makeReturnTypeAssertion(
"module$src$log.devAssert"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"devAssert$$module$src$log"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"module$src$log.userAssert"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"userAssert$$module$src$log"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"assertService$$module$src$element_service"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"module$src$layout.assertLength"),
AssertionFunctionSpec.makeReturnTypeAssertion(
"assertLength$$module$src$layout")
);
}

Expand Down
19 changes: 13 additions & 6 deletions build-system/runner/src/org/ampproject/AmpCommandLineRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.javascript.jscomp.CommandLineRunner;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.CustomPassExecutionTime;
import com.google.javascript.jscomp.FlagUsageException;
import com.google.javascript.jscomp.PropertyRenamingPolicy;
import com.google.javascript.jscomp.VariableRenamingPolicy;
import com.google.javascript.rhino.IR;
Expand Down Expand Up @@ -49,11 +50,17 @@ public class AmpCommandLineRunner extends CommandLineRunner {
/**
* List of string suffixes to eliminate from the AST.
*/
ImmutableMap<String, Set<String>> suffixTypes = ImmutableMap.of(
"module$src$log.dev", ImmutableSet.of(
"assert", "fine", "assertElement", "assertString",
"assertNumber", "assertBoolean", "assertArray"),
"module$src$log.user", ImmutableSet.of("fine"));
ImmutableSet<String> suffixTypes = ImmutableSet.of(
"dev$$module$src$log().assert()",
"dev$$module$src$log().fine()",
"dev$$module$src$log().assertElement()",
"dev$$module$src$log().assertString()",
"dev$$module$src$log().assertNumber()",
"dev$$module$src$log().assertArray()",
"dev$$module$src$log().assertBoolean()",
"devAssert$$module$src$log()",
"user$$module$src$log().fine()"
);


ImmutableMap<String, Node> assignmentReplacements = ImmutableMap.of(
Expand All @@ -73,7 +80,7 @@ protected AmpCommandLineRunner(String[] args) {
return createTypeCheckingOptions();
}
CompilerOptions options = super.createOptions();
options.setCollapseProperties(true);
options.setCollapsePropertiesLevel(CompilerOptions.PropertyCollapseLevel.ALL);
AmpPass ampPass = new AmpPass(getCompiler(), is_production_env, suffixTypes,
assignmentReplacements, prodAssignmentReplacements);
options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, ampPass);
Expand Down
93 changes: 40 additions & 53 deletions build-system/runner/src/org/ampproject/AmpPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package org.ampproject;

import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.AbstractCompiler;
import com.google.javascript.jscomp.HotSwapCompilerPass;
import com.google.javascript.jscomp.NodeTraversal;
Expand All @@ -43,14 +45,14 @@
class AmpPass extends AbstractPostOrderCallback implements HotSwapCompilerPass {

final AbstractCompiler compiler;
private final Map<String, Set<String>> stripTypeSuffixes;
private final Map<String, Node> assignmentReplacements;
private final Map<String, Node> prodAssignmentReplacements;
private final ImmutableSet<String> stripTypeSuffixes;
private final ImmutableMap<String, Node> assignmentReplacements;
private final ImmutableMap<String, Node> prodAssignmentReplacements;
final boolean isProd;

public AmpPass(AbstractCompiler compiler, boolean isProd,
Map<String, Set<String>> stripTypeSuffixes,
Map<String, Node> assignmentReplacements, Map<String, Node> prodAssignmentReplacements) {
ImmutableSet<String> stripTypeSuffixes,
ImmutableMap<String, Node> assignmentReplacements, ImmutableMap<String, Node> prodAssignmentReplacements) {
this.compiler = compiler;
this.stripTypeSuffixes = stripTypeSuffixes;
this.isProd = isProd;
Expand All @@ -63,7 +65,7 @@ public AmpPass(AbstractCompiler compiler, boolean isProd,
}

@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) {
NodeTraversal.traverseEs6(compiler, scriptRoot, this);
NodeTraversal.traverse(compiler, scriptRoot, this);
}

@Override public void visit(NodeTraversal t, Node n, Node parent) {
Expand Down Expand Up @@ -166,59 +168,15 @@ private Node getAmpExtensionCallback(Node n) {
}

/**
* For a function that looks like:
* function fun(val) {
* return dev().assert(val);
* }
*
* The AST would look like:
* RETURN 24 [length: 25] [source_file: ./src/main.js]
* CALL 24 [length: 17] [source_file: ./src/main.js]
* GETPROP 24 [length: 12] [source_file: ./src/main.js]
* CALL 24 [length: 5] [source_file: ./src/main.js]
* NAME $dev$$module$src$log$$ 38 [length: 3] [originalname: dev] [source_file: ./src/log.js]
* STRING assert 24 [length: 6] [source_file: ./src/main.js]
* NAME $val$$ 24 [length: 3] [source_file: ./src/main.js]
*
* We are looking for the `CALL` that has a child NAME "$dev$$module$src$log$$" (or any signature from keys)
* and a child STRING "assert" (or any other signature from Set<String> value)
*/
private boolean isCallRemovable(Node n) {
if (n == null || !n.isCall()) {
return false;
}

Node callGetprop = n.getFirstChild();
if (callGetprop == null || !callGetprop.isGetProp()) {
return false;
}

// Temp hack not needed in single pass compiler because it can inline the
// this itself.
if ("module$src$log.devAssert".equals(callGetprop.getQualifiedName())) {
return true;
}

Node parentCall = callGetprop.getFirstChild();
if (parentCall == null || !parentCall.isCall()) {
return false;
}

Node parentCallGetprop = parentCall.getFirstChild();
Node methodName = parentCall.getNext();
if (parentCallGetprop == null || !parentCallGetprop.isGetProp() ||
methodName == null || !methodName.isString()) {
return false;
}

String parentMethodName = parentCallGetprop.getQualifiedName();
Set<String> methodCallNames = stripTypeSuffixes.get(parentMethodName);
if (methodCallNames == null) {
return false;
}

for (String methodCallName : methodCallNames) {
if (methodCallName == methodName.getString()) {
String name = buildQualifiedName(n);
for (String removable : stripTypeSuffixes) {
if (name.equals(removable)) {
return true;
}
}
Expand Down Expand Up @@ -278,6 +236,35 @@ private boolean isFunctionInvokeAndPropAccess(Node n, String fnQualifiedName, Se
return false;
}

/**
* Builds a string representation of MemberExpression and CallExpressions.
*/
private String buildQualifiedName(Node n) {
StringBuilder sb = new StringBuilder();
buildQualifiedNameInternal(n, sb);
return sb.toString();
}

private void buildQualifiedNameInternal(Node n, StringBuilder sb) {
if (n == null) {
sb.append("NULL");
return;
}

if (n.isCall()) {
buildQualifiedNameInternal(n.getFirstChild(), sb);
sb.append("()");
} else if (n.isGetProp()) {
buildQualifiedNameInternal(n.getFirstChild(), sb);
sb.append(".");
buildQualifiedNameInternal(n.getSecondChild(), sb);
} else if (n.isName() || n.isString()) {
sb.append(n.getString());
} else {
sb.append("UNKNOWN");
}
}

private void replaceWithBooleanExpression(boolean bool, Node n, Node parent) {
Node booleanNode = bool ? IR.trueNode() : IR.falseNode();
booleanNode.useSourceInfoIfMissingFrom(n);
Expand Down
Loading

0 comments on commit 680dd18

Please sign in to comment.