forked from babel/babel
/
cloning.js
143 lines (129 loc) 路 4.69 KB
/
cloning.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
import * as t from "../lib";
import { parse } from "@babel/parser";
describe("cloneNode", function() {
it("should handle undefined", function() {
const node = undefined;
const cloned = t.cloneNode(node);
expect(cloned).toBeUndefined();
});
it("should handle null", function() {
const node = null;
const cloned = t.cloneNode(node);
expect(cloned).toBeNull();
});
it("should handle simple cases", function() {
const node = t.identifier("a");
const cloned = t.cloneNode(node);
expect(node).not.toBe(cloned);
expect(t.isNodesEquivalent(node, cloned)).toBe(true);
});
it("should handle full programs", function() {
const file = parse("1 + 1");
const cloned = t.cloneNode(file);
expect(file).not.toBe(cloned);
expect(file.program.body[0].expression.right).not.toBe(
cloned.program.body[0].expression.right,
);
expect(file.program.body[0].expression.left).not.toBe(
cloned.program.body[0].expression.left,
);
expect(t.isNodesEquivalent(file, cloned)).toBe(true);
});
it("should handle complex programs", function() {
const program = "'use strict'; function lol() { wow();return 1; }";
const node = parse(program);
const cloned = t.cloneNode(node);
expect(node).not.toBe(cloned);
expect(t.isNodesEquivalent(node, cloned)).toBe(true);
});
it("should handle missing array element", function() {
const node = parse("[,0]");
const cloned = t.cloneNode(node);
expect(node).not.toBe(cloned);
expect(t.isNodesEquivalent(node, cloned)).toBe(true);
});
it("should support shallow cloning", function() {
const node = t.memberExpression(t.identifier("foo"), t.identifier("bar"));
const cloned = t.cloneNode(node, /* deep */ false);
expect(node).not.toBe(cloned);
expect(node.object).toBe(cloned.object);
expect(node.property).toBe(cloned.property);
});
it("should preserve type annotations", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
typeAnnotation: t.anyTypeAnnotation(),
}),
]);
const cloned = t.cloneNode(node, /* deep */ true);
expect(cloned.declarations[0].id.typeAnnotation).toEqual(
node.declarations[0].id.typeAnnotation,
);
expect(cloned.declarations[0].id.typeAnnotation).not.toBe(
node.declarations[0].id.typeAnnotation,
);
});
it("should support shallow cloning without loc", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
typeAnnotation: t.anyTypeAnnotation(),
}),
]);
node.loc = {};
const cloned = t.cloneNode(node, /* deep */ false, /* withoutLoc */ true);
expect(cloned.loc).toBeNull();
});
it("should support deep cloning without loc", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
typeAnnotation: t.anyTypeAnnotation(),
}),
]);
node.loc = {};
node.declarations[0].id.loc = {};
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true);
expect(cloned.loc).toBeNull();
expect(cloned.declarations[0].id.loc).toBeNull();
});
it("should support deep cloning for comments", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
leadingComments: [{ loc: {} }],
innerComments: [{ loc: {} }],
trailingComments: [{ loc: {} }],
}),
]);
node.loc = {};
node.declarations[0].id.loc = {};
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ false);
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe(
node.declarations[0].id.leadingComments[0].loc,
);
expect(cloned.declarations[0].id.innerComments[0].loc).toBe(
node.declarations[0].id.innerComments[0].loc,
);
expect(cloned.declarations[0].id.trailingComments[0].loc).toBe(
node.declarations[0].id.trailingComments[0].loc,
);
});
it("should support deep cloning for comments without loc", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
leadingComments: [{ loc: {} }],
innerComments: [{ loc: {} }],
trailingComments: [{ loc: {} }],
}),
]);
node.loc = {};
node.declarations[0].id.loc = {};
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true);
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe(null);
expect(cloned.declarations[0].id.innerComments[0].loc).toBe(null);
expect(cloned.declarations[0].id.trailingComments[0].loc).toBe(null);
});
});