| 1 |
import { NODE_FIELDS } from "../definitions"; |
| 2 |
import type * as t from ".."; |
| 3 |
import { isFile, isIdentifier } from "../validators/generated"; |
| 4 |
|
| 5 |
const has = Function.call.bind(Object.prototype.hasOwnProperty); |
| 6 |
|
| 7 |
type CommentCache = Map<t.Comment, t.Comment>; |
| 8 |
|
| 9 |
// This function will never be called for comments, only for real nodes. |
| 10 |
function cloneIfNode( |
| 11 |
obj: t.Node | undefined | null, |
| 12 |
deep: boolean, |
| 13 |
withoutLoc: boolean, |
| 14 |
commentsCache: CommentCache, |
| 15 |
) { |
| 16 |
if (obj && typeof obj.type === "string") { |
| 17 |
return cloneNodeInternal(obj, deep, withoutLoc, commentsCache); |
| 18 |
} |
| 19 |
|
| 20 |
return obj; |
| 21 |
} |
| 22 |
|
| 23 |
function cloneIfNodeOrArray( |
| 24 |
obj: t.Node | undefined | null | (t.Node | undefined | null)[], |
| 25 |
deep: boolean, |
| 26 |
withoutLoc: boolean, |
| 27 |
commentsCache: CommentCache, |
| 28 |
) { |
| 29 |
if (Array.isArray(obj)) { |
| 30 |
return obj.map(node => cloneIfNode(node, deep, withoutLoc, commentsCache)); |
| 31 |
} |
| 32 |
return cloneIfNode(obj, deep, withoutLoc, commentsCache); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Create a clone of a `node` including only properties belonging to the node. |
| 37 |
* If the second parameter is `false`, cloneNode performs a shallow clone. |
| 38 |
* If the third parameter is true, the cloned nodes exclude location properties. |
| 39 |
*/ |
| 40 |
export default function cloneNode<T extends t.Node>( |
| 41 |
node: T, |
| 42 |
deep: boolean = true, |
| 43 |
withoutLoc: boolean = false, |
| 44 |
): T { |
| 45 |
return cloneNodeInternal(node, deep, withoutLoc, new Map()); |
| 46 |
} |
| 47 |
|
| 48 |
function cloneNodeInternal<T extends t.Node>( |
| 49 |
node: T, |
| 50 |
deep: boolean = true, |
| 51 |
withoutLoc: boolean = false, |
| 52 |
commentsCache: CommentCache, |
| 53 |
): T { |
| 54 |
if (!node) return node; |
| 55 |
|
| 56 |
const { type } = node; |
| 57 |
const newNode: any = { type: node.type }; |
| 58 |
|
| 59 |
// Special-case identifiers since they are the most cloned nodes. |
| 60 |
if (isIdentifier(node)) { |
| 61 |
newNode.name = node.name; |
| 62 |
|
| 63 |
if (has(node, "optional") && typeof node.optional === "boolean") { |
| 64 |
newNode.optional = node.optional; |
| 65 |
} |
| 66 |
|
| 67 |
if (has(node, "typeAnnotation")) { |
| 68 |
newNode.typeAnnotation = deep |
| 69 |
? cloneIfNodeOrArray( |
| 70 |
node.typeAnnotation, |
| 71 |
true, |
| 72 |
withoutLoc, |
| 73 |
commentsCache, |
| 74 |
) |
| 75 |
: node.typeAnnotation; |
| 76 |
} |
| 77 |
} else if (!has(NODE_FIELDS, type)) { |
| 78 |
throw new Error(`Unknown node type: "${type}"`); |
| 79 |
} else { |
| 80 |
for (const field of Object.keys(NODE_FIELDS[type])) { |
| 81 |
if (has(node, field)) { |
| 82 |
if (deep) { |
| 83 |
newNode[field] = |
| 84 |
isFile(node) && field === "comments" |
| 85 |
? maybeCloneComments( |
| 86 |
node.comments, |
| 87 |
deep, |
| 88 |
withoutLoc, |
| 89 |
commentsCache, |
| 90 |
) |
| 91 |
: cloneIfNodeOrArray( |
| 92 |
// @ts-expect-error node[field] has been guarded by has check |
| 93 |
node[field], |
| 94 |
true, |
| 95 |
withoutLoc, |
| 96 |
commentsCache, |
| 97 |
); |
| 98 |
} else { |
| 99 |
newNode[field] = |
| 100 |
// @ts-expect-error node[field] has been guarded by has check |
| 101 |
node[field]; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (has(node, "loc")) { |
| 108 |
if (withoutLoc) { |
| 109 |
newNode.loc = null; |
| 110 |
} else { |
| 111 |
newNode.loc = node.loc; |
| 112 |
} |
| 113 |
} |
| 114 |
if (has(node, "leadingComments")) { |
| 115 |
newNode.leadingComments = maybeCloneComments( |
| 116 |
node.leadingComments, |
| 117 |
deep, |
| 118 |
withoutLoc, |
| 119 |
commentsCache, |
| 120 |
); |
| 121 |
} |
| 122 |
if (has(node, "innerComments")) { |
| 123 |
newNode.innerComments = maybeCloneComments( |
| 124 |
node.innerComments, |
| 125 |
deep, |
| 126 |
withoutLoc, |
| 127 |
commentsCache, |
| 128 |
); |
| 129 |
} |
| 130 |
if (has(node, "trailingComments")) { |
| 131 |
newNode.trailingComments = maybeCloneComments( |
| 132 |
node.trailingComments, |
| 133 |
deep, |
| 134 |
withoutLoc, |
| 135 |
commentsCache, |
| 136 |
); |
| 137 |
} |
| 138 |
if (has(node, "extra")) { |
| 139 |
newNode.extra = { |
| 140 |
...node.extra, |
| 141 |
}; |
| 142 |
} |
| 143 |
|
| 144 |
return newNode; |
| 145 |
} |
| 146 |
|
| 147 |
function maybeCloneComments<T extends t.Comment>( |
| 148 |
comments: ReadonlyArray<T> | null, |
| 149 |
deep: boolean, |
| 150 |
withoutLoc: boolean, |
| 151 |
commentsCache: Map<T, T>, |
| 152 |
): ReadonlyArray<T> | null { |
| 153 |
if (!comments || !deep) { |
| 154 |
return comments; |
| 155 |
} |
| 156 |
return comments.map(comment => { |
| 157 |
const cache = commentsCache.get(comment); |
| 158 |
if (cache) return cache; |
| 159 |
|
| 160 |
const { type, value, loc } = comment; |
| 161 |
|
| 162 |
const ret = { type, value, loc } as T; |
| 163 |
if (withoutLoc) { |
| 164 |
ret.loc = null; |
| 165 |
} |
| 166 |
|
| 167 |
commentsCache.set(comment, ret); |
| 168 |
|
| 169 |
return ret; |
| 170 |
}); |
| 171 |
} |