From 60adcd68a041df03a1be27359bc35aa9ae88d37f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 26 Apr 2017 22:16:38 +0100 Subject: [PATCH] Port flow object spread from #418 to 6.x (#5653) * Add support for object type spread * Type spread: remove variance and add stripping test * Fix tests --- packages/babel-generator/src/generators/flow.js | 5 +++++ .../test/fixtures/flow/object-literal-types/actual.js | 7 +++++++ .../test/fixtures/flow/object-literal-types/expected.js | 7 +++++++ .../fixtures/strip-types/strip-type-annotations/actual.js | 1 + .../strip-types/strip-type-annotations/expected.js | 1 + packages/babel-types/src/definitions/flow.js | 8 ++++++++ 6 files changed, 29 insertions(+) create mode 100644 packages/babel-generator/test/fixtures/flow/object-literal-types/actual.js create mode 100644 packages/babel-generator/test/fixtures/flow/object-literal-types/expected.js diff --git a/packages/babel-generator/src/generators/flow.js b/packages/babel-generator/src/generators/flow.js index 9f5c6f3623..ecb676ba7d 100644 --- a/packages/babel-generator/src/generators/flow.js +++ b/packages/babel-generator/src/generators/flow.js @@ -333,6 +333,11 @@ export function ObjectTypeProperty(node: Object) { this.print(node.value, node); } +export function ObjectTypeSpreadProperty(node: Object) { + this.token("..."); + this.print(node.argument, node); +} + export function QualifiedTypeIdentifier(node: Object) { this.print(node.qualification, node); this.token("."); diff --git a/packages/babel-generator/test/fixtures/flow/object-literal-types/actual.js b/packages/babel-generator/test/fixtures/flow/object-literal-types/actual.js new file mode 100644 index 0000000000..80d29c99ee --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/object-literal-types/actual.js @@ -0,0 +1,7 @@ +type U = {}; +type V = {}; +type T = { ...U, }; +type T = { ...U, ...V }; +type T = { p: V, ...U }; +type T = { ...U, p: V, }; +type T = { ...{}|{ p: V, }}; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/flow/object-literal-types/expected.js b/packages/babel-generator/test/fixtures/flow/object-literal-types/expected.js new file mode 100644 index 0000000000..94a1bf0f7b --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/object-literal-types/expected.js @@ -0,0 +1,7 @@ +type U = {}; +type V = {}; +type T = { ...U }; +type T = { ...U; ...V; }; +type T = { p: V; ...U; }; +type T = { ...U; p: V; }; +type T = { ...{} | { p: V } }; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/actual.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/actual.js index f602db2cbc..3d96731ccf 100644 --- a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/actual.js +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/actual.js @@ -44,6 +44,7 @@ var a: { subObj: {strVal: string} } var a: { subObj: ?{strVal: string} } var a: { param1: number; param2: string } var a: { param1: number; param2?: string } +var a: { ...any; ...{}|{p: void} }; var a: { [a: number]: string; [b: number]: string; }; var a: { add(x: number, ...y: Array): void }; var a: { id(x: T): T; }; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/expected.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/expected.js index a6c982a6a3..adec467fc7 100644 --- a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/expected.js +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/expected.js @@ -47,6 +47,7 @@ var a; var a; var a; var a; +var a; var a = [1, 2, 3]; a = class Foo {}; a = class Foo extends Bar {}; diff --git a/packages/babel-types/src/definitions/flow.js b/packages/babel-types/src/definitions/flow.js index 208382420c..0c74bdb00d 100644 --- a/packages/babel-types/src/definitions/flow.js +++ b/packages/babel-types/src/definitions/flow.js @@ -308,6 +308,14 @@ defineType("ObjectTypeProperty", { } }); +defineType("ObjectTypeSpreadProperty", { + visitor: ["argument"], + aliases: ["Flow", "UserWhitespacable"], + fields: { + // todo + }, +}); + defineType("QualifiedTypeIdentifier", { visitor: ["id", "qualification"], aliases: ["Flow"],