context.js.map 8.24 KB
{"version":3,"names":["VISITOR_KEYS","TraversalContext","constructor","scope","opts","state","parentPath","queue","priorityQueue","shouldVisit","node","enter","exit","type","keys","length","key","create","container","listKey","NodePath","get","parent","maybeQueue","path","notPriority","push","visitMultiple","visitQueue","visitSingle","visited","WeakSet","stop","resync","contexts","pushContext","has","add","visit","popContext","nodes","Array","isArray"],"sources":["../src/context.ts"],"sourcesContent":["import NodePath from \"./path\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type Scope from \"./scope\";\nimport type { TraverseOptions } from \".\";\nimport type * as t from \"@babel/types\";\nimport type { Visitor } from \"./types\";\n\nexport default class TraversalContext<S = unknown> {\n  constructor(\n    scope: Scope,\n    opts: TraverseOptions,\n    state: S,\n    parentPath: NodePath,\n  ) {\n    this.parentPath = parentPath;\n    this.scope = scope;\n    this.state = state;\n    this.opts = opts;\n  }\n\n  declare parentPath: NodePath;\n  declare scope: Scope;\n  declare state: S;\n  declare opts: TraverseOptions;\n  queue: Array<NodePath> | null = null;\n  priorityQueue: Array<NodePath> | null = null;\n\n  /**\n   * This method does a simple check to determine whether or not we really need to attempt\n   * visit a node. This will prevent us from constructing a NodePath.\n   */\n\n  shouldVisit(node: t.Node): boolean {\n    const opts = this.opts as Visitor;\n    if (opts.enter || opts.exit) return true;\n\n    // check if we have a visitor for this node\n    if (opts[node.type]) return true;\n\n    // check if we're going to traverse into this node\n    const keys: Array<string> | undefined = VISITOR_KEYS[node.type];\n    if (!keys?.length) return false;\n\n    // we need to traverse into this node so ensure that it has children to traverse into!\n    for (const key of keys) {\n      if (\n        // @ts-expect-error key is from visitor keys\n        node[key]\n      ) {\n        return true;\n      }\n    }\n\n    return false;\n  }\n\n  create(\n    node: t.Node,\n    container: t.Node | t.Node[],\n    key: string | number,\n    listKey?: string,\n  ): NodePath {\n    // We don't need to `.setContext()` here, since `.visitQueue()` already\n    // calls `.pushContext`.\n    return NodePath.get({\n      parentPath: this.parentPath,\n      parent: node,\n      container,\n      key: key,\n      listKey,\n    });\n  }\n\n  maybeQueue(path: NodePath, notPriority?: boolean) {\n    if (this.queue) {\n      if (notPriority) {\n        this.queue.push(path);\n      } else {\n        this.priorityQueue.push(path);\n      }\n    }\n  }\n\n  visitMultiple(container: t.Node[], parent: t.Node, listKey: string) {\n    // nothing to traverse!\n    if (container.length === 0) return false;\n\n    const queue = [];\n\n    // build up initial queue\n    for (let key = 0; key < container.length; key++) {\n      const node = container[key];\n      if (node && this.shouldVisit(node)) {\n        queue.push(this.create(parent, container, key, listKey));\n      }\n    }\n\n    return this.visitQueue(queue);\n  }\n\n  visitSingle(node: t.Node, key: string): boolean {\n    if (\n      this.shouldVisit(\n        // @ts-expect-error key may not index node\n        node[key],\n      )\n    ) {\n      return this.visitQueue([this.create(node, node, key)]);\n    } else {\n      return false;\n    }\n  }\n\n  visitQueue(queue: Array<NodePath>): boolean {\n    // set queue\n    this.queue = queue;\n    this.priorityQueue = [];\n\n    const visited = new WeakSet();\n    let stop = false;\n\n    // visit the queue\n    for (const path of queue) {\n      path.resync();\n\n      if (\n        path.contexts.length === 0 ||\n        path.contexts[path.contexts.length - 1] !== this\n      ) {\n        // The context might already have been pushed when this path was inserted and queued.\n        // If we always re-pushed here, we could get duplicates and risk leaving contexts\n        // on the stack after the traversal has completed, which could break things.\n        path.pushContext(this);\n      }\n\n      // this path no longer belongs to the tree\n      if (path.key === null) continue;\n\n      // ensure we don't visit the same node twice\n      const { node } = path;\n      if (visited.has(node)) continue;\n      if (node) visited.add(node);\n\n      if (path.visit()) {\n        stop = true;\n        break;\n      }\n\n      if (this.priorityQueue.length) {\n        stop = this.visitQueue(this.priorityQueue);\n        this.priorityQueue = [];\n        this.queue = queue;\n        if (stop) break;\n      }\n    }\n\n    // clear queue\n    for (const path of queue) {\n      path.popContext();\n    }\n\n    // clear queue\n    this.queue = null;\n\n    return stop;\n  }\n\n  visit(node: t.Node, key: string) {\n    // @ts-expect-error key may not index node\n    const nodes = node[key] as t.Node | t.Node[] | null;\n    if (!nodes) return false;\n\n    if (Array.isArray(nodes)) {\n      return this.visitMultiple(nodes, node, key);\n    } else {\n      return this.visitSingle(node, key);\n    }\n  }\n}\n"],"mappings":";;;;;;AAAA;AACA;AAA4C;EAAnCA;AAAY;AAMN,MAAMC,gBAAgB,CAAc;EACjDC,WAAW,CACTC,KAAY,EACZC,IAAqB,EACrBC,KAAQ,EACRC,UAAoB,EACpB;IAAA,KAWFC,KAAK,GAA2B,IAAI;IAAA,KACpCC,aAAa,GAA2B,IAAI;IAX1C,IAAI,CAACF,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACH,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACE,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACD,IAAI,GAAGA,IAAI;EAClB;;EAcAK,WAAW,CAACC,IAAY,EAAW;IACjC,MAAMN,IAAI,GAAG,IAAI,CAACA,IAAe;IACjC,IAAIA,IAAI,CAACO,KAAK,IAAIP,IAAI,CAACQ,IAAI,EAAE,OAAO,IAAI;;IAGxC,IAAIR,IAAI,CAACM,IAAI,CAACG,IAAI,CAAC,EAAE,OAAO,IAAI;;IAGhC,MAAMC,IAA+B,GAAGd,YAAY,CAACU,IAAI,CAACG,IAAI,CAAC;IAC/D,IAAI,EAACC,IAAI,YAAJA,IAAI,CAAEC,MAAM,GAAE,OAAO,KAAK;;IAG/B,KAAK,MAAMC,GAAG,IAAIF,IAAI,EAAE;MACtB;MAEEJ,IAAI,CAACM,GAAG,CAAC,EACT;QACA,OAAO,IAAI;MACb;IACF;IAEA,OAAO,KAAK;EACd;EAEAC,MAAM,CACJP,IAAY,EACZQ,SAA4B,EAC5BF,GAAoB,EACpBG,OAAgB,EACN;IAGV,OAAOC,aAAQ,CAACC,GAAG,CAAC;MAClBf,UAAU,EAAE,IAAI,CAACA,UAAU;MAC3BgB,MAAM,EAAEZ,IAAI;MACZQ,SAAS;MACTF,GAAG,EAAEA,GAAG;MACRG;IACF,CAAC,CAAC;EACJ;EAEAI,UAAU,C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