Skip to content

Commit 8486670

Browse files
kevinoconnor7copybara-github
authored andcommitted
Avoiding rerunning ProcessDefines in J2clUtilGetDefineRewriterPass
The way `J2clUtilGetDefineRewriterPass` currently works is pretty messy as the rerun of `ProcessDefines` does not work as one would expect. When we actually rerun it all of the `goog.define` calls have already been removed, so the define name is actually based on the name of the node annotated with `@define`. Instead of doing this we can have `ProcessDefines` stash the name of all the defines that it found. That's the only information that `J2clUtilGetDefineRewriterPass` needs to operate, and no new defines are going to be added along the way. This does not change the requirement that the J2CL requires the define name to match a `goog.provide` of the same name. Resolving that problem is quite a bit more involved. For now this change just improves the improves the performance and reduces the brittleness of the existing solution. PiperOrigin-RevId: 700492292
1 parent b5097d0 commit 8486670

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

src/com/google/javascript/jscomp/AbstractCompiler.java

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.Serializable;
4747
import java.nio.file.Path;
4848
import java.util.ArrayList;
49+
import java.util.Collection;
4950
import java.util.EnumSet;
5051
import java.util.List;
5152
import java.util.Set;
@@ -116,6 +117,11 @@ void afterPass(String passName) {}
116117
/** Gets the names that have been exported. */
117118
public abstract Set<String> getExportedNames();
118119

120+
/** Adds @define names to keep track. */
121+
public abstract void setDefineNames(Collection<String> defineNames);
122+
123+
public abstract ImmutableSet<String> getDefineNames();
124+
119125
/** Sets the variable renaming map */
120126
public abstract void setVariableMap(VariableMap variableMap);
121127

src/com/google/javascript/jscomp/Compiler.java

+13
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,9 @@ public SourceMap getSourceMap() {
36103610
/** Names exported by goog.exportSymbol. */
36113611
private final LinkedHashSet<String> exportedNames = new LinkedHashSet<>();
36123612

3613+
/** Names defined by goog.define. */
3614+
private ImmutableSet<String> defineNames = ImmutableSet.of();
3615+
36133616
@Override
36143617
public void setVariableMap(VariableMap variableMap) {
36153618
this.variableMap = variableMap;
@@ -3685,6 +3688,16 @@ public Set<String> getExportedNames() {
36853688
return exportedNames;
36863689
}
36873690

3691+
@Override
3692+
public void setDefineNames(Collection<String> defineNames) {
3693+
this.defineNames = ImmutableSet.copyOf(defineNames);
3694+
}
3695+
3696+
@Override
3697+
public ImmutableSet<String> getDefineNames() {
3698+
return defineNames;
3699+
}
3700+
36883701
@Override
36893702
public CompilerOptions getOptions() {
36903703
return options;

src/com/google/javascript/jscomp/DefaultPassConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ private void assertValidOrderForOptimizations(PassListBuilder optimizations) {
13061306
processDefinesOptimize,
13071307
j2clUtilGetDefineRewriterPass,
13081308
"J2CL define re-writing should be done after processDefines since it relies on "
1309-
+ "collectDefines which has side effects.");
1309+
+ "Compiler#getDefineNames to have been populated by it.");
13101310

13111311
optimizations.assertPassOrder(
13121312
removeUnusedCode,

src/com/google/javascript/jscomp/J2clUtilGetDefineRewriterPass.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ public void process(Node externs, Node root) {
3535
if (!J2clSourceFileChecker.shouldRunJ2clPasses(compiler)) {
3636
return;
3737
}
38-
defines =
39-
new ProcessDefines.Builder(compiler)
40-
.setMode(ProcessDefines.Mode.OPTIMIZE)
41-
.build()
42-
.collectDefineNames(externs, root);
38+
defines = compiler.getDefineNames();
4339
NodeTraversal.traverse(compiler, root, this);
4440
}
4541

src/com/google/javascript/jscomp/ProcessDefines.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ public void process(Node externs, Node root) {
208208
this.overrideDefines();
209209
}
210210

211-
final ImmutableSet<String> collectDefineNames(Node externs, Node root) {
212-
this.initNamespace(externs, root);
213-
this.collectDefines(root);
214-
215-
return ImmutableSet.copyOf(this.defineByDefineName.keySet());
216-
}
217-
218211
private void initNamespace(Node externs, Node root) {
219212
if (namespaceSupplier != null) {
220213
this.namespace = namespaceSupplier.get();
@@ -352,6 +345,7 @@ private void collectDefines(Node root) {
352345
}
353346
}
354347
}
348+
compiler.setDefineNames(defineByDefineName.keySet());
355349
}
356350

357351
private @Nullable Ref selectDefineDeclaration(Name name) {

test/com/google/javascript/jscomp/J2clUtilGetDefineRewriterPassTest.java

+27-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.javascript.jscomp;
1717

18+
import com.google.javascript.jscomp.ProcessDefines.Mode;
1819
import org.junit.Test;
1920
import org.junit.runner.RunWith;
2021
import org.junit.runners.JUnit4;
@@ -25,7 +26,12 @@ public class J2clUtilGetDefineRewriterPassTest extends CompilerTestCase {
2526

2627
@Override
2728
protected CompilerPass getProcessor(final Compiler compiler) {
28-
return new J2clUtilGetDefineRewriterPass(compiler);
29+
return (externs, root) -> {
30+
// We require the ProcessDefines pass to run first, so run it in optimize mode so that we
31+
// replicate the state we expect the code to be in when it reaches this pass.
32+
new ProcessDefines.Builder(compiler).setMode(Mode.OPTIMIZE).build().process(externs, root);
33+
new J2clUtilGetDefineRewriterPass(compiler).process(externs, root);
34+
};
2935
}
3036

3137
@Override
@@ -37,13 +43,28 @@ protected Compiler createCompiler() {
3743

3844
@Test
3945
public void testUtilGetDefine() {
40-
String defineAbc = "var a={}; a.b={}; /** @define {boolean} */ a.b.c = true;\n";
4146
test(
42-
defineAbc + "nativebootstrap.Util.$getDefine('a.b.c', 'def');",
43-
defineAbc + "('def', String(a.b.c));");
47+
lines(
48+
"var a = {};",
49+
"a.b = {}",
50+
"/** @define {boolean} */ a.b.c = goog.define('a.b.c', true);",
51+
"nativebootstrap.Util.$getDefine('a.b.c', 'def');"),
52+
lines(
53+
"var a = {};",
54+
"a.b = {}",
55+
"/** @define {boolean} */ a.b.c = true;",
56+
"('def', String(a.b.c));"));
4457
test(
45-
defineAbc + "nativebootstrap.Util.$getDefine('a.b.c');",
46-
defineAbc + "(null, String(a.b.c));");
58+
lines(
59+
"var a = {};",
60+
"a.b = {}",
61+
"/** @define {boolean} */ a.b.c = goog.define('a.b.c', true);",
62+
"nativebootstrap.Util.$getDefine('a.b.c');"),
63+
lines(
64+
"var a = {};",
65+
"a.b = {}",
66+
"/** @define {boolean} */ a.b.c = true;",
67+
"(null, String(a.b.c));"));
4768
}
4869

4970
@Test

0 commit comments

Comments
 (0)