| 1 | import { VISITOR_KEYSscip-typescript npm @babel/types 7.20.7 src/definitions/`utils.ts`/VISITOR_KEYS. } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 2 | import type * as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 3 | |
| 4 | type StackItemscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem# = { |
| 5 | nodescip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:node.: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#; |
| 6 | parentscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:parent.: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node# | null; |
| 7 | }; |
| 8 | export default function checkDuplicateNodesscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/checkDuplicateNodes().(astscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/checkDuplicateNodes().(ast): tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#) { |
| 9 | if (arguments.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/IArguments#length. !== 1) { |
| 10 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#("checkDuplicateNodes accepts only one argument: ast"); |
| 11 | } |
| 12 | // A Map from node to its parent |
| 13 | const parentsMap = new Mapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#(); |
| 14 | |
| 15 | const hidePrivateProperties = (key: string, val: unknown) => { |
| 16 | // Hides properties like _shadowedFunctionLiteral, |
| 17 | // which makes the AST circular |
| 18 | if (key[0] === "_") return "[Private]"; |
| 19 | return val; |
| 20 | }; |
| 21 | |
| 22 | const stack: StackItemscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#[] = [{ nodescip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:node.: astscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/checkDuplicateNodes().(ast), parentscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:parent.: null }]; |
| 23 | let item; |
| 24 | |
| 25 | while ((item = stack.popscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#pop().()) !== undefined) { |
| 26 | const { nodescip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:node., parentscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:parent. } = item; |
| 27 | if (!node) continue; |
| 28 | |
| 29 | const keys = VISITOR_KEYSscip-typescript npm @babel/types 7.20.7 src/definitions/`utils.ts`/VISITOR_KEYS.[node.typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type.]; |
| 30 | if (!keys) continue; |
| 31 | |
| 32 | if (parentsMap.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#has().(node)) { |
| 33 | const parents = [parentsMap.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#get().(node), parent]; |
| 34 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#( |
| 35 | "Do not reuse nodes. Use `t.cloneNode` (or `t.clone`/`t.cloneDeep` if using babel@6) to copy them.\n" + |
| 36 | JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(node, hidePrivateProperties, 2) + |
| 37 | "\nParent:\n" + |
| 38 | JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(parents, hidePrivateProperties, 2), |
| 39 | ); |
| 40 | } |
| 41 | parentsMap.setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#set().(node, parent); |
| 42 | |
| 43 | for (const key of keys) { |
| 44 | const subNode = |
| 45 | // @ts-expect-error visitor keys must present in node |
| 46 | node[key]; |
| 47 | |
| 48 | if (Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.isArrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ArrayConstructor#isArray().(subNode)) { |
| 49 | for (const child of subNode) { |
| 50 | stack.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ nodescip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:node.: child, parentscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:parent.: node }); |
| 51 | } |
| 52 | } else if (typeof subNode === "object" && subNode !== null) { |
| 53 | stack.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().({ nodescip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:node.: subNode, parentscip-typescript npm @babel/helper-check-duplicate-nodes 7.18.6 src/`index.ts`/StackItem#typeLiteral0:parent.: node }); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |