Skip to content

Commit

Permalink
[NTI] Refactor CodingConvention.applySubclassRelationship to use Prop…
Browse files Browse the repository at this point in the history
…etyDeclarer.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168760846
  • Loading branch information
shicks authored and lauraharker committed Sep 15, 2017
1 parent 68a64d6 commit 37a91ba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/com/google/javascript/jscomp/ClosureCodingConvention.java
Expand Up @@ -73,21 +73,24 @@ public ClosureCodingConvention(CodingConvention wrapped) {
* subclass, and a {@code constructor} property. * subclass, and a {@code constructor} property.
*/ */
@Override @Override
public void applySubclassRelationship(FunctionType parentCtor, public void applySubclassRelationship(
FunctionType childCtor, SubclassType type) { PropertyDeclarer declarer,
super.applySubclassRelationship(parentCtor, childCtor, type); FunctionTypeI parentCtor,
FunctionTypeI childCtor,
SubclassType type) {
super.applySubclassRelationship(declarer, parentCtor, childCtor, type);
if (type == SubclassType.INHERITS) { if (type == SubclassType.INHERITS) {
childCtor.defineDeclaredProperty("superClass_", declarer.declareProperty(childCtor, "superClass_",
parentCtor.getPrototype(), childCtor.getSource()); parentCtor.getPrototypeProperty(), childCtor.getSource());
childCtor.getPrototype().defineDeclaredProperty("constructor", declarer.declareProperty(childCtor.getPrototypeProperty(), "constructor",
// Notice that constructor functions do not need to be covariant // Notice that constructor functions do not need to be covariant
// on the superclass. // on the superclass.
// So if G extends F, new G() and new F() can accept completely // So if G extends F, new G() and new F() can accept completely
// different argument types, but G.prototype.constructor needs // different argument types, but G.prototype.constructor needs
// to be covariant on F.prototype.constructor. // to be covariant on F.prototype.constructor.
// To get around this, we just turn off type-checking on arguments // To get around this, we just turn off type-checking on arguments
// and return types of G.prototype.constructor. // and return types of G.prototype.constructor.
(FunctionType) childCtor.toBuilder().withUnknownReturnType().withNoParameters().build(), childCtor.toBuilder().withUnknownReturnType().withNoParameters().build(),
childCtor.getSource()); childCtor.getSource());
} }
} }
Expand Down
7 changes: 5 additions & 2 deletions src/com/google/javascript/jscomp/CodingConvention.java
Expand Up @@ -214,8 +214,11 @@ public interface CodingConvention extends Serializable {
* In many JS libraries, the function that produces inheritance also * In many JS libraries, the function that produces inheritance also
* adds properties to the superclass and/or subclass. * adds properties to the superclass and/or subclass.
*/ */
public void applySubclassRelationship(FunctionType parentCtor, public void applySubclassRelationship(
FunctionType childCtor, SubclassType type); PropertyDeclarer declarer,
FunctionTypeI parentCtor,
FunctionTypeI childCtor,
SubclassType type);


/** /**
* Function name for abstract methods. An abstract method can be assigned to * Function name for abstract methods. An abstract method can be assigned to
Expand Down
17 changes: 11 additions & 6 deletions src/com/google/javascript/jscomp/CodingConventions.java
Expand Up @@ -200,10 +200,12 @@ public List<String> identifyTypeDeclarationCall(Node n) {
} }


@Override @Override
public void applySubclassRelationship(FunctionType parentCtor, public void applySubclassRelationship(
FunctionType childCtor, SubclassType type) { PropertyDeclarer declarer,
nextConvention.applySubclassRelationship( FunctionTypeI parentCtor,
parentCtor, childCtor, type); FunctionTypeI childCtor,
SubclassType type) {
nextConvention.applySubclassRelationship(declarer, parentCtor, childCtor, type);
} }


@Override @Override
Expand Down Expand Up @@ -458,8 +460,11 @@ public List<String> identifyTypeDeclarationCall(Node n) {
} }


@Override @Override
public void applySubclassRelationship(FunctionType parentCtor, public void applySubclassRelationship(
FunctionType childCtor, SubclassType type) { PropertyDeclarer declarer,
FunctionTypeI parentCtor,
FunctionTypeI childCtor,
SubclassType type) {
// do nothing // do nothing
} }


Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TypedScopeCreator.java
Expand Up @@ -1502,7 +1502,7 @@ private void checkForClassDefiningCalls(Node n) {
FunctionType subCtor = subClass.getConstructor(); FunctionType subCtor = subClass.getConstructor();
if (superCtor != null && subCtor != null) { if (superCtor != null && subCtor != null) {
codingConvention.applySubclassRelationship( codingConvention.applySubclassRelationship(
superCtor, subCtor, relationship.type); PROPERTY_DECLARER, superCtor, subCtor, relationship.type);
} }
} }
} }
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSTypeRegistry; import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.PropertyDeclarer;
import junit.framework.TestCase; import junit.framework.TestCase;


/** /**
Expand Down Expand Up @@ -215,6 +216,7 @@ public void testRequire() {


public void testApplySubclassRelationship() { public void testApplySubclassRelationship() {
JSTypeRegistry registry = new JSTypeRegistry(null); JSTypeRegistry registry = new JSTypeRegistry(null);
PropertyDeclarer declarer = new PropertyDeclarer();


Node nodeA = new Node(Token.FUNCTION); Node nodeA = new Node(Token.FUNCTION);
FunctionType ctorA = FunctionType ctorA =
Expand All @@ -224,7 +226,7 @@ public void testApplySubclassRelationship() {
FunctionType ctorB = FunctionType ctorB =
registry.createConstructorType("B", nodeB, new Node(Token.PARAM_LIST), null, null, false); 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")); assertTrue(ctorB.getPrototype().hasOwnProperty("constructor"));
assertEquals(nodeB, ctorB.getPrototype().getPropertyNode("constructor")); assertEquals(nodeB, ctorB.getPrototype().getPropertyNode("constructor"));
Expand Down

0 comments on commit 37a91ba

Please sign in to comment.