All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.lib.transformation.block-hoist-plugin.js.map Maven / Gradle / Ivy

There is a newer version: 7.25.2
Show newest version
{"version":3,"names":["_traverse","data","require","_plugin","LOADED_PLUGIN","blockHoistPlugin","name","visitor","Block","exit","node","body","performHoisting","SwitchCase","consequent","max","Math","pow","hasChange","i","length","n","p","priority","stableSort","slice","loadBlockHoistPlugin","Plugin","Object","assign","traverse","explode","bodyNode","_blockHoist","buckets","create","bucket","push","keys","map","k","sort","a","b","index","key"],"sources":["../../src/transformation/block-hoist-plugin.ts"],"sourcesContent":["import traverse from \"@babel/traverse\";\nimport type { Statement } from \"@babel/types\";\nimport type { PluginObject } from \"../config/index.ts\";\nimport Plugin from \"../config/plugin.ts\";\n\nlet LOADED_PLUGIN: Plugin | void;\n\nconst blockHoistPlugin: PluginObject = {\n  /**\n   * [Please add a description.]\n   *\n   * Priority:\n   *\n   *  - 0 We want this to be at the **very** bottom\n   *  - 1 Default node position\n   *  - 2 Priority over normal nodes\n   *  - 3 We want this to be at the **very** top\n   *  - 4 Reserved for the helpers used to implement module imports.\n   */\n\n  name: \"internal.blockHoist\",\n\n  visitor: {\n    Block: {\n      exit({ node }) {\n        node.body = performHoisting(node.body);\n      },\n    },\n    SwitchCase: {\n      exit({ node }) {\n        // In case statements, hoisting is difficult to perform correctly due to\n        // functions that are declared and referenced in different blocks.\n        // Nevertheless, hoisting the statements *inside* of each case should at\n        // least mitigate the failure cases.\n        node.consequent = performHoisting(node.consequent);\n      },\n    },\n  },\n};\n\nfunction performHoisting(body: Statement[]): Statement[] {\n  // Largest SMI\n  let max = 2 ** 30 - 1;\n  let hasChange = false;\n  for (let i = 0; i < body.length; i++) {\n    const n = body[i];\n    const p = priority(n);\n    if (p > max) {\n      hasChange = true;\n      break;\n    }\n    max = p;\n  }\n  if (!hasChange) return body;\n\n  // My kingdom for a stable sort!\n  return stableSort(body.slice());\n}\n\nexport default function loadBlockHoistPlugin(): Plugin {\n  if (!LOADED_PLUGIN) {\n    // cache the loaded blockHoist plugin plugin\n    LOADED_PLUGIN = new Plugin(\n      {\n        ...blockHoistPlugin,\n        visitor: traverse.explode(blockHoistPlugin.visitor),\n      },\n      {},\n    );\n  }\n\n  return LOADED_PLUGIN;\n}\n\nfunction priority(bodyNode: Statement & { _blockHoist?: number | true }) {\n  const priority = bodyNode?._blockHoist;\n  if (priority == null) return 1;\n  if (priority === true) return 2;\n  return priority;\n}\n\nfunction stableSort(body: Statement[]) {\n  // By default, we use priorities of 0-4.\n  const buckets = Object.create(null);\n\n  // By collecting into buckets, we can guarantee a stable sort.\n  for (let i = 0; i < body.length; i++) {\n    const n = body[i];\n    const p = priority(n);\n\n    // In case some plugin is setting an unexpected priority.\n    const bucket = buckets[p] || (buckets[p] = []);\n    bucket.push(n);\n  }\n\n  // Sort our keys in descending order. Keys are unique, so we don't have to\n  // worry about stability.\n  const keys = Object.keys(buckets)\n    .map(k => +k)\n    .sort((a, b) => b - a);\n\n  let index = 0;\n  for (const key of keys) {\n    const bucket = buckets[key];\n    for (const n of bucket) {\n      body[index++] = n;\n    }\n  }\n  return body;\n}\n"],"mappings":";;;;;;AAAA,SAAAA,UAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,SAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,IAAAE,OAAA,GAAAD,OAAA;AAEA,IAAIE,aAA4B;AAEhC,MAAMC,gBAA8B,GAAG;EAarCC,IAAI,EAAE,qBAAqB;EAE3BC,OAAO,EAAE;IACPC,KAAK,EAAE;MACLC,IAAIA,CAAC;QAAEC;MAAK,CAAC,EAAE;QACbA,IAAI,CAACC,IAAI,GAAGC,eAAe,CAACF,IAAI,CAACC,IAAI,CAAC;MACxC;IACF,CAAC;IACDE,UAAU,EAAE;MACVJ,IAAIA,CAAC;QAAEC;MAAK,CAAC,EAAE;QAKbA,IAAI,CAACI,UAAU,GAAGF,eAAe,CAACF,IAAI,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