-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
replacement.js
188 lines (178 loc) 路 5.86 KB
/
replacement.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import traverse from "../lib";
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import * as t from "@babel/types";
describe("path/replacement", function () {
describe("replaceWith", function () {
it("replaces declaration in ExportDefaultDeclaration node", function () {
const ast = parse("export default function() {};", {
sourceType: "module",
});
traverse(ast, {
FunctionDeclaration(path) {
path.replaceWith(
t.arrayExpression([
t.functionExpression(
path.node.id,
path.node.params,
path.node.body,
path.node.generator,
path.node.async,
),
]),
);
},
});
expect(ast.program.body[0].declaration.type).toBe("ArrayExpression");
});
it("throws error when trying to replace Program with a non-Program node", function () {
const ast = parse("var x = 3;");
expect(function () {
traverse(ast, {
Program(path) {
path.replaceWith(t.identifier("a"));
},
});
}).toThrow(
/You can only replace a Program root node with another Program node/,
);
});
it("throws error when used with an array of nodes", function () {
const ast = parse("function abc() {}; var test = 17;");
expect(function () {
traverse(ast, {
NumericLiteral(path) {
path.replaceWith([
t.identifier("should"),
t.identifier("never"),
t.identifier("happen"),
]);
},
});
}).toThrow(
/Don't use `path\.replaceWith\(\)` with an array of nodes, use `path\.replaceWithMultiple\(\)`/,
);
});
it("throws error when used with source string", function () {
const ast = parse(
"(function() { var x = 3; var y = 17; var c = x + y; })();",
);
expect(function () {
traverse(ast, {
BinaryExpression(path) {
path.replaceWith("17 + 23");
},
});
}).toThrow(
/Don't use `path\.replaceWith\(\)` with a source string, use `path\.replaceWithSourceString\(\)`/,
);
});
it("throws error when trying to replace removed node", function () {
const ast = parse("var z = 'abc';");
expect(function () {
traverse(ast, {
StringLiteral(path) {
path.remove();
path.replaceWith(t.identifier("p"));
},
});
}).toThrow(/You can't replace this node, we've already removed it/);
});
it("throws error when passed a falsy value", function () {
const ast = parse("var z = 'abc';");
expect(function () {
traverse(ast, {
StringLiteral(path) {
path.replaceWith();
},
});
}).toThrow(
/You passed `path\.replaceWith\(\)` a falsy node, use `path\.remove\(\)` instead/,
);
});
it("does not revisit the replaced node if it is the node being replaced", () => {
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWith(path.node);
},
});
expect(visitCounter).toBe(1);
});
// https://github.com/babel/babel/issues/12386
it("updates pathCache with the replaced node", () => {
const ast = parse(`() => (a?.b)?.c`, {
createParenthesizedExpressions: true,
});
traverse(ast, {
OptionalMemberExpression(path) {
path.node.type = "MemberExpression";
// force `replaceWith` to replace `path.node`
path.replaceWith(t.cloneNode(path.node));
path.parentPath.ensureBlock();
const aQuestionDotBNode = path.node.object.expression;
// avoid traversing to a?.b
aQuestionDotBNode.type = "MemberExpression";
},
ParenthesizedExpression(path) {
path.replaceWith(path.node.expression);
},
});
expect(generate(ast).code).toMatchInlineSnapshot(`
"() => {
return a.b.c;
};"
`);
});
});
describe("replaceWithMultiple", () => {
it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => {
const ast = parse(`<div><span><p></p><h></h></span></div>`, {
plugins: ["jsx"],
});
traverse(ast, {
JSXElement: path => {
if (path.node.openingElement.name.name === "span") {
path.replaceWithMultiple(path.node.children.filter(t.isJSXElement));
}
},
});
expect(generate(ast).code).toBe("<div><p></p><h></h></div>;");
});
it("does not revisit one of new nodes if it is the node being replaced and is the head of nodes", () => {
// packages/babel-plugin-transform-block-scoping/src/index.js relies on this behaviour
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWithMultiple([path.node, t.emptyStatement()]);
},
});
expect(visitCounter).toBe(1);
});
it("does not revisit one of new nodes if it is the node being replaced and is the tail of nodes", () => {
// packages/babel-plugin-transform-block-scoping/src/index.js relies on this behaviour
const ast = parse(`var x;`);
let visitCounter = 0;
traverse(ast, {
VariableDeclaration(path) {
visitCounter++;
if (visitCounter > 1) {
return true;
}
path.replaceWithMultiple([t.emptyStatement(), path.node]);
},
});
expect(visitCounter).toBe(1);
});
});
});