Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the special-case printing of single-param arrow functions (#…
…13204) * Simplify the special-case printing of single-param arrow functions * Update test fixtures with single-param arrow functions * Add some explicit snapshot tests for known edge-cases
- Loading branch information
Showing
14 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => {};" | ||
`); | ||
}); | ||
}); |
6 changes: 3 additions & 3 deletions
6
.../babel-generator/test/fixtures/edgecase/single-arg-async-arrow-with-retainlines/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => | ||
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/babel-generator/test/fixtures/parentheses/async-arrow-function/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...lugin-proposal-object-rest-spread/test/fixtures/object-rest/nested-computed-key/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ugin-proposal-object-rest-spread/test/fixtures/object-rest/nested-default-value/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
2 changes: 1 addition & 1 deletion
2
packages/babel-preset-env/test/fixtures/bugfixes/_esmodules-no-bugfixes/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Edge | ||
(_ref) => { | ||
_ref => { | ||
var { | ||
x = 2 | ||
} = _ref; | ||
|
2 changes: 1 addition & 1 deletion
2
packages/babel-preset-env/test/fixtures/bugfixes/edge-default-params-chrome-40/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(_ref) => { | ||
_ref => { | ||
var { | ||
x = 2 | ||
} = _ref; | ||
|
2 changes: 1 addition & 1 deletion
2
packages/babel-preset-env/test/fixtures/bugfixes/edge-default-params-edge-14/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
2 changes: 1 addition & 1 deletion
2
...babel-preset-env/test/fixtures/bugfixes/edge-default-params-edge-17-no-bugfixes/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(_ref) => { | ||
_ref => { | ||
let { | ||
x = 2 | ||
} = _ref; | ||
|