/
cloneNode.ts
158 lines (140 loc) 路 3.9 KB
/
cloneNode.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
import { NODE_FIELDS } from "../definitions";
import type * as t from "..";
import { isFile, isIdentifier } from "../validators/generated";
const has = Function.call.bind(Object.prototype.hasOwnProperty);
// This function will never be called for comments, only for real nodes.
function cloneIfNode(obj, deep, withoutLoc, commentsCache) {
if (obj && typeof obj.type === "string") {
return cloneNodeInternal(obj, deep, withoutLoc, commentsCache || new Map());
}
return obj;
}
function cloneIfNodeOrArray(obj, deep, withoutLoc, commentsCache) {
commentsCache ??= new Map();
if (Array.isArray(obj)) {
return obj.map(node => cloneIfNode(node, deep, withoutLoc, commentsCache));
}
return cloneIfNode(obj, deep, withoutLoc, commentsCache);
}
/**
* Create a clone of a `node` including only properties belonging to the node.
* If the second parameter is `false`, cloneNode performs a shallow clone.
* If the third parameter is true, the cloned nodes exclude location properties.
*/
export default function cloneNode<T extends t.Node>(
node: T,
deep: boolean = true,
withoutLoc: boolean = false,
): T {
return cloneNodeInternal(node, deep, withoutLoc, new Map());
}
function cloneNodeInternal<T extends t.Node>(
node: T,
deep: boolean = true,
withoutLoc: boolean = false,
commentsCache: Map<t.Comment, t.Comment>,
): T {
if (!node) return node;
const { type } = node;
const newNode: any = { type: node.type };
// Special-case identifiers since they are the most cloned nodes.
if (isIdentifier(node)) {
newNode.name = node.name;
if (has(node, "optional") && typeof node.optional === "boolean") {
newNode.optional = node.optional;
}
if (has(node, "typeAnnotation")) {
newNode.typeAnnotation = deep
? cloneIfNodeOrArray(
node.typeAnnotation,
true,
withoutLoc,
commentsCache,
)
: node.typeAnnotation;
}
} else if (!has(NODE_FIELDS, type)) {
throw new Error(`Unknown node type: "${type}"`);
} else {
for (const field of Object.keys(NODE_FIELDS[type])) {
if (has(node, field)) {
if (deep) {
newNode[field] =
isFile(node) && field === "comments"
? maybeCloneComments(
node.comments,
deep,
withoutLoc,
commentsCache,
)
: cloneIfNodeOrArray(
node[field],
true,
withoutLoc,
commentsCache,
);
} else {
newNode[field] = node[field];
}
}
}
}
if (has(node, "loc")) {
if (withoutLoc) {
newNode.loc = null;
} else {
newNode.loc = node.loc;
}
}
if (has(node, "leadingComments")) {
newNode.leadingComments = maybeCloneComments(
node.leadingComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "innerComments")) {
newNode.innerComments = maybeCloneComments(
node.innerComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "trailingComments")) {
newNode.trailingComments = maybeCloneComments(
node.trailingComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "extra")) {
newNode.extra = {
...node.extra,
};
}
return newNode;
}
function maybeCloneComments<T extends t.Comment>(
comments: ReadonlyArray<T> | null,
deep: boolean,
withoutLoc: boolean,
commentsCache: Map<T, T>,
): ReadonlyArray<T> | null {
if (!comments || !deep) {
return comments;
}
return comments.map(comment => {
const cache = commentsCache.get(comment);
if (cache) return cache;
const { type, value, loc } = comment;
const ret = { type, value, loc } as T;
if (withoutLoc) {
ret.loc = null;
}
commentsCache.set(comment, ret);
return ret;
});
}