Flow: Fix generating arrow functions with param (#4504)
* transform-flow-comments, single arrow param support #4503 * further tests for printing single arrow func param flow code * cleanup
This commit is contained in:
parent
5249b9d809
commit
41f2bbc104
@ -77,8 +77,10 @@ export function ArrowFunctionExpression(node: Object) {
|
||||
this.space();
|
||||
}
|
||||
|
||||
if (node.params.length === 1 && t.isIdentifier(node.params[0])) {
|
||||
this.print(node.params[0], node);
|
||||
const firstParam = node.params[0];
|
||||
|
||||
if (node.params.length === 1 && t.isIdentifier(firstParam) && !hasTypes(node, firstParam)) {
|
||||
this.print(firstParam, node);
|
||||
} else {
|
||||
this._params(node);
|
||||
}
|
||||
@ -89,3 +91,7 @@ export function ArrowFunctionExpression(node: Object) {
|
||||
|
||||
this.print(node.body, node);
|
||||
}
|
||||
|
||||
function hasTypes(node, param) {
|
||||
return node.typeParameters || node.returnType || param.typeAnnotation || param.optional || param.trailingComments;
|
||||
}
|
||||
|
||||
7
packages/babel-generator/test/fixtures/flow/arrow-functions/actual.js
vendored
Normal file
7
packages/babel-generator/test/fixtures/flow/arrow-functions/actual.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
const bar1 = (x: number): string => {};
|
||||
const bar2 = (x?) => {};
|
||||
const bar3 = (x?: string) => {};
|
||||
const bar4 = x => {};
|
||||
const bar5 = (x): string => {};
|
||||
const bar6 = (x: number) => {};
|
||||
const bar7 = <T>(x) => {};
|
||||
7
packages/babel-generator/test/fixtures/flow/arrow-functions/expected.js
vendored
Normal file
7
packages/babel-generator/test/fixtures/flow/arrow-functions/expected.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
const bar1 = (x: number): string => {};
|
||||
const bar2 = (x?) => {};
|
||||
const bar3 = (x?: string) => {};
|
||||
const bar4 = x => {};
|
||||
const bar5 = (x): string => {};
|
||||
const bar6 = (x: number) => {};
|
||||
const bar7 = <T>(x) => {};
|
||||
@ -0,0 +1 @@
|
||||
const x = (foo?) => {}
|
||||
@ -0,0 +1 @@
|
||||
const x = (foo /*:: ?*/) => {};
|
||||
@ -0,0 +1 @@
|
||||
const x = (foo: string) => {};
|
||||
@ -0,0 +1 @@
|
||||
const x = (foo /*: string*/) => {};
|
||||
Loading…
x
Reference in New Issue
Block a user