Skip to content

Commit 6806f04

Browse files
mhausnercopybara-github
authored andcommitted
Fix 2 for object literal type inference
The first attempt in cl/497979576 broke some targets because properties were dropped from object literals that were expected in other parts of the code. This fix makes sure that all properties before the spread operator are added. Note that any properties after the spread operator are still dropped. Description of the first fix: Type inference may visit the same object literal multiple times. If it contains a spread operator, the type of the literal is converted to the global type Object. A subsequent pass over the same literal could later add inferred properties to type Object, which is wrong. PiperOrigin-RevId: 498473432
1 parent c8579f8 commit 6806f04

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,9 +1802,10 @@ private FlowScope traverseObjectLiteral(Node n, FlowScope scope) {
18021802
// See b/260837012. traverseObjectLiteral() can be invoked multiple times for the same
18031803
// literal. When a spread operator is encountered, the type of the literal is changed to
18041804
// OBJECT_TYPE. A second invocation of this code may then erroneously define new properties
1805-
// on the global OBJECT_TYPE. So, if there are spread operators in the literal, do not add
1806-
// newly inferred properties to the type.
1807-
if (!spreadOperatorSeen) {
1805+
// on the global OBJECT_TYPE. So, if there are spread operators in the literal and the type
1806+
// has already been propagated to OBJECT_TYPE, do not add newly inferred properties to the
1807+
// type.
1808+
if (!spreadOperatorSeen || !n.getJSType().isNativeObjectType()) {
18081809
objectType.defineInferredProperty(memberName, valueType, key);
18091810
}
18101811

0 commit comments

Comments
 (0)