-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
removal.js
147 lines (133 loc) 路 3.78 KB
/
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
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
import { parse } from "@babel/parser";
import _traverse from "../lib/index.js";
import _generate from "@babel/generator";
const traverse = _traverse.default || _traverse;
const generate = _generate.default || _generate;
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("");
});
it("leading comments", function () {
const ast = parse(`
// update-tsconfig-file
const a = 5;
// const updateTSConfig = require('../update-tsconfig')
// https://nextjs.org/docs/app/building-your-application/upgrading/from-vite
const getValue = (a) => a.value;
getValue();
`);
traverse(ast, {
VariableDeclarator: function (path) {
path.remove();
},
});
expect(ast.program.body[0].leadingComments).toMatchInlineSnapshot(`
Array [
Object {
"end": 28,
"loc": SourceLocation {
"end": Position {
"column": 27,
"index": 28,
"line": 2,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 4,
"index": 5,
"line": 2,
},
},
"start": 5,
"type": "CommentLine",
"value": " update-tsconfig-file",
},
Object {
"end": 105,
"loc": SourceLocation {
"end": Position {
"column": 59,
"index": 105,
"line": 4,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 4,
"index": 50,
"line": 4,
},
},
"start": 50,
"type": "CommentLine",
"value": " const updateTSConfig = require('../update-tsconfig')",
},
Object {
"end": 186,
"loc": SourceLocation {
"end": Position {
"column": 80,
"index": 186,
"line": 5,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 4,
"index": 110,
"line": 5,
},
},
"start": 110,
"type": "CommentLine",
"value": " https://nextjs.org/docs/app/building-your-application/upgrading/from-vite",
},
]
`);
});
describe("within an IfStatement", function () {
it("does not make consequent null", function () {
const rootPath = getPath("if (x) foo(); else bar();");
const ifPath = rootPath.get("body.0");
ifPath.get("consequent").remove();
expect(ifPath.get("consequent").type).toBe("BlockStatement");
});
it("completely removes alternate", function () {
const rootPath = getPath("if (x) foo(); else bar();");
const ifPath = rootPath.get("body.0");
ifPath.get("alternate").remove();
expect(ifPath.get("alternate").node).toBeNull();
});
});
});