forked from babel/babel
/
arrow-functions.js
119 lines (97 loc) 路 2.23 KB
/
arrow-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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) => {};"
`);
});
});