Skip to content

Commit

Permalink
Rename _F_toggleOrdinals to CLOSURE_TOGGLE_ORDINALS.
Browse files Browse the repository at this point in the history
RELNOTES: n/a

PiperOrigin-RevId: 558258590
  • Loading branch information
shicks authored and copybara-github committed Aug 18, 2023
1 parent 032ea83 commit f310c01
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/ReplaceToggles.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class ReplaceToggles implements CompilerPass {
static final DiagnosticType INVALID_ORDINAL_MAPPING =
DiagnosticType.error(
"JSC_INVALID_ORDINAL_MAPPING",
"_F_toggleOrdinals must be initialized with an object literal mapping strings to booleans"
+ " or unique whole numbers: {0}");
"CLOSURE_TOGGLE_ORDINALS must be initialized with an object literal mapping strings to"
+ " booleans or unique whole numbers: {0}");

static final DiagnosticType UNKNOWN_TOGGLE =
DiagnosticType.error(
Expand Down Expand Up @@ -72,15 +72,15 @@ public void process(Node externs, Node root) {
NodeTraversal.traverse(compiler, root, new Traversal());
}

private static final String ORDINAL_VAR_NAME = "_F_toggleOrdinals";
private static final String ORDINAL_VAR_NAME = "CLOSURE_TOGGLE_ORDINALS";
private static final QualifiedName readToggleFunctionName =
QualifiedName.of("goog.readToggleInternalDoNotCallDirectly");

private class Traversal extends AbstractPostOrderCallback {

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
// Look for `var _F_toggleOrdinals = {...};`. Note that we only do this in check mode.
// Look for `var CLOSURE_TOGGLE_ORDINALS = {...};`. Note that we only do this in check mode.
// It's not our responsibility to delete the unused variable in optimized mode - if all
// the calls to readToggle are deleted, then the bootstrap will be unused and deleted, too.
if (check
Expand Down
52 changes: 26 additions & 26 deletions test/com/google/javascript/jscomp/ReplaceTogglesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ public void resetPassParameters() throws Exception {
@Test
public void testBootstrapOrdinals_uninitialized() {
check = true;
testSame(srcs("var _F_toggleOrdinals;"), expectOrdinals(null));
testSame(srcs("var CLOSURE_TOGGLE_ORDINALS;"), expectOrdinals(null));
}

@Test
public void testBootstrapOrdinals_empty() {
check = true;
testSame(srcs("var _F_toggleOrdinals = {};"), expectOrdinals(ImmutableMap.of()));
testSame(srcs("var CLOSURE_TOGGLE_ORDINALS = {};"), expectOrdinals(ImmutableMap.of()));
}

@Test
public void testBootstrapOrdinals_simple() {
check = true;
testSame(
srcs("var _F_toggleOrdinals = {'foo_bar': 1};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {'foo_bar': 1};"),
expectOrdinals(ImmutableMap.of("foo_bar", 1)));
testSame(
srcs("var _F_toggleOrdinals = {'foo_bar': 1, qux: 2};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {'foo_bar': 1, qux: 2};"),
expectOrdinals(ImmutableMap.of("foo_bar", 1, "qux", 2)));
}

Expand All @@ -68,45 +68,45 @@ public void testBootstrapOrdinals_ignoresExtraUninitializedDefinitions() {
testSame(
srcs(
lines(
"var _F_toggleOrdinals = {'foo': 1};", //
"var _F_toggleOrdinals;")),
"var CLOSURE_TOGGLE_ORDINALS = {'foo': 1};", //
"var CLOSURE_TOGGLE_ORDINALS;")),
expectOrdinals(ImmutableMap.of("foo", 1)));
testSame(
srcs(
lines(
"var _F_toggleOrdinals;", //
"var _F_toggleOrdinals = {'foo': 1};")),
"var CLOSURE_TOGGLE_ORDINALS;", //
"var CLOSURE_TOGGLE_ORDINALS = {'foo': 1};")),
expectOrdinals(ImmutableMap.of("foo", 1)));
}

@Test
public void testBootstrapOrdinals_booleanAllowed() {
check = true;
testSame(
srcs("var _F_toggleOrdinals = {'foo_bar': false, 'baz': true};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {'foo_bar': false, 'baz': true};"),
expectOrdinals(ImmutableMap.of("foo_bar", FALSE_VALUE, "baz", TRUE_VALUE)));
}

@Test
public void testBootstrapOrdinals_booleanAllowsDuplicates() {
check = true;
testSame(
srcs("var _F_toggleOrdinals = {'foo_bar': false, 'baz': false, 'qux': 0};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {'foo_bar': false, 'baz': false, 'qux': 0};"),
expectOrdinals(ImmutableMap.of("foo_bar", FALSE_VALUE, "baz", FALSE_VALUE, "qux", 0)));
testSame(
srcs("var _F_toggleOrdinals = {'foo_bar': true, 'baz': true, 'qux': 0};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {'foo_bar': true, 'baz': true, 'qux': 0};"),
expectOrdinals(ImmutableMap.of("foo_bar", TRUE_VALUE, "baz", TRUE_VALUE, "qux", 0)));
}

@Test
public void testBootstrapOrdinals_notAnObject() {
check = true;
test(
srcs("var _F_toggleOrdinals = 1;"),
srcs("var CLOSURE_TOGGLE_ORDINALS = 1;"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("not an object literal"));
test(
srcs("var _F_toggleOrdinals = [];"),
srcs("var CLOSURE_TOGGLE_ORDINALS = [];"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("not an object literal"));
}
Expand All @@ -115,57 +115,57 @@ public void testBootstrapOrdinals_notAnObject() {
public void testBootstrapOrdinals_badKey() {
check = true;
test(
srcs("var _F_toggleOrdinals = {[x]: 1};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {[x]: 1};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING).withMessageContaining("non-string key"));
test(
srcs("var _F_toggleOrdinals = {['x']: 1};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {['x']: 1};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING).withMessageContaining("non-string key"));
test(
srcs("var _F_toggleOrdinals = {x() { return 1; }};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x() { return 1; }};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING).withMessageContaining("non-string key"));
}

@Test
public void testBootstrapOrdinals_duplicateKey() {
check = true;
test(
srcs("var _F_toggleOrdinals = {x: 1, y: 2, x: 3};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: 1, y: 2, x: 3};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING).withMessageContaining("duplicate key: x"));
}

@Test
public void testBootstrapOrdinals_badValue() {
check = true;
test(
srcs("var _F_toggleOrdinals = {x: 'y'};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: 'y'};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: 1 + 2};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: 1 + 2};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: NaN};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: NaN};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: Infinity};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: Infinity};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: -1};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: -1};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: 1.2};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: 1.2};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
test(
srcs("var _F_toggleOrdinals = {x: null};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: null};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("value not a boolean or whole number literal"));
}
Expand All @@ -174,7 +174,7 @@ public void testBootstrapOrdinals_badValue() {
public void testBootstrapOrdinals_duplicateOrdinal() {
check = true;
test(
srcs("var _F_toggleOrdinals = {x: 1, y: 2, z: 1};"),
srcs("var CLOSURE_TOGGLE_ORDINALS = {x: 1, y: 2, z: 1};"),
error(ReplaceToggles.INVALID_ORDINAL_MAPPING)
.withMessageContaining("duplicate ordinal: 1"));
}
Expand Down

0 comments on commit f310c01

Please sign in to comment.