{"version":3,"names":["_index","require","_index2","hasOwn","Function","call","bind","Object","prototype","hasOwnProperty","cloneIfNode","obj","deep","withoutLoc","commentsCache","type","cloneNodeInternal","cloneIfNodeOrArray","Array","isArray","map","node","cloneNode","Map","newNode","isIdentifier","name","optional","typeAnnotation","decorators","NODE_FIELDS","Error","field","keys","isFile","maybeCloneComments","comments","loc","leadingComments","innerComments","trailingComments","extra","assign","comment","cache","get","value","ret","set"],"sources":["../../src/clone/cloneNode.ts"],"sourcesContent":["import { NODE_FIELDS } from \"../definitions/index.ts\";\nimport type * as t from \"../index.ts\";\nimport { isFile, isIdentifier } from \"../validators/generated/index.ts\";\n\nconst { hasOwn } = process.env.BABEL_8_BREAKING\n ? Object\n : { hasOwn: Function.call.bind(Object.prototype.hasOwnProperty) };\n\ntype CommentCache = Map;\n\n// This function will never be called for comments, only for real nodes.\nfunction cloneIfNode(\n obj: t.Node | undefined | null,\n deep: boolean,\n withoutLoc: boolean,\n commentsCache: CommentCache,\n) {\n if (obj && typeof obj.type === \"string\") {\n return cloneNodeInternal(obj, deep, withoutLoc, commentsCache);\n }\n\n return obj;\n}\n\nfunction cloneIfNodeOrArray(\n obj: t.Node | undefined | null | (t.Node | undefined | null)[],\n deep: boolean,\n withoutLoc: boolean,\n commentsCache: CommentCache,\n) {\n if (Array.isArray(obj)) {\n return obj.map(node => cloneIfNode(node, deep, withoutLoc, commentsCache));\n }\n return cloneIfNode(obj, deep, withoutLoc, commentsCache);\n}\n\n/**\n * Create a clone of a `node` including only properties belonging to the node.\n * If the second parameter is `false`, cloneNode performs a shallow clone.\n * If the third parameter is true, the cloned nodes exclude location properties.\n */\nexport default function cloneNode(\n node: T,\n deep: boolean = true,\n withoutLoc: boolean = false,\n): T {\n return cloneNodeInternal(node, deep, withoutLoc, new Map());\n}\n\nfunction cloneNodeInternal(\n node: T,\n deep: boolean = true,\n withoutLoc: boolean = false,\n commentsCache: CommentCache,\n): T {\n if (!node) return node;\n\n const { type } = node;\n const newNode: any = { type: node.type };\n\n // Special-case identifiers since they are the most cloned nodes.\n if (isIdentifier(node)) {\n newNode.name = node.name;\n\n if (hasOwn(node, \"optional\") && typeof node.optional === \"boolean\") {\n newNode.optional = node.optional;\n }\n\n if (hasOwn(node, \"typeAnnotation\")) {\n newNode.typeAnnotation = deep\n ? cloneIfNodeOrArray(\n node.typeAnnotation,\n true,\n withoutLoc,\n commentsCache,\n )\n : node.typeAnnotation;\n }\n\n if (hasOwn(node, \"decorators\")) {\n newNode.decorators = deep\n ? cloneIfNodeOrArray(node.decorators, true, withoutLoc, commentsCache)\n : node.decorators;\n }\n } else if (!hasOwn(NODE_FIELDS, type)) {\n throw new Error(`Unknown node type: \"${type}\"`);\n } else {\n for (const field of Object.keys(NODE_FIELDS[type])) {\n if (hasOwn(node, field)) {\n if (deep) {\n newNode[field] =\n isFile(node) && field === \"comments\"\n ? maybeCloneComments(\n node.comments,\n deep,\n withoutLoc,\n commentsCache,\n )\n : cloneIfNodeOrArray(\n // @ts-expect-error node[field] has been guarded by has check\n node[field],\n true,\n withoutLoc,\n commentsCache,\n );\n } else {\n newNode[field] =\n // @ts-expect-error node[field] has been guarded by has check\n node[field];\n }\n }\n }\n }\n\n if (hasOwn(node, \"loc\")) {\n if (withoutLoc) {\n newNode.loc = null;\n } else {\n newNode.loc = node.loc;\n }\n }\n if (hasOwn(node, \"leadingComments\")) {\n newNode.leadingComments = maybeCloneComments(\n node.leadingComments,\n deep,\n withoutLoc,\n commentsCache,\n );\n }\n if (hasOwn(node, \"innerComments\")) {\n newNode.innerComments = maybeCloneComments(\n node.innerComments,\n deep,\n withoutLoc,\n commentsCache,\n );\n }\n if (hasOwn(node, \"trailingComments\")) {\n newNode.trailingComments = maybeCloneComments(\n node.trailingComments,\n deep,\n withoutLoc,\n commentsCache,\n );\n }\n if (hasOwn(node, \"extra\")) {\n newNode.extra = {\n ...node.extra,\n };\n }\n\n return newNode;\n}\n\nfunction maybeCloneComments(\n comments: ReadonlyArray | null,\n deep: boolean,\n withoutLoc: boolean,\n commentsCache: Map,\n): ReadonlyArray | null {\n if (!comments || !deep) {\n return comments;\n }\n return comments.map(comment => {\n const cache = commentsCache.get(comment);\n if (cache) return cache;\n\n const { type, value, loc } = comment;\n\n const ret = { type, value, loc } as T;\n if (withoutLoc) {\n ret.loc = null;\n }\n\n commentsCache.set(comment, ret);\n\n return ret;\n });\n}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAD,OAAA;AAEA,MAAM;EAAEE;AAAO,CAAC,GAEZ;EAAEA,MAAM,EAAEC,QAAQ,CAACC,IAAI,CAACC,IAAI,CAACC,MAAM,CAACC,SAAS,CAACC,cAAc;AAAE,CAAC;AAKnE,SAASC,WAAWA,CAClBC,GAA8B,EAC9BC,IAAa,EACbC,UAAmB,EACnBC,aAA2B,EAC3B;EACA,IAAIH,GAAG,IAAI,OAAOA,GAAG,CAACI,IAAI,KAAK,QAAQ,EAAE;IACvC,OAAOC,iBAAiB,CAACL,GAAG,EAAEC,IAAI,EAAEC,UAAU,EAAEC,aAAa,CAAC;EAChE;EAEA,OAAOH,GAAG;AACZ;AAEA,SAASM,kBAAkBA,CACzBN,GAA8D,EAC9DC,IAAa,EACbC,UAAmB,EACnBC,aAA2B,EAC3B;EACA,IAAII,KAAK,CAACC,OAAO,CAACR,GAAG,CAAC,EAAE;IACtB,OAAOA,GAAG,CAACS,GAAG,CAACC,IAAI,IAAIX,WAAW,CAACW,IAAI,EAAET,IAAI,EAAEC,UAAU,EAAEC,aAAa,CAAC,CAAC;EAC5E;EACA,OAAOJ,WAAW,CAACC,GAAG,EAAEC,IAAI,EAAEC,UAAU,EAAEC,aAAa,CAAC;AAC1D;AAOe,SAASQ,SAASA,CAC/BD,IAAO,EACPT,IAAa,GAAG,IAAI,EACpBC,UAAmB,GAAG,KAAK,EACxB;EACH,OAAOG,iBAAiB,CAACK,IAAI,EAAET,IAAI,EAAEC,UAAU,EAAE,IAAIU,GAAG,CAA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