Skip to content

Commit 56ae527

Browse files
Closure Teamcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 483712521
1 parent 8bb3809 commit 56ae527

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public final class GatherModuleMetadata implements CompilerPass {
6969
DiagnosticType.error(
7070
"JSC_INVALID_REQUIRE_TYPE", "Argument to goog.requireType must be a string.");
7171

72+
static final DiagnosticType INVALID_REQUIRE_DYNAMIC =
73+
DiagnosticType.error(
74+
"JSC_INVALID_REQUIRE_DYNAMIC", "Argument to goog.requireDynamic must be a string.");
75+
7276
static final DiagnosticType INVALID_SET_TEST_ONLY =
7377
DiagnosticType.error(
7478
"JSC_INVALID_SET_TEST_ONLY",
@@ -81,6 +85,7 @@ public final class GatherModuleMetadata implements CompilerPass {
8185
private static final Node GOOG_MODULE = IR.getprop(IR.name("goog"), "module");
8286
private static final Node GOOG_REQUIRE = IR.getprop(IR.name("goog"), "require");
8387
private static final Node GOOG_REQUIRE_TYPE = IR.getprop(IR.name("goog"), "requireType");
88+
private static final Node GOOG_REQUIRE_DYNAMIC = IR.getprop(IR.name("goog"), "requireDynamic");
8489
private static final Node GOOG_SET_TEST_ONLY = IR.getprop(IR.name("goog"), "setTestOnly");
8590
private static final Node GOOG_MODULE_DECLARELEGACYNAMESPACE =
8691
IR.getprop(GOOG_MODULE.cloneTree(), "declareLegacyNamespace");
@@ -429,6 +434,15 @@ private void visitGoogCall(NodeTraversal t, Node n) {
429434
} else {
430435
t.report(n, INVALID_SET_TEST_ONLY);
431436
}
437+
} else if (getprop.matchesQualifiedName(GOOG_REQUIRE_DYNAMIC)) {
438+
if (n.hasTwoChildren() && n.getLastChild().isStringLit()) {
439+
currentModule
440+
.metadataBuilder
441+
.dynamicallyRequiredGoogNamespacesBuilder()
442+
.add(n.getLastChild().getString());
443+
} else {
444+
t.report(n, INVALID_REQUIRE_DYNAMIC);
445+
}
432446
}
433447
}
434448

src/com/google/javascript/jscomp/deps/JsFileFullParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public enum ModuleType {
7777
public final Set<String> deltemplates = new TreeSet<>();
7878
// Use a LinkedHashSet as import order matters!
7979
public final Set<String> importedModules = new LinkedHashSet<>();
80+
public final Set<String> dynamicRequires = new LinkedHashSet<>();
8081
public final List<String> modName = new ArrayList<>();
8182
public final List<String> mods = new ArrayList<>();
8283

@@ -98,8 +99,8 @@ private static class CommentAnnotation {
9899
/** Annotation name, e.g. "@fileoverview" or "@externs". */
99100
final String name;
100101
/**
101-
* Annotation value: either the bare identifier immediately after the
102-
* annotation, or else string in braces.
102+
* Annotation value: either the bare identifier immediately after the annotation, or else string
103+
* in braces.
103104
*/
104105
final String value;
105106

@@ -211,8 +212,7 @@ protected void printSummary() {}
211212
return info; // Avoid potential crashes due to assumptions of the code below being violated.
212213
}
213214
ModuleMetadata module =
214-
Iterables.getOnlyElement(
215-
compiler.getModuleMetadataMap().getModulesByPath().values());
215+
Iterables.getOnlyElement(compiler.getModuleMetadataMap().getModulesByPath().values());
216216
if (module.isEs6Module()) {
217217
info.loadFlags.put("module", "es6");
218218
} else if (module.isGoogModule()) {
@@ -250,6 +250,7 @@ private static void recordModuleMetadata(FileInfo info, ModuleMetadata module) {
250250
if (module.usesClosure()) {
251251
info.provides.addAll(module.googNamespaces());
252252
info.requires.addAll(module.stronglyRequiredGoogNamespaces());
253+
info.dynamicRequires.addAll(module.dynamicallyRequiredGoogNamespaces());
253254
info.typeRequires.addAll(module.weaklyRequiredGoogNamespaces());
254255
info.testonly = module.isTestOnly();
255256
}

src/com/google/javascript/jscomp/modules/ModuleMetadataMap.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public boolean isModule() {
175175
*/
176176
public abstract ImmutableMultiset<String> stronglyRequiredGoogNamespaces();
177177

178+
/**
179+
* Closure namespaces this file dynamically require, i.e., arguments to goog.requireDynamic()
180+
* calls.
181+
*
182+
* <p>This is a multiset as it does not warn on duplicate namespaces, but will still encapsulate
183+
* that information with this multiset.
184+
*/
185+
public abstract ImmutableMultiset<String> dynamicallyRequiredGoogNamespaces();
186+
178187
/**
179188
* Closure namespaces this file weakly requires, i.e., arguments to goog.requireType calls.
180189
*
@@ -222,6 +231,8 @@ public Builder addGoogNamespace(String namespace) {
222231

223232
public abstract ImmutableMultiset.Builder<String> stronglyRequiredGoogNamespacesBuilder();
224233

234+
public abstract ImmutableMultiset.Builder<String> dynamicallyRequiredGoogNamespacesBuilder();
235+
225236
public abstract ImmutableMultiset.Builder<String> weaklyRequiredGoogNamespacesBuilder();
226237

227238
public abstract ImmutableMultiset.Builder<String> es6ImportSpecifiersBuilder();

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,23 @@ public void testRequireType() {
573573
assertThat(m.weaklyRequiredGoogNamespaces()).containsExactly("my.Type");
574574
}
575575

576+
@Test
577+
public void testRequireDynamic() {
578+
testSame("async function test() {await goog.requireDynamic('my.Type');}");
579+
ModuleMetadata m = metadataMap().getModulesByPath().get("testcode");
580+
assertThat(m.dynamicallyRequiredGoogNamespaces()).containsExactly("my.Type");
581+
}
582+
583+
@Test
584+
public void testRequireDynamicWithIllegalArg() {
585+
testError(
586+
"async function test() {await goog.requireDynamic('my.Type','extra.Type');}",
587+
GatherModuleMetadata.INVALID_REQUIRE_DYNAMIC);
588+
testError(
589+
"async function test() {await goog.requireDynamic(42);}",
590+
GatherModuleMetadata.INVALID_REQUIRE_DYNAMIC);
591+
}
592+
576593
@Test
577594
public void testRequiredClosureNamespaces() {
578595
testSame("goog.require('my.Type');");

test/com/google/javascript/jscomp/deps/JsFileFullParserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ public void testProvidesRequires() {
8282
parse(
8383
"goog.provide('providedSymbol');",
8484
"goog.require('stronglyRequiredSymbol');",
85-
"goog.requireType('weaklyRequiredSymbol');");
85+
"goog.requireType('weaklyRequiredSymbol');",
86+
"async function test() {await goog.requireDynamic('dynamicallyRequiredSymbol');}");
8687

8788
assertThat(info.provides).containsExactly("providedSymbol");
8889
assertThat(info.requires).containsExactly("stronglyRequiredSymbol");
8990
assertThat(info.typeRequires).containsExactly("weaklyRequiredSymbol");
91+
assertThat(info.dynamicRequires).containsExactly("dynamicallyRequiredSymbol");
9092
}
9193

9294
@Test

0 commit comments

Comments
 (0)