New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify the special-case printing of single-param arrow functions #13204
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import generate from "../lib"; | ||
import { parse } from "@babel/parser"; | ||
|
||
describe("parameter parentheses", () => { | ||
// Common source text for several snapshot tests | ||
const source = ` | ||
() => {}; | ||
a => {}; | ||
(a, b) => {}; | ||
async () => {}; | ||
async a => {}; | ||
async (a, b) => {}; | ||
`; | ||
// Apply a callback function to each parameter in the AST of the above source | ||
function forEachParam(ast, callbackFn) { | ||
ast.program.body.forEach(s => { | ||
s.expression.params.forEach(p => { | ||
callbackFn(p); | ||
}); | ||
}); | ||
} | ||
|
||
it("auxiliaryCommentBefore", () => { | ||
const ast = parse(source); | ||
forEachParam(ast, p => (p.loc = null)); | ||
const output = generate(ast, { auxiliaryCommentBefore: "BEFORE" }).code; | ||
expect(output).toMatchInlineSnapshot(` | ||
"() => {}; | ||
|
||
( | ||
/*BEFORE*/ | ||
a) => {}; | ||
|
||
( | ||
/*BEFORE*/ | ||
a, | ||
/*BEFORE*/ | ||
b) => {}; | ||
|
||
async () => {}; | ||
|
||
async ( | ||
/*BEFORE*/ | ||
a) => {}; | ||
|
||
async ( | ||
/*BEFORE*/ | ||
a, | ||
/*BEFORE*/ | ||
b) => {};" | ||
`); | ||
}); | ||
it("auxiliaryCommentAfter", () => { | ||
const ast = parse(source); | ||
forEachParam(ast, p => (p.loc = null)); | ||
const output = generate(ast, { auxiliaryCommentAfter: "AFTER" }).code; | ||
expect(output).toMatchInlineSnapshot(` | ||
"() => {}; | ||
|
||
(a | ||
/*AFTER*/ | ||
) => {}; | ||
|
||
(a | ||
/*AFTER*/ | ||
, b | ||
/*AFTER*/ | ||
) => {}; | ||
|
||
async () => {}; | ||
|
||
async (a | ||
/*AFTER*/ | ||
) => {}; | ||
|
||
async (a | ||
/*AFTER*/ | ||
, b | ||
/*AFTER*/ | ||
) => {};" | ||
`); | ||
}); | ||
it("empty leadingComments array", () => { | ||
const ast = parse(source); | ||
forEachParam(ast, p => (p.leadingComments = [])); | ||
const output = generate(ast).code; | ||
expect(output).toMatchInlineSnapshot(` | ||
"() => {}; | ||
|
||
a => {}; | ||
|
||
(a, b) => {}; | ||
|
||
async () => {}; | ||
|
||
async a => {}; | ||
|
||
async (a, b) => {};" | ||
`); | ||
}); | ||
it("empty trailingComments array", () => { | ||
const ast = parse(source); | ||
forEachParam(ast, p => (p.trailingComments = [])); | ||
const output = generate(ast).code; | ||
expect(output).toMatchInlineSnapshot(` | ||
"() => {}; | ||
|
||
a => {}; | ||
|
||
(a, b) => {}; | ||
|
||
async () => {}; | ||
|
||
async a => {}; | ||
|
||
async (a, b) => {};" | ||
`); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
var fn = async ( | ||
arg | ||
) => {}; | ||
arg) => | ||
{}; | ||
|
||
async (x) => | ||
{}; | ||
|
||
async x => {}; | ||
async (x) => {}; | ||
|
||
async (x) => | ||
{}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const x = async ( // some comment | ||
a) => { | ||
a) => { | ||
return foo(await a); | ||
}; | ||
|
||
function foo(a) { | ||
return a; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
var _ref2; | ||
|
||
const { | ||
[(_ref) => { | ||
[_ref => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into why some of these arrow functions are losing their parens. It turns out that the previous implementation of Switching to |
||
let rest = babelHelpers.extends({}, _ref); | ||
let b = babelHelpers.extends({}, {}); | ||
}]: a, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ const _bar = bar(), | |
|
||
const { | ||
a | ||
} = foo((_ref) => { | ||
} = foo(_ref => { | ||
let { | ||
b | ||
} = _ref, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ const get = () => { | |
return 3; | ||
}; | ||
|
||
const f = (_ref) => { | ||
const f = _ref => { | ||
let { | ||
a = get(), | ||
b | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Edge | ||
(_ref) => { | ||
_ref => { | ||
var { | ||
x = 2 | ||
} = _ref; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(_ref) => { | ||
_ref => { | ||
var { | ||
x = 2 | ||
} = _ref; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(_ref) => { | ||
_ref => { | ||
let _ref$x = _ref.x, | ||
x = _ref$x === void 0 ? 2 : _ref$x; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(_ref) => { | ||
_ref => { | ||
let { | ||
x = 2 | ||
} = _ref; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't type-check because
predicate
isn't declared onArrowFunctionExpression
.