From 5289e421a3bea73d2460277008fdc02d486de1de Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 14 Mar 2023 15:25:13 +0300 Subject: [PATCH] Externs namespaces consolidation --- .../google/javascript/jscomp/TypedScopeCreator.java | 4 +++- src/com/google/javascript/rhino/jstype/JSType.java | 4 ++++ .../javascript/rhino/jstype/JSTypeRegistry.java | 11 ++++++++++- .../javascript/rhino/jstype/PrototypeObjectType.java | 12 ++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/com/google/javascript/jscomp/TypedScopeCreator.java b/src/com/google/javascript/jscomp/TypedScopeCreator.java index 323a11beb9f..442e102b9b9 100644 --- a/src/com/google/javascript/jscomp/TypedScopeCreator.java +++ b/src/com/google/javascript/jscomp/TypedScopeCreator.java @@ -1073,7 +1073,9 @@ private void defineObjectLiteral(Node objectLit) { } if (type == null) { - type = typeRegistry.createAnonymousObjectType(info); + boolean isExterns = NodeUtil.getSourceFile(objectLit).isExtern(); + boolean fromEmptyObjLit = objectLit.getFirstChild() == null; + type = typeRegistry.createAnonymousObjectType(info, isExterns && fromEmptyObjLit); } setDeferredType(objectLit, type); diff --git a/src/com/google/javascript/rhino/jstype/JSType.java b/src/com/google/javascript/rhino/jstype/JSType.java index 9867ac47bfe..1dc62074c27 100644 --- a/src/com/google/javascript/rhino/jstype/JSType.java +++ b/src/com/google/javascript/rhino/jstype/JSType.java @@ -210,6 +210,10 @@ public boolean isBigIntValueType() { return false; } + public boolean isFromEmptyObjLitExtern() { + return false; + } + /** Whether this is the prototype of a function. */ // TODO(sdh): consider renaming this to isPrototypeObject. public boolean isFunctionPrototypeType() { diff --git a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java index 270982360c1..e6cb15fc167 100644 --- a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java +++ b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java @@ -1110,7 +1110,7 @@ private void registerNativeType(JSTypeNative typeId, JSType type) { // we don't need to store these properties in the propertyIndex separately. private static boolean isObjectLiteralThatCanBeSkipped(JSType t) { t = t.restrictByNotNullOrUndefined(); - return t.isRecordType() || t.isLiteralObject(); + return (t.isRecordType() || t.isLiteralObject()) && !t.isFromEmptyObjLitExtern(); } void registerDroppedPropertiesInUnion(RecordType subtype, RecordType supertype) { @@ -1782,6 +1782,15 @@ public ObjectType createAnonymousObjectType(@Nullable JSDocInfo info) { type.setJSDocInfo(info); return type; } + public ObjectType createAnonymousObjectType(@Nullable JSDocInfo info, boolean fromExternsEmptyObjLit) { + PrototypeObjectType type = PrototypeObjectType.builder(this) + .setAnonymous(true) + .setFromExternsEmptyObjectLit(fromExternsEmptyObjLit) + .build(); + type.setPrettyPrint(true); + type.setJSDocInfo(info); + return type; + } /** * Set the implicit prototype if it's possible to do so. There are a few different reasons why diff --git a/src/com/google/javascript/rhino/jstype/PrototypeObjectType.java b/src/com/google/javascript/rhino/jstype/PrototypeObjectType.java index 998872e6872..c2c254eae1d 100644 --- a/src/com/google/javascript/rhino/jstype/PrototypeObjectType.java +++ b/src/com/google/javascript/rhino/jstype/PrototypeObjectType.java @@ -76,6 +76,7 @@ public class PrototypeObjectType extends ObjectType { private final PropertyMap properties = new PropertyMap(); private final boolean nativeType; private final boolean anonymousType; + private boolean fromEmptyObjLitExtern; // NOTE(nicksantos): The implicit prototype can change over time. // Modeling this is a bear. Always call getImplicitPrototype(), because @@ -104,6 +105,7 @@ public class PrototypeObjectType extends ObjectType { this.templateParamCount = builder.templateParamCount; this.nativeType = builder.nativeType; this.anonymousType = builder.anonymousType; + this.fromEmptyObjLitExtern = builder.externsEmptyObjLit; this.properties.setParentSource(this); @@ -136,6 +138,7 @@ static class Builder> { private boolean nativeType; private boolean anonymousType; + private boolean externsEmptyObjLit; private TemplateTypeMap templateTypeMap; private int templateParamCount; @@ -166,6 +169,11 @@ final T setAnonymous(boolean x) { return castThis(); } + final T setFromExternsEmptyObjectLit(boolean x) { + this.externsEmptyObjLit = x; + return castThis(); + } + final T setTemplateTypeMap(TemplateTypeMap x) { this.templateTypeMap = x; return castThis(); @@ -495,4 +503,8 @@ int recursionUnsafeHashCode() { return System.identityHashCode(this); } } + + public boolean isFromEmptyObjLitExtern() { + return fromEmptyObjLitExtern; + } }