Nicolò Ribaudo c285d5409e
Add %%placeholders%% support to @babel/template (#9648)
This is the last step to make https://github.com/babel/babel/pull/9364 usable in Babel. I'm sorry for opening this PR so late, but I hope to get it in v7.4.

In this PR I added a new option to `@babel/template`, `syntacticPlaceholders: ?boolean`, which toggles between `%%foo%%` placeholders (when `true`) and `FOO` placeholders. If it isn't specified, Babel tries to be "smart" to avoid breaking backward compat: if `%%foo%%` is used `syntacticPlaceholders` defaults to `true`, otherwise to `false`.

0e58e252913efe84eba926cc9c9c19fb18d5c620 commit shows how some templates we used could be simplified by using this new placeholders syntax (we can't actually do it yet because we are importing `template` from `@babel/core` which could be an older version).

NOTE: Since I wanted to keep this PR as small as possible to make it easier to review, I didn't migrate `template.ast` to internally use the new syntax. It is an implementation detail, so it will be possible to change it in a patch release.
2019-03-18 21:23:10 +01:00

105 lines
2.7 KiB
JavaScript

// @flow
import type { Formatter } from "./formatters";
import { normalizeReplacements, type TemplateOpts } from "./options";
import parseAndBuildMetadata from "./parse";
import populatePlaceholders from "./populate";
export default function literalTemplate<T>(
formatter: Formatter<T>,
tpl: Array<string>,
opts: TemplateOpts,
): (Array<mixed>) => mixed => T {
const { metadata, names } = buildLiteralData(formatter, tpl, opts);
return (arg: Array<mixed>) => {
const defaultReplacements = arg.reduce((acc, replacement, i) => {
acc[names[i]] = replacement;
return acc;
}, {});
return (arg: mixed) => {
const replacements = normalizeReplacements(arg);
if (replacements) {
Object.keys(replacements).forEach(key => {
if (Object.prototype.hasOwnProperty.call(defaultReplacements, key)) {
throw new Error("Unexpected replacement overlap.");
}
});
}
return formatter.unwrap(
populatePlaceholders(
metadata,
replacements
? Object.assign(replacements, defaultReplacements)
: defaultReplacements,
),
);
};
};
}
function buildLiteralData<T>(
formatter: Formatter<T>,
tpl: Array<string>,
opts: TemplateOpts,
) {
let names;
let nameSet;
let metadata;
let prefix = "";
do {
// If there are cases where the template already contains $0 or any other
// matching pattern, we keep adding "$" characters until a unique prefix
// is found.
prefix += "$";
const result = buildTemplateCode(tpl, prefix);
names = result.names;
nameSet = new Set(names);
metadata = parseAndBuildMetadata(formatter, formatter.code(result.code), {
parser: opts.parser,
// Explicitly include our generated names in the whitelist so users never
// have to think about whether their placeholder pattern will match.
placeholderWhitelist: new Set(
result.names.concat(
opts.placeholderWhitelist
? Array.from(opts.placeholderWhitelist)
: [],
),
),
placeholderPattern: opts.placeholderPattern,
preserveComments: opts.preserveComments,
syntacticPlaceholders: opts.syntacticPlaceholders,
});
} while (
metadata.placeholders.some(
placeholder => placeholder.isDuplicate && nameSet.has(placeholder.name),
)
);
return { metadata, names };
}
function buildTemplateCode(
tpl: Array<string>,
prefix: string,
): { names: Array<string>, code: string } {
const names = [];
let code = tpl[0];
for (let i = 1; i < tpl.length; i++) {
const value = `${prefix}${i - 1}`;
names.push(value);
code += value + tpl[i];
}
return { names, code };
}