/
ast-utils.test.ts
323 lines (289 loc) · 7.97 KB
/
ast-utils.test.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"use strict";
import * as j from "jscodeshift/dist/core";
import * as utils from "../ast-utils";
import { INode } from "../types/NodePath";
describe("utils", () => {
describe("createProperty", () => {
it("should create properties for Boolean", () => {
const res = utils.createProperty(j, "foo", true);
expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
});
it("should create properties for Number", () => {
const res = utils.createProperty(j, "foo", -1);
expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
});
it("should create properties for String", () => {
const res = utils.createProperty(j, "foo", "bar");
expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
});
it("should create properties for complex keys", () => {
const res = utils.createProperty(j, "foo-bar", "bar");
expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
});
it("should create properties for non-literal keys", () => {
const res = utils.createProperty(j, 1, "bar");
expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
});
});
describe("findPluginsByName", () => {
it("should find plugins in AST", () => {
const ast = j(`
{ foo: new TerserPlugin() }
`);
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
expect(res.size()).toEqual(1);
});
it("should find all plugins in AST", () => {
const ast = j(`
[
new TerserPlugin(),
new TestPlugin()
]
`);
const res = utils.findPluginsByName(j, ast, ["TerserPlugin", "TestPlugin"]);
expect(res.size()).toEqual(2);
});
it("should not find false positives", () => {
const ast = j(`
{ foo: new webpack.optimize.TerserPlugin() }
`);
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
expect(res.size()).toEqual(0);
});
});
describe("findPluginsArrayAndRemoveIfEmpty", () => {
it("should remove plugins property", () => {
const ast = j(`
const a = {
plugins: []
}
`);
utils.findPluginsArrayAndRemoveIfEmpty(j, ast);
expect(ast.toSource()).toMatchSnapshot();
});
it("It should not remove plugins array, given an array with length greater than zero", () => {
const ast = j(`
const a = {
plugins: [
new MyCustomPlugin()
]
}
`);
utils.findPluginsArrayAndRemoveIfEmpty(j, ast);
expect(ast.toSource()).toMatchSnapshot();
});
});
describe("findRootNodesByName", () => {
it("should find plugins: [] nodes", () => {
const ast = j(`
const a = { plugins: [], foo: { plugins: [] } }
`);
const res = utils.findRootNodesByName(j, ast, "plugins");
expect(res.size()).toEqual(2);
});
it("should not find plugins: [] nodes", () => {
const ast = j(`
const a = { plugs: [] }
`);
const res = utils.findRootNodesByName(j, ast, "plugins");
expect(res.size()).toEqual(0);
});
});
describe("createOrUpdatePluginByName", () => {
it("should create a new plugin without arguments", () => {
const ast = j("{ plugins: [] }");
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin");
});
expect(ast.toSource()).toMatchSnapshot();
});
it("should create a new plugin with arguments", () => {
const ast = j("{ plugins: [] }");
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
foo: "bar",
});
});
expect(ast.toSource()).toMatchSnapshot();
});
it("should add an object as an argument", () => {
const ast = j("[new Plugin()]");
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
foo: true,
});
});
expect(ast.toSource()).toMatchSnapshot();
});
it("should merge options objects", () => {
const ast = j("[new Plugin({ foo: true })]");
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
bar: "baz",
foo: false,
});
utils.createOrUpdatePluginByName(j, node, "Plugin", {
"baz-long": true,
});
});
expect(ast.toSource()).toMatchSnapshot();
});
});
describe("findVariableToPlugin", () => {
it("should find the variable name of a plugin", () => {
const ast = j(`
const packageName = require('package-name');
const someOtherconst = somethingElse;
const otherPackage = require('other-package');
`);
const found = utils.findVariableToPlugin(j, ast, "other-package");
expect(found).toEqual("otherPackage");
});
});
describe("createLiteral", () => {
it("should create basic literal", () => {
const literal = utils.createLiteral(j, "stringLiteral");
expect(j(literal).toSource()).toMatchSnapshot();
});
it("should create boolean", () => {
const literal = utils.createLiteral(j, "true");
expect(j(literal).toSource()).toMatchSnapshot();
});
});
describe("createIdentifierOrLiteral", () => {
it("should create basic literal", () => {
const literal = utils.createIdentifierOrLiteral(j, "'stringLiteral'");
expect(j(literal).toSource()).toMatchSnapshot();
});
it("should create boolean", () => {
const literal = utils.createIdentifierOrLiteral(j, "true");
expect(j(literal).toSource()).toMatchSnapshot();
});
});
describe("findObjWithOneOfKeys", () => {
it("should find keys", () => {
const ast = j(`
const ab = {
a: 1,
b: 2
}
`);
expect(
ast
.find(j.ObjectExpression)
.filter((p) => utils.findObjWithOneOfKeys(p, ["a"]))
.size(),
).toEqual(1);
});
});
describe("getRequire", () => {
it("should create a require statement", () => {
const require = utils.getRequire(j, "filesys", "fs");
expect(j(require).toSource()).toMatchSnapshot();
});
});
describe("safeTraverse", () => {
it("should safe traverse", () => {
const testObject = {
type: "NodeType",
};
const p = {
foo: {
bar: testObject,
},
};
const require = utils.safeTraverse(p, ["foo", "bar"]);
expect(require).toEqual(testObject);
});
it("should safe traverse thrice", () => {
const type = {
type: "NodeType",
};
const p = {
parent: {
value: {
value: type,
},
},
};
const traversedValue = utils.safeTraverse(p, ["parent", "value", "value"]);
expect(traversedValue).toEqual(type);
});
});
describe("safeTraverseAndGetType", () => {
it("should safe traverse and get type", () => {
const NODE_TYPE = "NodeType";
const p = {
value: {
value: {
type: NODE_TYPE,
},
},
};
const typeValue = utils.safeTraverseAndGetType(p);
expect(typeValue).toEqual(NODE_TYPE);
});
it("should safe traverse and return false if not found", () => {
const NODE_TYPE = "NodeType";
const p = {
foo: {
bar: {
type: NODE_TYPE,
},
},
};
const typeValue = utils.safeTraverseAndGetType(p);
expect(typeValue).toEqual(false);
});
});
describe("addProperty", () => {
it("add entry property using init", () => {
const ast = j("module.exports = {}");
const propertyValue = {
man: "() => duper",
nice: "':)'",
objects: "are",
super: [
"yeah",
{
loader: "'eslint-loader'",
},
],
};
const root = ast.find(j.ObjectExpression);
root.forEach((p) => {
utils.addProperty(j, p, "entry", propertyValue);
});
expect(ast.toSource()).toMatchSnapshot();
});
it("add entry property using add", () => {
const ast = j(`module.exports = {
entry: {
objects: are,
super: [yeah, {
options: {
formatter: 'someOption'
}
}],
man: () => duper
}
}`);
const propertyValue = {
man: "() => duper",
nice: "':)'",
objects: "are",
super: [
"yeah",
{
loader: "'eslint-loader'",
},
],
};
const root = ast.find(j.ObjectExpression);
utils.findRootNodesByName(j, root, "entry").forEach((p: INode) => {
j(p).replaceWith(utils.addProperty(j, p, "entry", propertyValue, "add"));
});
expect(ast.toSource()).toMatchSnapshot();
});
});
});