Add variance node type and generate property variance annotations (#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
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
|
||||
@@ -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(":");
|
||||
@@ -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(":");
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
+p: T = e;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
p = e;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-flow-strip-types", "syntax-class-properties"]
|
||||
}
|
||||
@@ -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 }
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
class C {}
|
||||
class C1 {}
|
||||
function f() {}
|
||||
|
||||
class C2 {}
|
||||
class C3 {}
|
||||
|
||||
Reference in New Issue
Block a user