/
removal.js
48 lines (41 loc) 路 1007 Bytes
/
removal.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
import traverse from "../lib";
import { parse } from "@babel/parser";
import generate from "@babel/generator";
function getPath(code) {
const ast = parse(code);
let path;
traverse(ast, {
Program: function(_path) {
path = _path;
_path.stop();
},
});
return path;
}
function generateCode(path) {
return generate(path.node).code;
}
describe("removal", function() {
describe("ArrowFunction", function() {
it("remove body", function() {
const rootPath = getPath("x = () => b;");
const path = rootPath
.get("body")[0]
.get("expression")
.get("right");
const body = path.get("body");
body.remove();
expect(generateCode(rootPath)).toBe("x = () => {};");
});
});
it("remove with noScope", function() {
const ast = parse("a=1");
traverse(ast, {
AssignmentExpression: function(path) {
path.remove();
},
noScope: true,
});
expect(generate(ast).code).toBe("");
});
});