Skip to content

Commit

Permalink
Externs namespaces consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
paidsource committed Mar 14, 2023
1 parent e61082a commit 5289e42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/TypedScopeCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/jstype/JSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 10 additions & 1 deletion src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/com/google/javascript/rhino/jstype/PrototypeObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -136,6 +138,7 @@ static class Builder<T extends Builder<T>> {

private boolean nativeType;
private boolean anonymousType;
private boolean externsEmptyObjLit;

private TemplateTypeMap templateTypeMap;
private int templateParamCount;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -495,4 +503,8 @@ int recursionUnsafeHashCode() {
return System.identityHashCode(this);
}
}

public boolean isFromEmptyObjLitExtern() {
return fromEmptyObjLitExtern;
}
}

0 comments on commit 5289e42

Please sign in to comment.