1 |
- {"version":3,"names":["_t","require","_helpersGenerated","cloneNode","identifier","deep","obj","path","value","parts","split","last","shift","length","arguments","e","message","permuteHelperAST","ast","metadata","bindingName","localBindings","getDependency","adjustAst","locals","dependencies","exportBindingAssignments","exportName","bindings","Set","add","name","paths","Object","entries","o","keys","map","k","newName","has","ref","forEach","p","helperData","create","loadHelper","helper","helpers","assign","ReferenceError","code","minVersion","build","nodes","body","globals","getDependencies","get","id","type","undefined","exports","ensure","list","replace","_default","default"],"sources":["../src/index.ts"],"sourcesContent":["import { cloneNode, identifier } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport helpers from \"./helpers-generated.ts\";\nimport type { HelperMetadata } from \"./helpers-generated.ts\";\n\ntype GetDependency = (name: string) => t.Expression;\n\nfunction deep(obj: any, path: string, value?: unknown) {\n try {\n const parts = path.split(\".\");\n let last = parts.shift();\n while (parts.length > 0) {\n obj = obj[last];\n last = parts.shift();\n }\n if (arguments.length > 2) {\n obj[last] = value;\n } else {\n return obj[last];\n }\n } catch (e) {\n e.message += ` (when accessing ${path})`;\n throw e;\n }\n}\n\ntype AdjustAst = (\n ast: t.Program,\n exportName: string,\n mapExportBindingAssignments: (\n map: (node: t.Expression) => t.Expression,\n ) => void,\n) => void;\n\n/**\n * Given a helper AST and information about how it will be used, update the AST to match the usage.\n */\nfunction permuteHelperAST(\n ast: t.Program,\n metadata: HelperMetadata,\n bindingName: string | undefined,\n localBindings: string[] | undefined,\n getDependency: GetDependency | undefined,\n adjustAst: AdjustAst | undefined,\n) {\n const { locals, dependencies, exportBindingAssignments, exportName } =\n metadata;\n\n const bindings = new Set(localBindings || []);\n if (bindingName) bindings.add(bindingName);\n for (const [name, paths] of Object.entries(locals)) {\n let newName = name;\n if (bindingName && name === exportName) {\n newName = bindingName;\n } else {\n while (bindings.has(newName)) newName = \"_\" + newName;\n }\n\n if (newName !== name) {\n for (const path of paths) {\n deep(ast, path, identifier(newName));\n }\n }\n }\n\n for (const [name, paths] of Object.entries(dependencies)) {\n const ref =\n (typeof getDependency === \"function\" && getDependency(name)) ||\n identifier(name);\n for (const path of paths) {\n deep(ast, path, cloneNode(ref));\n }\n }\n\n adjustAst?.(ast, exportName, map => {\n exportBindingAssignments.forEach(p => deep(ast, p, map(deep(ast, p))));\n });\n}\n\ninterface HelperData {\n build: (\n getDependency: GetDependency | undefined,\n bindingName: string | undefined,\n localBindings: string[] | undefined,\n adjustAst: AdjustAst | undefined,\n ) => {\n nodes: t.Program[\"body\"];\n globals: string[];\n };\n minVersion: string;\n getDependencies: () => string[];\n}\n\nconst helperData: Record<string, HelperData> = Object.create(null);\nfunction loadHelper(name: string) {\n if (!helperData[name]) {\n const helper = helpers[name];\n if (!helper) {\n throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {\n code: \"BABEL_HELPER_UNKNOWN\",\n helper: name,\n });\n }\n\n helperData[name] = {\n minVersion: helper.minVersion,\n build(getDependency, bindingName, localBindings, adjustAst) {\n const ast = helper.ast();\n permuteHelperAST(\n ast,\n helper.metadata,\n bindingName,\n localBindings,\n getDependency,\n adjustAst,\n );\n\n return {\n nodes: ast.body,\n globals: helper.metadata.globals,\n };\n },\n getDependencies() {\n return Object.keys(helper.metadata.dependencies);\n },\n };\n }\n\n return helperData[name];\n}\n\nexport function get(\n name: string,\n getDependency?: GetDependency,\n bindingName?: string,\n localBindings?: string[],\n adjustAst?: AdjustAst,\n) {\n if (!process.env.BABEL_8_BREAKING) {\n // In older versions, bindingName was a t.Identifier | t.MemberExpression\n if (typeof bindingName === \"object\") {\n const id = bindingName as t.Identifier | t.MemberExpression | null;\n if (id?.type === \"Identifier\") {\n bindingName = id.name;\n } else {\n bindingName = undefined;\n }\n }\n }\n return loadHelper(name).build(\n getDependency,\n bindingName,\n localBindings,\n adjustAst,\n );\n}\n\nexport function minVersion(name: string) {\n return loadHelper(name).minVersion;\n}\n\nexport function getDependencies(name: string): ReadonlyArray<string> {\n return loadHelper(name).getDependencies();\n}\n\nif (!process.env.BABEL_8_BREAKING && !USE_ESM) {\n // eslint-disable-next-line no-restricted-globals\n exports.ensure = (name: string) => {\n loadHelper(name);\n };\n}\n\nexport const list = Object.keys(helpers).map(name => name.replace(/^_/, \"\"));\n\nexport default get;\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,EAAA,GAAAC,OAAA;AAEA,IAAAC,iBAAA,GAAAD,OAAA;AAA6C;EAFpCE,SAAS;EAAEC;AAAU,IAAAJ,EAAA;AAO9B,SAASK,IAAIA,CAACC,GAAQ,EAAEC,IAAY,EAAEC,KAAe,EAAE;EACrD,IAAI;IACF,MAAMC,KAAK,GAAGF,IAAI,CAACG,KAAK,CAAC,GAAG,CAAC;IAC7B,IAAIC,IAAI,GAAGF,KAAK,CAACG,KAAK,CAAC,CAAC;IACxB,OAAOH,KAAK,CAACI,MAAM,GAAG,CAAC,EAAE;MACvBP,GAAG,GAAGA,GAAG,CAACK,IAAI,CAAC;MACfA,IAAI,GAAGF,KAAK,CAACG,KAAK,CAAC,CAAC;IACtB;IACA,IAAIE,SAAS,CAACD,MAAM,GAAG,CAAC,EAAE;MACxBP,GAAG,CAACK,IAAI,CAAC,GAAGH,KAAK;IACnB,CAAC,MAAM;MACL,OAAOF,GAAG,CAACK,IAAI,CAAC;IAClB;EACF,CAAC,CAAC,OAAOI,CAAC,EAAE;IACVA,CAAC,CAACC,OAAO,IAAI,oBAAoBT,IAAI,GAAG;IACxC,MAAMQ,CAAC;EACT;AACF;AAaA,SAASE,gBAAgBA,CACvBC,GAAc,EACdC,QAAwB,EACxBC,WAA+B,EAC/BC,aAAmC,EACnCC,aAAwC,EACxCC,SAAgC,EAChC;EACA,MAAM;IAAEC,MAAM;IAAEC,YAAY;IAAEC,wBAAwB;IAAEC;EAAW,CAAC,GAClER,QAAQ;EAEV,MAAMS,QAAQ,GAAG,IAAIC,GAAG,CAACR,aAAa,IAAI,EAAE,CAAC;EAC7C,IAAID,WAAW,EAAEQ,QAAQ,CAACE,GAAG,CAACV,WAAW,CAAC;EAC1C,KAA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