Skip to content

Commit 22b778b

Browse files
gkdncopybara-github
authored andcommitted
Add unit tests to capture some current behavior around ReferenceCollector.isWellDefined.
PiperOrigin-RevId: 788983162
1 parent cb3c9a2 commit 22b778b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,57 @@ public void testObjectRest_declaration() {
9797
});
9898
}
9999

100+
@Test
101+
public void testClass() {
102+
testBehavior(
103+
"""
104+
class Foo {}
105+
""",
106+
(NodeTraversal t, ReferenceMap rm) -> {
107+
if (t.getScope().isGlobal()) {
108+
ReferenceCollection x = rm.getReferences(t.getScope().getVar("Foo"));
109+
110+
assertThat(x.isAssignedOnceInLifetime()).isTrue();
111+
assertThat(x.isWellDefined()).isTrue();
112+
assertThat(x).comparingElementsUsing(IS_DECLARATION).containsExactly(true).inOrder();
113+
}
114+
});
115+
}
116+
117+
@Test
118+
public void testClass_withPrototype() {
119+
testBehavior(
120+
"""
121+
class Foo {}
122+
Foo.prototype.bar = 1;
123+
""",
124+
(NodeTraversal t, ReferenceMap rm) -> {
125+
if (t.getScope().isGlobal()) {
126+
ReferenceCollection x = rm.getReferences(t.getScope().getVar("Foo"));
127+
128+
assertThat(x.isAssignedOnceInLifetime()).isTrue();
129+
assertThat(x.isWellDefined()).isFalse(); // TODO(b/435019132): Should be true.
130+
}
131+
});
132+
}
133+
134+
@Test
135+
public void testClass_referencedAfterDeclaration() {
136+
testBehavior(
137+
"""
138+
class Foo {}
139+
const x = Foo;
140+
""",
141+
(NodeTraversal t, ReferenceMap rm) -> {
142+
if (t.getScope().isGlobal()) {
143+
ReferenceCollection x = rm.getReferences(t.getScope().getVar("Foo"));
144+
145+
assertThat(x.isAssignedOnceInLifetime()).isTrue();
146+
assertThat(x.isWellDefined()).isFalse(); // TODO(b/435019132): Should be true.
147+
}
148+
});
149+
}
150+
100151
@Test
101152
public void testImport1() {
102153
testBehavior(

0 commit comments

Comments
 (0)