You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
7.5 KiB
1 line
7.5 KiB
1 month ago
|
{"version":3,"names":["_options","require","_string","_literal","NO_PLACEHOLDER","validate","placeholderPattern","createTemplateBuilder","formatter","defaultOpts","templateFnCache","WeakMap","templateAstCache","cachedOpts","Object","assign","tpl","args","length","Error","extendedTrace","stringTemplate","merge","Array","isArray","builder","get","literalTemplate","set","ast","fn","rootStack","error","stack","split","slice","join","arg","err"],"sources":["../src/builder.ts"],"sourcesContent":["import { merge, validate } from \"./options.ts\";\nimport type {\n TemplateOpts,\n PublicOpts,\n PublicReplacements,\n} from \"./options.ts\";\nimport type { Formatter } from \"./formatters.ts\";\n\nimport stringTemplate from \"./string.ts\";\nimport literalTemplate from \"./literal.ts\";\n\nexport type TemplateBuilder<T> = {\n // Build a new builder, merging the given options with the previous ones.\n (opts: PublicOpts): TemplateBuilder<T>;\n\n // Building from a string produces an AST builder function by default.\n (tpl: string, opts?: PublicOpts): (replacements?: PublicReplacements) => T;\n\n // Building from a template literal produces an AST builder function by default.\n (\n tpl: TemplateStringsArray,\n ...args: Array<unknown>\n ): (replacements?: PublicReplacements) => T;\n\n // Allow users to explicitly create templates that produce ASTs, skipping\n // the need for an intermediate function.\n ast: {\n (tpl: string, opts?: PublicOpts): T;\n (tpl: TemplateStringsArray, ...args: Array<unknown>): T;\n };\n};\n\n// Prebuild the options that will be used when parsing a `.ast` template.\n// These do not use a pattern because there is no way for users to pass in\n// replacement patterns to begin with, and disabling pattern matching means\n// users have more flexibility in what type of content they have in their\n// template JS.\nconst NO_PLACEHOLDER: TemplateOpts = validate({\n placeholderPattern: false,\n});\n\nexport default function createTemplateBuilder<T>(\n formatter: Formatter<T>,\n defaultOpts?: TemplateOpts,\n): TemplateBuilder<T> {\n const templateFnCache = new WeakMap();\n const templateAstCache = new WeakMap();\n const cachedOpts = defaultOpts || validate(null);\n\n return Object.assign(\n ((tpl, ...args) => {\n if (typeof tpl === \"string\") {\n if (args.length > 1) throw new Error(\"Unexpected extra params.\");\n return extendedTrace(\n stringTemplate(formatter, tpl, merge(cachedOpts, validate(args[0]))),\n );\n } else if (Array.isArray(tpl)) {\n let builder = templateFnCache.get(tpl);\n if (!builder) {\n builder = literalTemplate(formatter, tpl, cachedOpts);\n templateFnCache.set(tpl, builder);\n }\n return extendedTrace(builder(args));\n } else if (typeof tpl === \"object\" && tpl) {\n if (args.length > 0) throw new Error(\"Unexpected extra params.\");\n return createTemplateBuilder(\n formatter,\n merge(cachedOpts, validate(tpl)),\n );\n }\n throw new Error(`Unexpected template param ${typeof tpl}`);\n }) as TemplateBuilder<T>,\n {\n ast: (tpl: string | Array<string>, ...args: Array<unknown>) => {\n if (typeof tpl === \"string\") {\n if (args.length > 1) throw new Error(\"Unexpected extra params.\");\n return stringTemplate(\n formatter,\n tpl,\n merge(merge(cachedOpts, validate(args[0])), NO_PLACEHOLDER),\n )();\n } else if (Array.isArray(tpl)) {\n let builder = templateAstCache.get(tpl);\n if (!builder) {\n builder = literalTemplate(\n formatter,\n tpl,\n merge(cachedOpts, NO_PLACEHOLDER),\n );\n templateAstCache.set(tpl, builder);\n }\n return builder(args)();\n }\n\n throw new Error(`Unexpected template param ${typeof tpl}`);\n },\n },\n );\n}\n\nfunction extendedTrace<Arg, Result>(\n fn: (_: Arg) => Result,\n): (_: Arg) => Res
|