| 1 |
import { parse } from "@babel/parser"; |
| 2 |
|
| 3 |
import _generate from "../lib/index.js"; |
| 4 |
const generate = _generate.default || _generate; |
| 5 |
|
| 6 |
describe("parameter parentheses", () => { |
| 7 |
|
| 8 |
const source = ` |
| 9 |
() => {}; |
| 10 |
a => {}; |
| 11 |
(a, b) => {}; |
| 12 |
async () => {}; |
| 13 |
async a => {}; |
| 14 |
async (a, b) => {}; |
| 15 |
`; |
| 16 |
|
| 17 |
function forEachParam(ast, callbackFn) { |
| 18 |
ast.program.body.forEach(s => { |
| 19 |
s.expression.params.forEach(p => { |
| 20 |
callbackFn(p); |
| 21 |
}); |
| 22 |
}); |
| 23 |
} |
| 24 |
|
| 25 |
it("auxiliaryCommentBefore", () => { |
| 26 |
const ast = parse(source); |
| 27 |
forEachParam(ast, p => (p.loc = null)); |
| 28 |
const output = generate(ast, { auxiliaryCommentBefore: "BEFORE" }).code; |
| 29 |
expect(output).toMatchInlineSnapshot(` |
| 30 |
"() => {}; |
| 31 |
/*BEFORE*/ |
| 32 |
a => {}; |
| 33 |
( |
| 34 |
/*BEFORE*/ |
| 35 |
a, |
| 36 |
/*BEFORE*/ |
| 37 |
b) => {}; |
| 38 |
async () => {}; |
| 39 |
async /*BEFORE*/a => {}; |
| 40 |
async ( |
| 41 |
/*BEFORE*/ |
| 42 |
a, |
| 43 |
/*BEFORE*/ |
| 44 |
b) => {};" |
| 45 |
`); |
| 46 |
}); |
| 47 |
it("auxiliaryCommentAfter", () => { |
| 48 |
const ast = parse(source); |
| 49 |
forEachParam(ast, p => (p.loc = null)); |
| 50 |
const output = generate(ast, { auxiliaryCommentAfter: "AFTER" }).code; |
| 51 |
expect(output).toMatchInlineSnapshot(` |
| 52 |
"() => {}; |
| 53 |
a /*AFTER*/=> {}; |
| 54 |
(a |
| 55 |
/*AFTER*/ |
| 56 |
, b |
| 57 |
/*AFTER*/ |
| 58 |
) => {}; |
| 59 |
async () => {}; |
| 60 |
async a /*AFTER*/=> {}; |
| 61 |
async (a |
| 62 |
/*AFTER*/ |
| 63 |
, b |
| 64 |
/*AFTER*/ |
| 65 |
) => {};" |
| 66 |
`); |
| 67 |
}); |
| 68 |
it("empty leadingComments array", () => { |
| 69 |
const ast = parse(source); |
| 70 |
forEachParam(ast, p => (p.leadingComments = [])); |
| 71 |
const output = generate(ast).code; |
| 72 |
expect(output).toMatchInlineSnapshot(` |
| 73 |
"() => {}; |
| 74 |
a => {}; |
| 75 |
(a, b) => {}; |
| 76 |
async () => {}; |
| 77 |
async a => {}; |
| 78 |
async (a, b) => {};" |
| 79 |
`); |
| 80 |
}); |
| 81 |
it("empty trailingComments array", () => { |
| 82 |
const ast = parse(source); |
| 83 |
forEachParam(ast, p => (p.trailingComments = [])); |
| 84 |
const output = generate(ast).code; |
| 85 |
expect(output).toMatchInlineSnapshot(` |
| 86 |
"() => {}; |
| 87 |
a => {}; |
| 88 |
(a, b) => {}; |
| 89 |
async () => {}; |
| 90 |
async a => {}; |
| 91 |
async (a, b) => {};" |
| 92 |
`); |
| 93 |
}); |
| 94 |
}); |