1 line
5.7 KiB
Plaintext
1 line
5.7 KiB
Plaintext
|
{"version":3,"names":["literalTemplate","formatter","tpl","opts","metadata","names","buildLiteralData","arg","defaultReplacements","forEach","replacement","i","replacements","normalizeReplacements","Object","keys","key","prototype","hasOwnProperty","call","Error","unwrap","populatePlaceholders","assign","nameSet","prefix","result","buildTemplateCode","Set","parseAndBuildMetadata","code","parser","placeholderWhitelist","concat","Array","from","placeholderPattern","preserveComments","syntacticPlaceholders","placeholders","some","placeholder","isDuplicate","has","name","length","value","push"],"sources":["../src/literal.ts"],"sourcesContent":["import type { Formatter } from \"./formatters\";\nimport type { TemplateReplacements, TemplateOpts } from \"./options\";\nimport { normalizeReplacements } from \"./options\";\nimport parseAndBuildMetadata from \"./parse\";\nimport populatePlaceholders from \"./populate\";\n\nexport default function literalTemplate<T>(\n formatter: Formatter<T>,\n tpl: Array<string>,\n opts: TemplateOpts,\n): (_: Array<unknown>) => (_: unknown) => T {\n const { metadata, names } = buildLiteralData(formatter, tpl, opts);\n\n return arg => {\n const defaultReplacements: TemplateReplacements = {};\n arg.forEach((replacement, i) => {\n defaultReplacements[names[i]] = replacement;\n });\n\n return (arg: unknown) => {\n const replacements = normalizeReplacements(arg);\n\n if (replacements) {\n Object.keys(replacements).forEach(key => {\n if (Object.prototype.hasOwnProperty.call(defaultReplacements, key)) {\n throw new Error(\"Unexpected replacement overlap.\");\n }\n });\n }\n\n return formatter.unwrap(\n populatePlaceholders(\n metadata,\n replacements\n ? Object.assign(replacements, defaultReplacements)\n : defaultReplacements,\n ),\n );\n };\n };\n}\n\nfunction buildLiteralData<T>(\n formatter: Formatter<T>,\n tpl: Array<string>,\n opts: TemplateOpts,\n) {\n let names;\n let nameSet: Set<string>;\n let metadata;\n let prefix = \"\";\n\n do {\n // If there are cases where the template already contains $0 or any other\n // matching pattern, we keep adding \"$\" characters until a unique prefix\n // is found.\n prefix += \"$\";\n const result = buildTemplateCode(tpl, prefix);\n\n names = result.names;\n nameSet = new Set(names);\n metadata = parseAndBuildMetadata(formatter, formatter.code(result.code), {\n parser: opts.parser,\n\n // Explicitly include our generated names in the whitelist so users never\n // have to think about whether their placeholder pattern will match.\n placeholderWhitelist: new Set(\n result.names.concat(\n opts.placeholderWhitelist\n ? Array.from(opts.placeholderWhitelist)\n : [],\n ),\n ),\n placeholderPattern: opts.placeholderPattern,\n preserveComments: opts.preserveComments,\n syntacticPlaceholders: opts.syntacticPlaceholders,\n });\n } while (\n metadata.placeholders.some(\n placeholder => placeholder.isDuplicate && nameSet.has(placeholder.name),\n )\n );\n\n return { metadata, names };\n}\n\nfunction buildTemplateCode(\n tpl: Array<string>,\n prefix: string,\n): { names: Array<string>; code: string } {\n const names = [];\n\n let code = tpl[0];\n\n for (let i = 1; i < tpl.length; i++) {\n const value = `${prefix}${i - 1}`;\n names.push(value);\n\n code += value + tpl[i];\n }\n\n return { names, code };\n}\n"],"mappings":";;;;;;AAEA;AACA;AACA;AAEe,SAASA,eAAe,CACrCC,SAAuB,EACvBC,GAAkB,EAClBC,IAAkB,EACwB;EAC1C,MAAM;IAAEC,QAAQ;IAAEC;EAAM,CAAC,GAAGC,gBAAgB,CAACL,SAAS,EAAEC,GAAG,EAAEC,IAAI,CAAC;EAElE,OAAOI,GAAG,IAAI;IACZ,MAAMC,mBAAyC,GAAG,CAAC,CAAC;IACpDD,GAAG,CAACE,OAAO,CAAC,CAACC,WAAW,EAAEC,CAAC,KAAK;MAC9BH,mBAAmB,CAACH,KAAK,CAACM,CAAC,CAAC,CAAC,GAAGD,WAAW;IAC7C,CAAC,CAAC;IAEF,OAAQH,GAAY,IAAK;MACvB,MAAMK,YAAY,GAAG,IAAAC,8BAAqB,EAACN,GAAG,CAAC;MAE/C,IAAIK,YAAY,EAAE;QAChBE,MAAM,CAACC,IAAI,CA
|