Skip to content

Commit

Permalink
Add variance node type and generate property variance annotations (ba…
Browse files Browse the repository at this point in the history
…bel#4697)

* Add variance node type and generate property variance annotations

babel/babylon#161 adds parsing support for property variance
annotations. This PR adds the necessary node type for the new Variance
node and generate support for all the positions where variance can now
appear.

* Variance is no longer a separate node type

This diff also adds tests to class properties and to the
flow-strip-types transform.

* Add test + fix for edge case with variance and class proeprties
  • Loading branch information
samwgoldman authored and panagosg7 committed Jan 17, 2017
1 parent 9edf24d commit f732bc2
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/babel-generator/src/generators/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function ClassProperty(node: Object) {
this.print(node.key, node);
this.token("]");
} else {
this._variance(node);
this.print(node.key, node);
}
this.print(node.typeAnnotation, node);
Expand Down
16 changes: 11 additions & 5 deletions packages/babel-generator/src/generators/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ export function _interfaceish(node: Object) {
this.print(node.body, node);
}

export function _variance(node) {
if (node.variance === "plus") {
this.token("+");
} else if (node.variance === "minus") {
this.token("-");
}
}

export function InterfaceDeclaration(node: Object) {
this.word("interface");
this.space();
Expand Down Expand Up @@ -225,11 +233,7 @@ export function TypeAnnotation(node: Object) {
}

export function TypeParameter(node: Object) {
if (node.variance === "plus") {
this.token("+");
} else if (node.variance === "minus") {
this.token("-");
}
this._variance(node);

this.word(node.name);

Expand Down Expand Up @@ -299,6 +303,7 @@ export function ObjectTypeIndexer(node: Object) {
this.word("static");
this.space();
}
this._variance(node);
this.token("[");
this.print(node.id, node);
this.token(":");
Expand All @@ -315,6 +320,7 @@ export function ObjectTypeProperty(node: Object) {
this.word("static");
this.space();
}
this._variance(node);
this.print(node.key, node);
if (node.optional) this.token("?");
this.token(":");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
class C<+T, -U> {}
class C1<+T, -U> {}
function f<+T, -U>() {}
type T<+T, -U> = {};
type T = { +p: T };
type T = { -p: T };
type T = { +[k:K]: V };
type T = { -[k:K]: V };
interface I { +p: T };
interface I { -p: T };
interface I { +[k:K]: V };
interface I { -[k:K]: V };
declare class I { +p: T };
declare class I { -p: T };
declare class I { +[k:K]: V };
declare class I { -[k:K]: V };
class C2 { +p: T = e };
class C3 { -p: T = e };
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
class C<+T, -U> {}
class C1<+T, -U> {}
function f<+T, -U>() {}
type T<+T, -U> = {};
type T = { +p: T };
type T = { -p: T };
type T = { +[k: K]: V };
type T = { -[k: K]: V };
interface I { +p: T };
interface I { -p: T };
interface I { +[k: K]: V };
interface I { -[k: K]: V };
declare class I { +p: T };
declare class I { -p: T };
declare class I { +[k: K]: V };
declare class I { -[k: K]: V };
class C2 {
+p: T = e;
};
class C3 {
-p: T = e;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function ({ types: t }) {
},

ClassProperty(path) {
path.node.variance = null;
path.node.typeAnnotation = null;
if (!path.node.value) path.remove();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class C {
+p: T = e;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class C {
p = e;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-flow-strip-types", "syntax-class-properties"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
class C<+T, -U> {}
class C1<+T, -U> {}
function f<+T, -U>() {}
type T<+T, -U> = {};
type T<+T, -U> = {}
type T = { +p: T }
type T = { -p: T }
type T = { +[k:K]: V }
type T = { -[k:K]: V }
interface I { +p: T }
interface I { -p: T }
interface I { +[k:K]: V }
interface I { -[k:K]: V }
declare class I { +p: T }
declare class I { -p: T }
declare class I { +[k:K]: V }
declare class I { -[k:K]: V }
class C2 { +p: T }
class C3 { -p: T }
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class C {}
class C1 {}
function f() {}

class C2 {}
class C3 {}

0 comments on commit f732bc2

Please sign in to comment.