Skip to content

Commit e2655d6

Browse files
lauraharkercopybara-github
authored andcommitted
Fix TypedScopeCreator crash on goog.provided names assigned in an if block
The root cause was TypedScopeCreator was declaring these names in the block scope but not the global scope, even though goog.provides are all global PiperOrigin-RevId: 515746418
1 parent 89e5d54 commit e2655d6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,13 @@ void defineCatch(Node n) {
11901190
});
11911191
}
11921192

1193-
/** Defines an assignment to a name as if it were an actual declaration. */
1194-
void defineAssignAsIfDeclaration(Node assignment) {
1193+
/** Defines an assignment to a name as if it were an actual `var` declaration. */
1194+
void defineAssignAsIfVarDeclaration(Node assignment) {
11951195
JSDocInfo info = assignment.getJSDocInfo();
11961196
Node name = assignment.getFirstChild();
11971197
checkArgument(name.isName(), name);
11981198
Node rvalue = assignment.getSecondChild();
1199-
defineName(name, rvalue, currentScope, info);
1199+
defineName(name, rvalue, currentScope.getClosestHoistScope(), info);
12001200
}
12011201

12021202
/** Defines a variable declared with `var`, `let`, or `const`. */
@@ -3165,7 +3165,7 @@ void visitPostorder(NodeTraversal t, Node n, Node parent) {
31653165
if (firstChild.isGetProp() && firstChild.isQualifiedName()) {
31663166
maybeDeclareQualifiedName(t, n.getJSDocInfo(), firstChild, n, firstChild.getNext());
31673167
} else if (undeclaredNamesForClosure.contains(firstChild)) {
3168-
defineAssignAsIfDeclaration(n);
3168+
defineAssignAsIfVarDeclaration(n);
31693169
}
31703170
break;
31713171

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6155,6 +6155,16 @@ public void testGoogProvide_singleNameWithAtTypeJSDocIsDeclared() {
61556155
assertThat(fooVar).isNotInferred();
61566156
}
61576157

6158+
@Test
6159+
public void testGoogProvide_singleNameAssignedInIfBlock() {
6160+
processClosurePrimitives = true;
6161+
testSame(srcs("goog.provide('foo'); if (!foo) { foo = {}; };"));
6162+
6163+
TypedVar fooVar = globalScope.getVar("foo");
6164+
assertThat(fooVar).hasJSTypeThat().toStringIsEqualTo("{}");
6165+
assertThat(fooVar).isInferred();
6166+
}
6167+
61586168
@Test
61596169
public void testGoogProvide_ignoresBlockScopedShadow() {
61606170
processClosurePrimitives = true;

0 commit comments

Comments
 (0)