@babel/template
Advanced tools
Comparing version 7.21.4-esm.2 to 7.21.4-esm.3
@@ -1,19 +0,25 @@ | ||
import { merge, validate } from "./options.js"; | ||
import stringTemplate from "./string.js"; | ||
import literalTemplate from "./literal.js"; | ||
const NO_PLACEHOLDER = validate({ | ||
"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)({ | ||
placeholderPattern: false | ||
}); | ||
export default function createTemplateBuilder(formatter, defaultOpts) { | ||
function createTemplateBuilder(formatter, defaultOpts) { | ||
const templateFnCache = new WeakMap(); | ||
const templateAstCache = new WeakMap(); | ||
const cachedOpts = defaultOpts || validate(null); | ||
const cachedOpts = defaultOpts || (0, _options.validate)(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(args[0])))); | ||
return extendedTrace((0, _string.default)(formatter, tpl, (0, _options.merge)(cachedOpts, (0, _options.validate)(args[0])))); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateFnCache.get(tpl); | ||
if (!builder) { | ||
builder = literalTemplate(formatter, tpl, cachedOpts); | ||
builder = (0, _literal.default)(formatter, tpl, cachedOpts); | ||
templateFnCache.set(tpl, builder); | ||
@@ -24,3 +30,3 @@ } | ||
if (args.length > 0) throw new Error("Unexpected extra params."); | ||
return createTemplateBuilder(formatter, merge(cachedOpts, validate(tpl))); | ||
return createTemplateBuilder(formatter, (0, _options.merge)(cachedOpts, (0, _options.validate)(tpl))); | ||
} | ||
@@ -32,7 +38,7 @@ throw new Error(`Unexpected template param ${typeof tpl}`); | ||
if (args.length > 1) throw new Error("Unexpected extra params."); | ||
return stringTemplate(formatter, tpl, merge(merge(cachedOpts, validate(args[0])), NO_PLACEHOLDER))(); | ||
return (0, _string.default)(formatter, tpl, (0, _options.merge)((0, _options.merge)(cachedOpts, (0, _options.validate)(args[0])), NO_PLACEHOLDER))(); | ||
} else if (Array.isArray(tpl)) { | ||
let builder = templateAstCache.get(tpl); | ||
if (!builder) { | ||
builder = literalTemplate(formatter, tpl, merge(cachedOpts, NO_PLACEHOLDER)); | ||
builder = (0, _literal.default)(formatter, tpl, (0, _options.merge)(cachedOpts, NO_PLACEHOLDER)); | ||
templateAstCache.set(tpl, builder); | ||
@@ -39,0 +45,0 @@ } |
@@ -1,2 +0,8 @@ | ||
import * as _t from "@babel/types"; | ||
"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"); | ||
const { | ||
@@ -14,3 +20,3 @@ assertExpressionStatement | ||
} | ||
export const smart = makeStatementFormatter(body => { | ||
const smart = makeStatementFormatter(body => { | ||
if (body.length > 1) { | ||
@@ -22,4 +28,6 @@ return body; | ||
}); | ||
export const statements = makeStatementFormatter(body => body); | ||
export const statement = makeStatementFormatter(body => { | ||
exports.smart = smart; | ||
const statements = makeStatementFormatter(body => body); | ||
exports.statements = statements; | ||
const statement = makeStatementFormatter(body => { | ||
if (body.length === 0) { | ||
@@ -33,3 +41,4 @@ throw new Error("Found nothing to return."); | ||
}); | ||
export const expression = { | ||
exports.statement = statement; | ||
const expression = { | ||
code: str => `(\n${str}\n)`, | ||
@@ -52,3 +61,4 @@ validate: ast => { | ||
}; | ||
export const program = { | ||
exports.expression = expression; | ||
const program = { | ||
code: str => str, | ||
@@ -58,3 +68,4 @@ validate: () => {}, | ||
}; | ||
exports.program = program; | ||
//# sourceMappingURL=formatters.js.map |
@@ -1,9 +0,20 @@ | ||
import * as formatters from "./formatters.js"; | ||
import createTemplateBuilder from "./builder.js"; | ||
export const smart = createTemplateBuilder(formatters.smart); | ||
export const statement = createTemplateBuilder(formatters.statement); | ||
export const statements = createTemplateBuilder(formatters.statements); | ||
export const expression = createTemplateBuilder(formatters.expression); | ||
export const program = createTemplateBuilder(formatters.program); | ||
export default Object.assign(smart.bind(undefined), { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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), { | ||
smart, | ||
@@ -16,3 +27,4 @@ statement, | ||
}); | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,11 @@ | ||
import { normalizeReplacements } from "./options.js"; | ||
import parseAndBuildMetadata from "./parse.js"; | ||
import populatePlaceholders from "./populate.js"; | ||
export default function literalTemplate(formatter, tpl, opts) { | ||
"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) { | ||
const { | ||
@@ -15,3 +21,3 @@ metadata, | ||
return arg => { | ||
const replacements = normalizeReplacements(arg); | ||
const replacements = (0, _options.normalizeReplacements)(arg); | ||
if (replacements) { | ||
@@ -24,3 +30,3 @@ Object.keys(replacements).forEach(key => { | ||
} | ||
return formatter.unwrap(populatePlaceholders(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements)); | ||
return formatter.unwrap((0, _populate.default)(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements)); | ||
}; | ||
@@ -39,3 +45,3 @@ }; | ||
nameSet = new Set(names); | ||
metadata = parseAndBuildMetadata(formatter, formatter.code(result.code), { | ||
metadata = (0, _parse.default)(formatter, formatter.code(result.code), { | ||
parser: opts.parser, | ||
@@ -42,0 +48,0 @@ placeholderWhitelist: new Set(result.names.concat(opts.placeholderWhitelist ? Array.from(opts.placeholderWhitelist) : [])), |
@@ -0,4 +1,12 @@ | ||
"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; } | ||
export function merge(a, b) { | ||
function merge(a, b) { | ||
const { | ||
@@ -18,3 +26,3 @@ placeholderWhitelist = a.placeholderWhitelist, | ||
} | ||
export function validate(opts) { | ||
function validate(opts) { | ||
if (opts != null && typeof opts !== "object") { | ||
@@ -54,3 +62,3 @@ throw new Error("Unknown template options."); | ||
} | ||
export function normalizeReplacements(replacements) { | ||
function normalizeReplacements(replacements) { | ||
if (Array.isArray(replacements)) { | ||
@@ -57,0 +65,0 @@ return replacements.reduce((acc, replacement, i) => { |
@@ -1,2 +0,10 @@ | ||
import * as _t from "@babel/types"; | ||
"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"); | ||
const { | ||
@@ -15,6 +23,4 @@ isCallExpression, | ||
} = _t; | ||
import { parse } from "@babel/parser"; | ||
import { codeFrameColumns } from "@babel/code-frame"; | ||
const PATTERN = /^[_$A-Z0-9]+$/; | ||
export default function parseAndBuildMetadata(formatter, code, opts) { | ||
function parseAndBuildMetadata(formatter, code, opts) { | ||
const { | ||
@@ -149,7 +155,7 @@ placeholderWhitelist, | ||
try { | ||
return parse(code, parserOpts); | ||
return (0, _parser.parse)(code, parserOpts); | ||
} catch (err) { | ||
const loc = err.loc; | ||
if (loc) { | ||
err.message += "\n" + codeFrameColumns(code, { | ||
err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, { | ||
start: loc | ||
@@ -156,0 +162,0 @@ }); |
@@ -1,2 +0,8 @@ | ||
import * as _t from "@babel/types"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = populatePlaceholders; | ||
var _t = require("@babel/types"); | ||
const { | ||
@@ -13,3 +19,3 @@ blockStatement, | ||
} = _t; | ||
export default function populatePlaceholders(metadata, replacements) { | ||
function populatePlaceholders(metadata, replacements) { | ||
const ast = cloneNode(metadata.ast); | ||
@@ -16,0 +22,0 @@ if (replacements) { |
@@ -1,11 +0,17 @@ | ||
import { normalizeReplacements } from "./options.js"; | ||
import parseAndBuildMetadata from "./parse.js"; | ||
import populatePlaceholders from "./populate.js"; | ||
export default function stringTemplate(formatter, code, opts) { | ||
"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) { | ||
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)); | ||
const replacements = (0, _options.normalizeReplacements)(arg); | ||
if (!metadata) metadata = (0, _parse.default)(formatter, code, opts); | ||
return formatter.unwrap((0, _populate.default)(metadata, replacements)); | ||
}; | ||
@@ -12,0 +18,0 @@ } |
{ | ||
"name": "@babel/template", | ||
"version": "7.21.4-esm.2", | ||
"version": "7.21.4-esm.3", | ||
"description": "Generate an AST from a string template.", | ||
@@ -19,5 +19,5 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/code-frame": "7.21.4-esm.2", | ||
"@babel/parser": "7.21.4-esm.2", | ||
"@babel/types": "7.21.4-esm.2" | ||
"@babel/code-frame": "7.21.4-esm.3", | ||
"@babel/parser": "7.21.4-esm.3", | ||
"@babel/types": "7.21.4-esm.3" | ||
}, | ||
@@ -24,0 +24,0 @@ "engines": { |
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
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
70047
601
+ Added@babel/code-frame@7.21.4-esm.3(transitive)
+ Added@babel/helper-string-parser@7.21.4-esm.3(transitive)
+ Added@babel/helper-validator-identifier@7.21.4-esm.3(transitive)
+ Added@babel/highlight@7.21.4-esm.3(transitive)
+ Added@babel/parser@7.21.4-esm.3(transitive)
+ Added@babel/types@7.21.4-esm.3(transitive)
- Removed@babel/code-frame@7.21.4-esm.2(transitive)
- Removed@babel/helper-string-parser@7.21.4-esm.2(transitive)
- Removed@babel/helper-validator-identifier@7.21.4-esm.2(transitive)
- Removed@babel/highlight@7.21.4-esm.2(transitive)
- Removed@babel/parser@7.21.4-esm.2(transitive)
- Removed@babel/types@7.21.4-esm.2(transitive)
Updated@babel/parser@7.21.4-esm.3
Updated@babel/types@7.21.4-esm.3