@babel/template
Advanced tools
Comparing version 7.22.5 to 8.0.0-alpha.0
@@ -1,25 +0,19 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = createTemplateBuilder; | ||
var _options = require("./options"); | ||
var _string = require("./string"); | ||
var _literal = require("./literal"); | ||
const NO_PLACEHOLDER = (0, _options.validate)({ | ||
import { merge, validate } from "./options.js"; | ||
import stringTemplate from "./string.js"; | ||
import literalTemplate from "./literal.js"; | ||
const NO_PLACEHOLDER = validate({ | ||
placeholderPattern: false | ||
}); | ||
function createTemplateBuilder(formatter, defaultOpts) { | ||
export default function createTemplateBuilder(formatter, defaultOpts) { | ||
const templateFnCache = new WeakMap(); | ||
const templateAstCache = new WeakMap(); | ||
const cachedOpts = defaultOpts || (0, _options.validate)(null); | ||
const cachedOpts = defaultOpts || validate(null); | ||
return Object.assign((tpl, ...args) => { | ||
if (typeof tpl === "string") { | ||
if (args.length > 1) throw new Error("Unexpected extra params."); | ||
return extendedTrace((0, _string.default)(formatter, tpl, (0, _options.merge)(cachedOpts, (0, _options.validate)(args[0])))); | ||
return extendedTrace(stringTemplate(formatter, tpl, merge(cachedOpts, validate(args[0])))); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateFnCache.get(tpl); | ||
if (!builder) { | ||
builder = (0, _literal.default)(formatter, tpl, cachedOpts); | ||
builder = literalTemplate(formatter, tpl, cachedOpts); | ||
templateFnCache.set(tpl, builder); | ||
@@ -30,3 +24,3 @@ } | ||
if (args.length > 0) throw new Error("Unexpected extra params."); | ||
return createTemplateBuilder(formatter, (0, _options.merge)(cachedOpts, (0, _options.validate)(tpl))); | ||
return createTemplateBuilder(formatter, merge(cachedOpts, validate(tpl))); | ||
} | ||
@@ -38,7 +32,7 @@ throw new Error(`Unexpected template param ${typeof tpl}`); | ||
if (args.length > 1) throw new Error("Unexpected extra params."); | ||
return (0, _string.default)(formatter, tpl, (0, _options.merge)((0, _options.merge)(cachedOpts, (0, _options.validate)(args[0])), NO_PLACEHOLDER))(); | ||
return stringTemplate(formatter, tpl, merge(merge(cachedOpts, validate(args[0])), NO_PLACEHOLDER))(); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateAstCache.get(tpl); | ||
if (!builder) { | ||
builder = (0, _literal.default)(formatter, tpl, (0, _options.merge)(cachedOpts, NO_PLACEHOLDER)); | ||
builder = literalTemplate(formatter, tpl, merge(cachedOpts, NO_PLACEHOLDER)); | ||
templateAstCache.set(tpl, builder); | ||
@@ -45,0 +39,0 @@ } |
@@ -1,8 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.statements = exports.statement = exports.smart = exports.program = exports.expression = void 0; | ||
var _t = require("@babel/types"); | ||
import * as _t from "@babel/types"; | ||
const { | ||
@@ -20,3 +14,3 @@ assertExpressionStatement | ||
} | ||
const smart = makeStatementFormatter(body => { | ||
export const smart = makeStatementFormatter(body => { | ||
if (body.length > 1) { | ||
@@ -28,6 +22,4 @@ return body; | ||
}); | ||
exports.smart = smart; | ||
const statements = makeStatementFormatter(body => body); | ||
exports.statements = statements; | ||
const statement = makeStatementFormatter(body => { | ||
export const statements = makeStatementFormatter(body => body); | ||
export const statement = makeStatementFormatter(body => { | ||
if (body.length === 0) { | ||
@@ -41,4 +33,3 @@ throw new Error("Found nothing to return."); | ||
}); | ||
exports.statement = statement; | ||
const expression = { | ||
export const expression = { | ||
code: str => `(\n${str}\n)`, | ||
@@ -61,4 +52,3 @@ validate: ast => { | ||
}; | ||
exports.expression = expression; | ||
const program = { | ||
export const program = { | ||
code: str => str, | ||
@@ -68,4 +58,3 @@ validate: () => {}, | ||
}; | ||
exports.program = program; | ||
//# sourceMappingURL=formatters.js.map |
547
lib/index.js
@@ -1,20 +0,531 @@ | ||
"use strict"; | ||
import * as _t from '@babel/types'; | ||
import { parse } from '@babel/parser'; | ||
import { codeFrameColumns } from '@babel/code-frame'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
const { | ||
assertExpressionStatement | ||
} = _t; | ||
function makeStatementFormatter(fn) { | ||
return { | ||
code: str => `/* @babel/template */;\n${str}`, | ||
validate: () => {}, | ||
unwrap: ast => { | ||
return fn(ast.program.body.slice(1)); | ||
} | ||
}; | ||
} | ||
const smart$1 = makeStatementFormatter(body => { | ||
if (body.length > 1) { | ||
return body; | ||
} else { | ||
return body[0]; | ||
} | ||
}); | ||
exports.statements = exports.statement = exports.smart = exports.program = exports.expression = exports.default = void 0; | ||
var formatters = require("./formatters"); | ||
var _builder = require("./builder"); | ||
const smart = (0, _builder.default)(formatters.smart); | ||
exports.smart = smart; | ||
const statement = (0, _builder.default)(formatters.statement); | ||
exports.statement = statement; | ||
const statements = (0, _builder.default)(formatters.statements); | ||
exports.statements = statements; | ||
const expression = (0, _builder.default)(formatters.expression); | ||
exports.expression = expression; | ||
const program = (0, _builder.default)(formatters.program); | ||
exports.program = program; | ||
var _default = Object.assign(smart.bind(undefined), { | ||
const statements$1 = makeStatementFormatter(body => body); | ||
const statement$1 = makeStatementFormatter(body => { | ||
if (body.length === 0) { | ||
throw new Error("Found nothing to return."); | ||
} | ||
if (body.length > 1) { | ||
throw new Error("Found multiple statements but wanted one"); | ||
} | ||
return body[0]; | ||
}); | ||
const expression$1 = { | ||
code: str => `(\n${str}\n)`, | ||
validate: ast => { | ||
if (ast.program.body.length > 1) { | ||
throw new Error("Found multiple statements but wanted one"); | ||
} | ||
if (expression$1.unwrap(ast).start === 0) { | ||
throw new Error("Parse result included parens."); | ||
} | ||
}, | ||
unwrap: ({ | ||
program | ||
}) => { | ||
const [stmt] = program.body; | ||
assertExpressionStatement(stmt); | ||
return stmt.expression; | ||
} | ||
}; | ||
const program$1 = { | ||
code: str => str, | ||
validate: () => {}, | ||
unwrap: ast => ast.program | ||
}; | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
const _excluded = ["placeholderWhitelist", "placeholderPattern", "preserveComments", "syntacticPlaceholders"]; | ||
function merge(a, b) { | ||
const { | ||
placeholderWhitelist = a.placeholderWhitelist, | ||
placeholderPattern = a.placeholderPattern, | ||
preserveComments = a.preserveComments, | ||
syntacticPlaceholders = a.syntacticPlaceholders | ||
} = b; | ||
return { | ||
parser: Object.assign({}, a.parser, b.parser), | ||
placeholderWhitelist, | ||
placeholderPattern, | ||
preserveComments, | ||
syntacticPlaceholders | ||
}; | ||
} | ||
function validate$1(opts) { | ||
if (opts != null && typeof opts !== "object") { | ||
throw new Error("Unknown template options."); | ||
} | ||
const _ref = opts || {}, | ||
{ | ||
placeholderWhitelist, | ||
placeholderPattern, | ||
preserveComments, | ||
syntacticPlaceholders | ||
} = _ref, | ||
parser = _objectWithoutPropertiesLoose(_ref, _excluded); | ||
if (placeholderWhitelist != null && !(placeholderWhitelist instanceof Set)) { | ||
throw new Error("'.placeholderWhitelist' must be a Set, null, or undefined"); | ||
} | ||
if (placeholderPattern != null && !(placeholderPattern instanceof RegExp) && placeholderPattern !== false) { | ||
throw new Error("'.placeholderPattern' must be a RegExp, false, null, or undefined"); | ||
} | ||
if (preserveComments != null && typeof preserveComments !== "boolean") { | ||
throw new Error("'.preserveComments' must be a boolean, null, or undefined"); | ||
} | ||
if (syntacticPlaceholders != null && typeof syntacticPlaceholders !== "boolean") { | ||
throw new Error("'.syntacticPlaceholders' must be a boolean, null, or undefined"); | ||
} | ||
if (syntacticPlaceholders === true && (placeholderWhitelist != null || placeholderPattern != null)) { | ||
throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'"); | ||
} | ||
return { | ||
parser, | ||
placeholderWhitelist: placeholderWhitelist || undefined, | ||
placeholderPattern: placeholderPattern == null ? undefined : placeholderPattern, | ||
preserveComments: preserveComments == null ? undefined : preserveComments, | ||
syntacticPlaceholders: syntacticPlaceholders == null ? undefined : syntacticPlaceholders | ||
}; | ||
} | ||
function normalizeReplacements(replacements) { | ||
if (Array.isArray(replacements)) { | ||
return replacements.reduce((acc, replacement, i) => { | ||
acc["$" + i] = replacement; | ||
return acc; | ||
}, {}); | ||
} else if (typeof replacements === "object" || replacements == null) { | ||
return replacements || undefined; | ||
} | ||
throw new Error("Template replacements must be an array, object, null, or undefined"); | ||
} | ||
const { | ||
isCallExpression, | ||
isExpressionStatement, | ||
isFunction, | ||
isIdentifier, | ||
isJSXIdentifier, | ||
isNewExpression, | ||
isPlaceholder, | ||
isStatement: isStatement$1, | ||
isStringLiteral: isStringLiteral$1, | ||
removePropertiesDeep, | ||
traverse | ||
} = _t; | ||
const PATTERN = /^[_$A-Z0-9]+$/; | ||
function parseAndBuildMetadata(formatter, code, opts) { | ||
const { | ||
placeholderWhitelist, | ||
placeholderPattern, | ||
preserveComments, | ||
syntacticPlaceholders | ||
} = opts; | ||
const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders); | ||
removePropertiesDeep(ast, { | ||
preserveComments | ||
}); | ||
formatter.validate(ast); | ||
const state = { | ||
syntactic: { | ||
placeholders: [], | ||
placeholderNames: new Set() | ||
}, | ||
legacy: { | ||
placeholders: [], | ||
placeholderNames: new Set() | ||
}, | ||
placeholderWhitelist, | ||
placeholderPattern, | ||
syntacticPlaceholders | ||
}; | ||
traverse(ast, placeholderVisitorHandler, state); | ||
return Object.assign({ | ||
ast | ||
}, state.syntactic.placeholders.length ? state.syntactic : state.legacy); | ||
} | ||
function placeholderVisitorHandler(node, ancestors, state) { | ||
let name; | ||
let hasSyntacticPlaceholders = state.syntactic.placeholders.length > 0; | ||
if (isPlaceholder(node)) { | ||
if (state.syntacticPlaceholders === false) { | ||
throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false."); | ||
} | ||
name = node.name.name; | ||
hasSyntacticPlaceholders = true; | ||
} else if (hasSyntacticPlaceholders || state.syntacticPlaceholders) { | ||
return; | ||
} else if (isIdentifier(node) || isJSXIdentifier(node)) { | ||
name = node.name; | ||
} else if (isStringLiteral$1(node)) { | ||
name = node.value; | ||
} else { | ||
return; | ||
} | ||
if (hasSyntacticPlaceholders && (state.placeholderPattern != null || state.placeholderWhitelist != null)) { | ||
throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'"); | ||
} | ||
if (!hasSyntacticPlaceholders && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !state.placeholderWhitelist?.has(name)) { | ||
return; | ||
} | ||
ancestors = ancestors.slice(); | ||
const { | ||
node: parent, | ||
key | ||
} = ancestors[ancestors.length - 1]; | ||
let type; | ||
if (isStringLiteral$1(node) || isPlaceholder(node, { | ||
expectedNode: "StringLiteral" | ||
})) { | ||
type = "string"; | ||
} else if (isNewExpression(parent) && key === "arguments" || isCallExpression(parent) && key === "arguments" || isFunction(parent) && key === "params") { | ||
type = "param"; | ||
} else if (isExpressionStatement(parent) && !isPlaceholder(node)) { | ||
type = "statement"; | ||
ancestors = ancestors.slice(0, -1); | ||
} else if (isStatement$1(node) && isPlaceholder(node)) { | ||
type = "statement"; | ||
} else { | ||
type = "other"; | ||
} | ||
const { | ||
placeholders, | ||
placeholderNames | ||
} = !hasSyntacticPlaceholders ? state.legacy : state.syntactic; | ||
placeholders.push({ | ||
name, | ||
type, | ||
resolve: ast => resolveAncestors(ast, ancestors), | ||
isDuplicate: placeholderNames.has(name) | ||
}); | ||
placeholderNames.add(name); | ||
} | ||
function resolveAncestors(ast, ancestors) { | ||
let parent = ast; | ||
for (let i = 0; i < ancestors.length - 1; i++) { | ||
const { | ||
key, | ||
index | ||
} = ancestors[i]; | ||
if (index === undefined) { | ||
parent = parent[key]; | ||
} else { | ||
parent = parent[key][index]; | ||
} | ||
} | ||
const { | ||
key, | ||
index | ||
} = ancestors[ancestors.length - 1]; | ||
return { | ||
parent, | ||
key, | ||
index | ||
}; | ||
} | ||
function parseWithCodeFrame(code, parserOpts, syntacticPlaceholders) { | ||
const plugins = (parserOpts.plugins || []).slice(); | ||
if (syntacticPlaceholders !== false) { | ||
plugins.push("placeholders"); | ||
} | ||
parserOpts = Object.assign({ | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
sourceType: "module" | ||
}, parserOpts, { | ||
plugins | ||
}); | ||
try { | ||
return parse(code, parserOpts); | ||
} catch (err) { | ||
const loc = err.loc; | ||
if (loc) { | ||
err.message += "\n" + codeFrameColumns(code, { | ||
start: loc | ||
}); | ||
err.code = "BABEL_TEMPLATE_PARSE_ERROR"; | ||
} | ||
throw err; | ||
} | ||
} | ||
const { | ||
blockStatement, | ||
cloneNode, | ||
emptyStatement, | ||
expressionStatement, | ||
identifier, | ||
isStatement, | ||
isStringLiteral, | ||
stringLiteral, | ||
validate | ||
} = _t; | ||
function populatePlaceholders(metadata, replacements) { | ||
const ast = cloneNode(metadata.ast); | ||
if (replacements) { | ||
metadata.placeholders.forEach(placeholder => { | ||
if (!Object.prototype.hasOwnProperty.call(replacements, placeholder.name)) { | ||
const placeholderName = placeholder.name; | ||
throw new Error(`Error: No substitution given for "${placeholderName}". If this is not meant to be a | ||
placeholder you may want to consider passing one of the following options to @babel/template: | ||
- { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])} | ||
- { placeholderPattern: /^${placeholderName}$/ }`); | ||
} | ||
}); | ||
Object.keys(replacements).forEach(key => { | ||
if (!metadata.placeholderNames.has(key)) { | ||
throw new Error(`Unknown substitution "${key}" given`); | ||
} | ||
}); | ||
} | ||
metadata.placeholders.slice().reverse().forEach(placeholder => { | ||
try { | ||
applyReplacement(placeholder, ast, replacements && replacements[placeholder.name] || null); | ||
} catch (e) { | ||
e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`; | ||
throw e; | ||
} | ||
}); | ||
return ast; | ||
} | ||
function applyReplacement(placeholder, ast, replacement) { | ||
if (placeholder.isDuplicate) { | ||
if (Array.isArray(replacement)) { | ||
replacement = replacement.map(node => cloneNode(node)); | ||
} else if (typeof replacement === "object") { | ||
replacement = cloneNode(replacement); | ||
} | ||
} | ||
const { | ||
parent, | ||
key, | ||
index | ||
} = placeholder.resolve(ast); | ||
if (placeholder.type === "string") { | ||
if (typeof replacement === "string") { | ||
replacement = stringLiteral(replacement); | ||
} | ||
if (!replacement || !isStringLiteral(replacement)) { | ||
throw new Error("Expected string substitution"); | ||
} | ||
} else if (placeholder.type === "statement") { | ||
if (index === undefined) { | ||
if (!replacement) { | ||
replacement = emptyStatement(); | ||
} else if (Array.isArray(replacement)) { | ||
replacement = blockStatement(replacement); | ||
} else if (typeof replacement === "string") { | ||
replacement = expressionStatement(identifier(replacement)); | ||
} else if (!isStatement(replacement)) { | ||
replacement = expressionStatement(replacement); | ||
} | ||
} else { | ||
if (replacement && !Array.isArray(replacement)) { | ||
if (typeof replacement === "string") { | ||
replacement = identifier(replacement); | ||
} | ||
if (!isStatement(replacement)) { | ||
replacement = expressionStatement(replacement); | ||
} | ||
} | ||
} | ||
} else if (placeholder.type === "param") { | ||
if (typeof replacement === "string") { | ||
replacement = identifier(replacement); | ||
} | ||
if (index === undefined) throw new Error("Assertion failure."); | ||
} else { | ||
if (typeof replacement === "string") { | ||
replacement = identifier(replacement); | ||
} | ||
if (Array.isArray(replacement)) { | ||
throw new Error("Cannot replace single expression with an array."); | ||
} | ||
} | ||
if (index === undefined) { | ||
validate(parent, key, replacement); | ||
parent[key] = replacement; | ||
} else { | ||
const items = parent[key].slice(); | ||
if (placeholder.type === "statement" || placeholder.type === "param") { | ||
if (replacement == null) { | ||
items.splice(index, 1); | ||
} else if (Array.isArray(replacement)) { | ||
items.splice(index, 1, ...replacement); | ||
} else { | ||
items[index] = replacement; | ||
} | ||
} else { | ||
items[index] = replacement; | ||
} | ||
validate(parent, key, items); | ||
parent[key] = items; | ||
} | ||
} | ||
function stringTemplate(formatter, code, opts) { | ||
code = formatter.code(code); | ||
let metadata; | ||
return arg => { | ||
const replacements = normalizeReplacements(arg); | ||
if (!metadata) metadata = parseAndBuildMetadata(formatter, code, opts); | ||
return formatter.unwrap(populatePlaceholders(metadata, replacements)); | ||
}; | ||
} | ||
function literalTemplate(formatter, tpl, opts) { | ||
const { | ||
metadata, | ||
names | ||
} = buildLiteralData(formatter, tpl, opts); | ||
return arg => { | ||
const defaultReplacements = {}; | ||
arg.forEach((replacement, i) => { | ||
defaultReplacements[names[i]] = replacement; | ||
}); | ||
return arg => { | ||
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(formatter, tpl, opts) { | ||
let prefix = "BABEL_TPL$"; | ||
const raw = tpl.join(""); | ||
do { | ||
prefix = "$$" + prefix; | ||
} while (raw.includes(prefix)); | ||
const { | ||
names, | ||
code | ||
} = buildTemplateCode(tpl, prefix); | ||
const metadata = parseAndBuildMetadata(formatter, formatter.code(code), { | ||
parser: opts.parser, | ||
placeholderWhitelist: new Set(names.concat(opts.placeholderWhitelist ? Array.from(opts.placeholderWhitelist) : [])), | ||
placeholderPattern: opts.placeholderPattern, | ||
preserveComments: opts.preserveComments, | ||
syntacticPlaceholders: opts.syntacticPlaceholders | ||
}); | ||
return { | ||
metadata, | ||
names | ||
}; | ||
} | ||
function buildTemplateCode(tpl, prefix) { | ||
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 | ||
}; | ||
} | ||
const NO_PLACEHOLDER = validate$1({ | ||
placeholderPattern: false | ||
}); | ||
function createTemplateBuilder(formatter, defaultOpts) { | ||
const templateFnCache = new WeakMap(); | ||
const templateAstCache = new WeakMap(); | ||
const cachedOpts = defaultOpts || validate$1(null); | ||
return Object.assign((tpl, ...args) => { | ||
if (typeof tpl === "string") { | ||
if (args.length > 1) throw new Error("Unexpected extra params."); | ||
return extendedTrace(stringTemplate(formatter, tpl, merge(cachedOpts, validate$1(args[0])))); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateFnCache.get(tpl); | ||
if (!builder) { | ||
builder = literalTemplate(formatter, tpl, cachedOpts); | ||
templateFnCache.set(tpl, builder); | ||
} | ||
return extendedTrace(builder(args)); | ||
} else if (typeof tpl === "object" && tpl) { | ||
if (args.length > 0) throw new Error("Unexpected extra params."); | ||
return createTemplateBuilder(formatter, merge(cachedOpts, validate$1(tpl))); | ||
} | ||
throw new Error(`Unexpected template param ${typeof tpl}`); | ||
}, { | ||
ast: (tpl, ...args) => { | ||
if (typeof tpl === "string") { | ||
if (args.length > 1) throw new Error("Unexpected extra params."); | ||
return stringTemplate(formatter, tpl, merge(merge(cachedOpts, validate$1(args[0])), NO_PLACEHOLDER))(); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateAstCache.get(tpl); | ||
if (!builder) { | ||
builder = literalTemplate(formatter, tpl, merge(cachedOpts, NO_PLACEHOLDER)); | ||
templateAstCache.set(tpl, builder); | ||
} | ||
return builder(args)(); | ||
} | ||
throw new Error(`Unexpected template param ${typeof tpl}`); | ||
} | ||
}); | ||
} | ||
function extendedTrace(fn) { | ||
let rootStack = ""; | ||
try { | ||
throw new Error(); | ||
} catch (error) { | ||
if (error.stack) { | ||
rootStack = error.stack.split("\n").slice(3).join("\n"); | ||
} | ||
} | ||
return arg => { | ||
try { | ||
return fn(arg); | ||
} catch (err) { | ||
err.stack += `\n =============\n${rootStack}`; | ||
throw err; | ||
} | ||
}; | ||
} | ||
const smart = createTemplateBuilder(smart$1); | ||
const statement = createTemplateBuilder(statement$1); | ||
const statements = createTemplateBuilder(statements$1); | ||
const expression = createTemplateBuilder(expression$1); | ||
const program = createTemplateBuilder(program$1); | ||
var index = Object.assign(smart.bind(undefined), { | ||
smart, | ||
@@ -27,4 +538,4 @@ statement, | ||
}); | ||
exports.default = _default; | ||
export { index as default, expression, program, smart, statement, statements }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,11 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = literalTemplate; | ||
var _options = require("./options"); | ||
var _parse = require("./parse"); | ||
var _populate = require("./populate"); | ||
function literalTemplate(formatter, tpl, opts) { | ||
import { normalizeReplacements } from "./options.js"; | ||
import parseAndBuildMetadata from "./parse.js"; | ||
import populatePlaceholders from "./populate.js"; | ||
export default function literalTemplate(formatter, tpl, opts) { | ||
const { | ||
@@ -21,3 +15,3 @@ metadata, | ||
return arg => { | ||
const replacements = (0, _options.normalizeReplacements)(arg); | ||
const replacements = normalizeReplacements(arg); | ||
if (replacements) { | ||
@@ -30,3 +24,3 @@ Object.keys(replacements).forEach(key => { | ||
} | ||
return formatter.unwrap((0, _populate.default)(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements)); | ||
return formatter.unwrap(populatePlaceholders(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements)); | ||
}; | ||
@@ -45,3 +39,3 @@ }; | ||
} = buildTemplateCode(tpl, prefix); | ||
const metadata = (0, _parse.default)(formatter, formatter.code(code), { | ||
const metadata = parseAndBuildMetadata(formatter, formatter.code(code), { | ||
parser: opts.parser, | ||
@@ -48,0 +42,0 @@ placeholderWhitelist: new Set(names.concat(opts.placeholderWhitelist ? Array.from(opts.placeholderWhitelist) : [])), |
@@ -1,12 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.merge = merge; | ||
exports.normalizeReplacements = normalizeReplacements; | ||
exports.validate = validate; | ||
const _excluded = ["placeholderWhitelist", "placeholderPattern", "preserveComments", "syntacticPlaceholders"]; | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function merge(a, b) { | ||
export function merge(a, b) { | ||
const { | ||
@@ -26,3 +18,3 @@ placeholderWhitelist = a.placeholderWhitelist, | ||
} | ||
function validate(opts) { | ||
export function validate(opts) { | ||
if (opts != null && typeof opts !== "object") { | ||
@@ -62,3 +54,3 @@ throw new Error("Unknown template options."); | ||
} | ||
function normalizeReplacements(replacements) { | ||
export function normalizeReplacements(replacements) { | ||
if (Array.isArray(replacements)) { | ||
@@ -65,0 +57,0 @@ return replacements.reduce((acc, replacement, i) => { |
@@ -1,10 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = parseAndBuildMetadata; | ||
var _t = require("@babel/types"); | ||
var _parser = require("@babel/parser"); | ||
var _codeFrame = require("@babel/code-frame"); | ||
import * as _t from "@babel/types"; | ||
const { | ||
@@ -23,4 +15,6 @@ isCallExpression, | ||
} = _t; | ||
import { parse } from "@babel/parser"; | ||
import { codeFrameColumns } from "@babel/code-frame"; | ||
const PATTERN = /^[_$A-Z0-9]+$/; | ||
function parseAndBuildMetadata(formatter, code, opts) { | ||
export default function parseAndBuildMetadata(formatter, code, opts) { | ||
const { | ||
@@ -56,3 +50,2 @@ placeholderWhitelist, | ||
function placeholderVisitorHandler(node, ancestors, state) { | ||
var _state$placeholderWhi; | ||
let name; | ||
@@ -78,3 +71,3 @@ let hasSyntacticPlaceholders = state.syntactic.placeholders.length > 0; | ||
} | ||
if (!hasSyntacticPlaceholders && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !((_state$placeholderWhi = state.placeholderWhitelist) != null && _state$placeholderWhi.has(name))) { | ||
if (!hasSyntacticPlaceholders && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !state.placeholderWhitelist?.has(name)) { | ||
return; | ||
@@ -150,7 +143,7 @@ } | ||
try { | ||
return (0, _parser.parse)(code, parserOpts); | ||
return parse(code, parserOpts); | ||
} catch (err) { | ||
const loc = err.loc; | ||
if (loc) { | ||
err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, { | ||
err.message += "\n" + codeFrameColumns(code, { | ||
start: loc | ||
@@ -157,0 +150,0 @@ }); |
@@ -1,8 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = populatePlaceholders; | ||
var _t = require("@babel/types"); | ||
import * as _t from "@babel/types"; | ||
const { | ||
@@ -19,3 +13,3 @@ blockStatement, | ||
} = _t; | ||
function populatePlaceholders(metadata, replacements) { | ||
export default function populatePlaceholders(metadata, replacements) { | ||
const ast = cloneNode(metadata.ast); | ||
@@ -22,0 +16,0 @@ if (replacements) { |
@@ -1,17 +0,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = stringTemplate; | ||
var _options = require("./options"); | ||
var _parse = require("./parse"); | ||
var _populate = require("./populate"); | ||
function stringTemplate(formatter, code, opts) { | ||
import { normalizeReplacements } from "./options.js"; | ||
import parseAndBuildMetadata from "./parse.js"; | ||
import populatePlaceholders from "./populate.js"; | ||
export default function stringTemplate(formatter, code, opts) { | ||
code = formatter.code(code); | ||
let metadata; | ||
return arg => { | ||
const replacements = (0, _options.normalizeReplacements)(arg); | ||
if (!metadata) metadata = (0, _parse.default)(formatter, code, opts); | ||
return formatter.unwrap((0, _populate.default)(metadata, replacements)); | ||
const replacements = normalizeReplacements(arg); | ||
if (!metadata) metadata = parseAndBuildMetadata(formatter, code, opts); | ||
return formatter.unwrap(populatePlaceholders(metadata, replacements)); | ||
}; | ||
@@ -18,0 +12,0 @@ } |
{ | ||
"name": "@babel/template", | ||
"version": "7.22.5", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Generate an AST from a string template.", | ||
@@ -19,10 +19,14 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/code-frame": "^7.22.5", | ||
"@babel/parser": "^7.22.5", | ||
"@babel/types": "^7.22.5" | ||
"@babel/code-frame": "^8.0.0-alpha.0", | ||
"@babel/parser": "^8.0.0-alpha.0", | ||
"@babel/types": "^8.0.0-alpha.0" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
@@ -5,3 +5,3 @@ # @babel/template | ||
See our website [@babel/template](https://babeljs.io/docs/en/babel-template) for more information or the [issues](https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A%20template%22+is%3Aopen) associated with this package. | ||
See our website [@babel/template](https://babeljs.io/docs/babel-template) for more information or the [issues](https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A%20template%22+is%3Aopen) associated with this package. | ||
@@ -8,0 +8,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128551
1053
Yes
1
+ Added@babel/code-frame@8.0.0-alpha.13(transitive)
+ Added@babel/helper-string-parser@8.0.0-alpha.13(transitive)
+ Added@babel/helper-validator-identifier@8.0.0-alpha.13(transitive)
+ Added@babel/parser@8.0.0-alpha.13(transitive)
+ Added@babel/types@8.0.0-alpha.13(transitive)
+ Addedjs-tokens@8.0.3(transitive)
- Removed@babel/code-frame@7.26.2(transitive)
- Removed@babel/helper-string-parser@7.25.9(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@babel/parser@7.26.2(transitive)
- Removed@babel/types@7.26.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
Updated@babel/parser@^8.0.0-alpha.0
Updated@babel/types@^8.0.0-alpha.0