Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 542933491
  • Loading branch information
shicks authored and copybara-github committed Jun 23, 2023
1 parent a685a90 commit 1beb766
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/com/google/javascript/jscomp/GatherModuleMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public final class GatherModuleMetadata implements CompilerPass {
static final DiagnosticType INVALID_NESTED_LOAD_MODULE =
DiagnosticType.error("JSC_INVALID_NESTED_LOAD_MODULE", "goog.loadModule cannot be nested.");

static final DiagnosticType INVALID_READ_TOGGLE =
DiagnosticType.error(
"JSC_INVALID_READ_TOGGLE",
"Argument to goog.readToggleInternalDoNotCallDirectly must be a string.");

private static final Node GOOG_PROVIDE = IR.getprop(IR.name("goog"), "provide");
private static final Node GOOG_MODULE = IR.getprop(IR.name("goog"), "module");
private static final Node GOOG_REQUIRE = IR.getprop(IR.name("goog"), "require");
Expand All @@ -90,6 +95,8 @@ public final class GatherModuleMetadata implements CompilerPass {
private static final Node GOOG_MODULE_DECLARELEGACYNAMESPACE =
IR.getprop(GOOG_MODULE.cloneTree(), "declareLegacyNamespace");
private static final Node GOOG_DECLARE_MODULE_ID = IR.getprop(IR.name("goog"), "declareModuleId");
private static final Node GOOG_READ_TOGGLE =
IR.getprop(IR.name("goog"), "readToggleInternalDoNotCallDirectly");

// TODO(johnplaisted): Remove once clients have migrated to declareModuleId
private static final Node GOOG_MODULE_DECLARNAMESPACE =
Expand Down Expand Up @@ -445,6 +452,12 @@ private void visitGoogCall(NodeTraversal t, Node n) {
} else {
t.report(n, INVALID_REQUIRE_DYNAMIC);
}
} else if (getprop.matchesQualifiedName(GOOG_READ_TOGGLE)) {
if (n.hasTwoChildren() && n.getLastChild().isStringLit()) {
currentModule.metadataBuilder.readTogglesBuilder().add(n.getLastChild().getString());
} else {
t.report(n, INVALID_READ_TOGGLE);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/deps/JsFileFullParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public enum ModuleType {
// Use a LinkedHashSet as import order matters!
public final Set<String> importedModules = new LinkedHashSet<>();
public final Set<String> dynamicRequires = new LinkedHashSet<>();
public final Set<String> readToggles = new LinkedHashSet<>();
public final List<String> modName = new ArrayList<>();
public final List<String> mods = new ArrayList<>();

Expand Down Expand Up @@ -251,6 +252,7 @@ private static void recordModuleMetadata(FileInfo info, ModuleMetadata module) {
info.provides.addAll(module.googNamespaces());
info.requires.addAll(module.stronglyRequiredGoogNamespaces());
info.dynamicRequires.addAll(module.dynamicallyRequiredGoogNamespaces().elementSet());
info.readToggles.addAll(module.readToggles().elementSet());
info.typeRequires.addAll(module.weaklyRequiredGoogNamespaces());
info.testonly = module.isTestOnly();
}
Expand Down
10 changes: 10 additions & 0 deletions src/com/google/javascript/jscomp/modules/ModuleMetadataMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ public boolean isModule() {

public abstract ImmutableList<ModuleMetadata> nestedModules();

/**
* Arguments to goog.readToggleInternalDoNotCallDirectly() calls.
*
* <p>This is a multiset as it does not warn on duplicate toggles, but will still encapsulate
* that information with this multiset.
*/
public abstract ImmutableMultiset<String> readToggles();

public abstract @Nullable ModulePath path();

public static Builder builder() {
Expand Down Expand Up @@ -239,6 +247,8 @@ public Builder addGoogNamespace(String namespace) {

public abstract ImmutableList.Builder<ModuleMetadata> nestedModulesBuilder();

public abstract ImmutableMultiset.Builder<String> readTogglesBuilder();

public abstract Builder path(@Nullable ModulePath value);

public abstract Builder usesClosure(boolean value);
Expand Down
10 changes: 10 additions & 0 deletions test/com/google/javascript/jscomp/GatherModuleMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,14 @@ public void testDynamicImport() {
assertThat(m.isNonProvideScript()).isTrue();
assertThat(m.es6ImportSpecifiers()).containsExactly("./imported.js");
}

@Test
public void testReadToggle() {
testSame(
lines(
"goog.readToggleInternalDoNotCallDirectly('foo_bar');",
"goog.readToggleInternalDoNotCallDirectly('baz');"));
ModuleMetadata m = metadataMap().getModulesByPath().get("testcode");
assertThat(m.readToggles()).containsExactly("foo_bar", "baz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public void testSoyDelcall_legacy() {
assertThat(info.delcalls).containsExactly("a");
}

@Test
public void testReadToggle() {
FileInfo info = parse("goog.readToggleInternalDoNotCallDirectly('foo_bar');");
assertThat(info.readToggles).containsExactly("foo_bar");
}

private static FileInfo parse(String... lines) {
return JsFileFullParser.parse(
Joiner.on('\n').join(lines),
Expand Down

0 comments on commit 1beb766

Please sign in to comment.