[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = literalTemplate;
|
---|
| 7 | var _options = require("./options.js");
|
---|
| 8 | var _parse = require("./parse.js");
|
---|
| 9 | var _populate = require("./populate.js");
|
---|
| 10 | function literalTemplate(formatter, tpl, opts) {
|
---|
| 11 | const {
|
---|
| 12 | metadata,
|
---|
| 13 | names
|
---|
| 14 | } = buildLiteralData(formatter, tpl, opts);
|
---|
| 15 | return arg => {
|
---|
| 16 | const defaultReplacements = {};
|
---|
| 17 | arg.forEach((replacement, i) => {
|
---|
| 18 | defaultReplacements[names[i]] = replacement;
|
---|
| 19 | });
|
---|
| 20 | return arg => {
|
---|
| 21 | const replacements = (0, _options.normalizeReplacements)(arg);
|
---|
| 22 | if (replacements) {
|
---|
| 23 | Object.keys(replacements).forEach(key => {
|
---|
| 24 | if (hasOwnProperty.call(defaultReplacements, key)) {
|
---|
| 25 | throw new Error("Unexpected replacement overlap.");
|
---|
| 26 | }
|
---|
| 27 | });
|
---|
| 28 | }
|
---|
| 29 | return formatter.unwrap((0, _populate.default)(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements));
|
---|
| 30 | };
|
---|
| 31 | };
|
---|
| 32 | }
|
---|
| 33 | function buildLiteralData(formatter, tpl, opts) {
|
---|
| 34 | let prefix = "BABEL_TPL$";
|
---|
| 35 | const raw = tpl.join("");
|
---|
| 36 | do {
|
---|
| 37 | prefix = "$$" + prefix;
|
---|
| 38 | } while (raw.includes(prefix));
|
---|
| 39 | const {
|
---|
| 40 | names,
|
---|
| 41 | code
|
---|
| 42 | } = buildTemplateCode(tpl, prefix);
|
---|
| 43 | const metadata = (0, _parse.default)(formatter, formatter.code(code), {
|
---|
| 44 | parser: opts.parser,
|
---|
| 45 | placeholderWhitelist: new Set(names.concat(opts.placeholderWhitelist ? Array.from(opts.placeholderWhitelist) : [])),
|
---|
| 46 | placeholderPattern: opts.placeholderPattern,
|
---|
| 47 | preserveComments: opts.preserveComments,
|
---|
| 48 | syntacticPlaceholders: opts.syntacticPlaceholders
|
---|
| 49 | });
|
---|
| 50 | return {
|
---|
| 51 | metadata,
|
---|
| 52 | names
|
---|
| 53 | };
|
---|
| 54 | }
|
---|
| 55 | function buildTemplateCode(tpl, prefix) {
|
---|
| 56 | const names = [];
|
---|
| 57 | let code = tpl[0];
|
---|
| 58 | for (let i = 1; i < tpl.length; i++) {
|
---|
| 59 | const value = `${prefix}${i - 1}`;
|
---|
| 60 | names.push(value);
|
---|
| 61 | code += value + tpl[i];
|
---|
| 62 | }
|
---|
| 63 | return {
|
---|
| 64 | names,
|
---|
| 65 | code
|
---|
| 66 | };
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | //# sourceMappingURL=literal.js.map
|
---|