From 37a91ba3571ee5ffd88fbb2af9b7d06c873fcfd8 Mon Sep 17 00:00:00 2001 From: sdh Date: Thu, 14 Sep 2017 16:04:54 -0700 Subject: [PATCH] [NTI] Refactor CodingConvention.applySubclassRelationship to use PropetyDeclarer. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=168760846 --- .../jscomp/ClosureCodingConvention.java | 17 ++++++++++------- .../javascript/jscomp/CodingConvention.java | 7 +++++-- .../javascript/jscomp/CodingConventions.java | 17 +++++++++++------ .../javascript/jscomp/TypedScopeCreator.java | 2 +- .../jscomp/ClosureCodingConventionTest.java | 4 +++- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/com/google/javascript/jscomp/ClosureCodingConvention.java b/src/com/google/javascript/jscomp/ClosureCodingConvention.java index eb185cfeb35..a1d9baf6d3a 100644 --- a/src/com/google/javascript/jscomp/ClosureCodingConvention.java +++ b/src/com/google/javascript/jscomp/ClosureCodingConvention.java @@ -73,13 +73,16 @@ public ClosureCodingConvention(CodingConvention wrapped) { * subclass, and a {@code constructor} property. */ @Override - public void applySubclassRelationship(FunctionType parentCtor, - FunctionType childCtor, SubclassType type) { - super.applySubclassRelationship(parentCtor, childCtor, type); + public void applySubclassRelationship( + PropertyDeclarer declarer, + FunctionTypeI parentCtor, + FunctionTypeI childCtor, + SubclassType type) { + super.applySubclassRelationship(declarer, parentCtor, childCtor, type); if (type == SubclassType.INHERITS) { - childCtor.defineDeclaredProperty("superClass_", - parentCtor.getPrototype(), childCtor.getSource()); - childCtor.getPrototype().defineDeclaredProperty("constructor", + declarer.declareProperty(childCtor, "superClass_", + parentCtor.getPrototypeProperty(), childCtor.getSource()); + declarer.declareProperty(childCtor.getPrototypeProperty(), "constructor", // Notice that constructor functions do not need to be covariant // on the superclass. // So if G extends F, new G() and new F() can accept completely @@ -87,7 +90,7 @@ public void applySubclassRelationship(FunctionType parentCtor, // to be covariant on F.prototype.constructor. // To get around this, we just turn off type-checking on arguments // and return types of G.prototype.constructor. - (FunctionType) childCtor.toBuilder().withUnknownReturnType().withNoParameters().build(), + childCtor.toBuilder().withUnknownReturnType().withNoParameters().build(), childCtor.getSource()); } } diff --git a/src/com/google/javascript/jscomp/CodingConvention.java b/src/com/google/javascript/jscomp/CodingConvention.java index 9e3659ea6b1..f556f9dc8d3 100644 --- a/src/com/google/javascript/jscomp/CodingConvention.java +++ b/src/com/google/javascript/jscomp/CodingConvention.java @@ -214,8 +214,11 @@ public interface CodingConvention extends Serializable { * In many JS libraries, the function that produces inheritance also * adds properties to the superclass and/or subclass. */ - public void applySubclassRelationship(FunctionType parentCtor, - FunctionType childCtor, SubclassType type); + public void applySubclassRelationship( + PropertyDeclarer declarer, + FunctionTypeI parentCtor, + FunctionTypeI childCtor, + SubclassType type); /** * Function name for abstract methods. An abstract method can be assigned to diff --git a/src/com/google/javascript/jscomp/CodingConventions.java b/src/com/google/javascript/jscomp/CodingConventions.java index 0364b380384..82aa86e0d21 100644 --- a/src/com/google/javascript/jscomp/CodingConventions.java +++ b/src/com/google/javascript/jscomp/CodingConventions.java @@ -200,10 +200,12 @@ public List identifyTypeDeclarationCall(Node n) { } @Override - public void applySubclassRelationship(FunctionType parentCtor, - FunctionType childCtor, SubclassType type) { - nextConvention.applySubclassRelationship( - parentCtor, childCtor, type); + public void applySubclassRelationship( + PropertyDeclarer declarer, + FunctionTypeI parentCtor, + FunctionTypeI childCtor, + SubclassType type) { + nextConvention.applySubclassRelationship(declarer, parentCtor, childCtor, type); } @Override @@ -458,8 +460,11 @@ public List identifyTypeDeclarationCall(Node n) { } @Override - public void applySubclassRelationship(FunctionType parentCtor, - FunctionType childCtor, SubclassType type) { + public void applySubclassRelationship( + PropertyDeclarer declarer, + FunctionTypeI parentCtor, + FunctionTypeI childCtor, + SubclassType type) { // do nothing } diff --git a/src/com/google/javascript/jscomp/TypedScopeCreator.java b/src/com/google/javascript/jscomp/TypedScopeCreator.java index a4c535b091a..676720e1f29 100644 --- a/src/com/google/javascript/jscomp/TypedScopeCreator.java +++ b/src/com/google/javascript/jscomp/TypedScopeCreator.java @@ -1502,7 +1502,7 @@ private void checkForClassDefiningCalls(Node n) { FunctionType subCtor = subClass.getConstructor(); if (superCtor != null && subCtor != null) { codingConvention.applySubclassRelationship( - superCtor, subCtor, relationship.type); + PROPERTY_DECLARER, superCtor, subCtor, relationship.type); } } } diff --git a/test/com/google/javascript/jscomp/ClosureCodingConventionTest.java b/test/com/google/javascript/jscomp/ClosureCodingConventionTest.java index 97d5d0a6720..5e5e981e2a7 100644 --- a/test/com/google/javascript/jscomp/ClosureCodingConventionTest.java +++ b/test/com/google/javascript/jscomp/ClosureCodingConventionTest.java @@ -22,6 +22,7 @@ import com.google.javascript.rhino.Token; import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.JSTypeRegistry; +import com.google.javascript.rhino.jstype.PropertyDeclarer; import junit.framework.TestCase; /** @@ -215,6 +216,7 @@ public void testRequire() { public void testApplySubclassRelationship() { JSTypeRegistry registry = new JSTypeRegistry(null); + PropertyDeclarer declarer = new PropertyDeclarer(); Node nodeA = new Node(Token.FUNCTION); FunctionType ctorA = @@ -224,7 +226,7 @@ public void testApplySubclassRelationship() { FunctionType ctorB = registry.createConstructorType("B", nodeB, new Node(Token.PARAM_LIST), null, null, false); - conv.applySubclassRelationship(ctorA, ctorB, SubclassType.INHERITS); + conv.applySubclassRelationship(declarer, ctorA, ctorB, SubclassType.INHERITS); assertTrue(ctorB.getPrototype().hasOwnProperty("constructor")); assertEquals(nodeB, ctorB.getPrototype().getPropertyNode("constructor"));