diff options
Diffstat (limited to 'node_modules/@babel/template')
| -rw-r--r-- | node_modules/@babel/template/README.md | 172 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/builder.js | 83 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/formatters.js | 63 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/index.js | 36 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/literal.js | 81 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/options.js | 69 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/parse.js | 154 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/populate.js | 132 | ||||
| -rw-r--r-- | node_modules/@babel/template/lib/string.js | 24 | ||||
| l--------- | node_modules/@babel/template/node_modules/.bin/babylon | 1 | ||||
| -rw-r--r-- | node_modules/@babel/template/package.json | 16 |
11 files changed, 831 insertions, 0 deletions
diff --git a/node_modules/@babel/template/README.md b/node_modules/@babel/template/README.md new file mode 100644 index 00000000..0219286c --- /dev/null +++ b/node_modules/@babel/template/README.md @@ -0,0 +1,172 @@ +# @babel/template + +> Generate an AST from a string template or template literal. + +In computer science, this is known as an implementation of quasiquotes. + +## Install + +```sh +npm install --save-dev @babel/template +``` + +## String Usage + +```js +import template from "@babel/template"; +import generate from "@babel/generator"; +import * as t from "@babel/types"; + +const buildRequire = template(` + var IMPORT_NAME = require(SOURCE); +`); + +const ast = buildRequire({ + IMPORT_NAME: t.identifier("myModule"), + SOURCE: t.stringLiteral("my-module") +}); + +console.log(generate(ast).code); +``` + +```js +const myModule = require("my-module"); +``` + +### `.ast` + +If no placeholders are in use and you just want a simple way to parse a +string into an AST, you can use the `.ast` version of the template. + +```js +const ast = template.ast(` + var myModule = require("my-module"); +`); +``` +which will parse and return the AST directly. + + +## Template Literal Usage + +```js +import template from "@babel/template"; +import generate from "@babel/generator"; +import * as t from "@babel/types"; + +const fn = template` + var IMPORT_NAME = require('${"my-module"}'); +`); + +const ast = fn({ + IMPORT_NAME: t.identifier("myModule"); +}); + +console.log(generate(ast).code); +``` + +Note that placeholders can be passed directly as part of the template literal +in order to make things as readable as possible, or they can be passed into +the template function. + +### `.ast` + +If no placeholders are in use and you just want a simple way to parse a +string into an AST, you can use the `.ast` version of the template. + +```js +const name = "my-module"; +const mod = "myModule"; + +const ast = template.ast` + var ${mod} = require("${name}"); +`; +``` +which will parse and return the AST directly. Note that unlike the string-based +version mentioned earlier, since this is a template literal, it is still +valid to perform replacements using template literal replacements. + + +## AST results + +The `@babel/template` API exposes a few flexible APIs to make it as easy as +possible to create ASTs with an expected structure. Each of these also has +the `.ast` property mentioned above. + +### `template` + +`template` returns either a single statement, or an array of +statements, depending on the parsed result. + +### `template.smart` + +This is the same as the default `template` API, returning either a single +node, or an array of nodes, depending on the parsed result. + +### `template.statement` + +`template.statement("foo;")()` returns a single statement node, and throw +an exception if the result is anything but a single statement. + +### `template.statements` + +`template.statements("foo;foo;")()` returns an array of statement nodes. + +### `template.expression` + +`template.expression("foo")()` returns the expression node. + +### `template.program` + +`template.program("foo;")()` returns the `Program` node for the template. + + +## API + +### `template(code, [opts])` + +#### code + +Type: `string` + +#### options + +`@babel/template` accepts all of the options from [babylon](https://github.com/babel/babel/tree/master/packages/babylon), and specifies +some defaults of its own: + +* `allowReturnOutsideFunction` is set to `true` by default. +* `allowSuperOutsideMethod` is set to `true` by default. +* `sourceType` is set to `module` by default. + +##### placeholderWhitelist + +Type: `Set<string>` +Default: `undefined` + +A set of placeholder names to automatically accept. Items in this list do +not need to match the given placeholder pattern. + +##### placeholderPattern + +Type: `RegExp | false` +Default: `/^[_$A-Z0-9]+$/` + +A pattern to search for when looking for Identifier and StringLiteral +nodes that should be considered placeholders. +'false' will disable placeholder searching entirely, leaving only the +'placeholderWhitelist' value to find placeholders. + +##### preserveComments + +Type: `boolean` +Default: `false` + +Set this to `true` to preserve any comments from the `code` parameter. + +#### Return value + +By default `@babel/template` returns a `function` which is invoked with an +optional object of replacements. See the usage section for an example. + +When using `.ast`, the AST will be returned directly. + +[babylon]: https://github.com/babel/babylon#options diff --git a/node_modules/@babel/template/lib/builder.js b/node_modules/@babel/template/lib/builder.js new file mode 100644 index 00000000..2a0e6297 --- /dev/null +++ b/node_modules/@babel/template/lib/builder.js @@ -0,0 +1,83 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = createTemplateBuilder; + +var _options = require("./options"); + +var _string = _interopRequireDefault(require("./string")); + +var _literal = _interopRequireDefault(require("./literal")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +const NO_PLACEHOLDER = (0, _options.validate)({ + placeholderPattern: false +}); + +function createTemplateBuilder(formatter, defaultOpts) { + const templateFnCache = new WeakMap(); + const templateAstCache = new WeakMap(); + 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((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 = (0, _literal.default)(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, (0, _options.merge)(cachedOpts, (0, _options.validate)(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 (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 = (0, _literal.default)(formatter, tpl, (0, _options.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; + } + }; +}
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/formatters.js b/node_modules/@babel/template/lib/formatters.js new file mode 100644 index 00000000..59e0984c --- /dev/null +++ b/node_modules/@babel/template/lib/formatters.js @@ -0,0 +1,63 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.program = exports.expression = exports.statement = exports.statements = exports.smart = void 0; + +function makeStatementFormatter(fn) { + return { + code: str => `/* @babel/template */;\n${str}`, + validate: () => {}, + unwrap: ast => { + return fn(ast.program.body.slice(1)); + } + }; +} + +const smart = makeStatementFormatter(body => { + if (body.length > 1) { + return body; + } else { + return body[0]; + } +}); +exports.smart = smart; +const statements = makeStatementFormatter(body => body); +exports.statements = statements; +const statement = 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]; +}); +exports.statement = statement; +const expression = { + code: str => `(\n${str}\n)`, + validate: ({ + program + }) => { + if (program.body.length > 1) { + throw new Error("Found multiple statements but wanted one"); + } + + const expression = program.body[0].expression; + + if (expression.start === 0) { + throw new Error("Parse result included parens."); + } + }, + unwrap: ast => ast.program.body[0].expression +}; +exports.expression = expression; +const program = { + code: str => str, + validate: () => {}, + unwrap: ast => ast.program +}; +exports.program = program;
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/index.js b/node_modules/@babel/template/lib/index.js new file mode 100644 index 00000000..7ce85e9f --- /dev/null +++ b/node_modules/@babel/template/lib/index.js @@ -0,0 +1,36 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = exports.program = exports.expression = exports.statements = exports.statement = exports.smart = void 0; + +var formatters = _interopRequireWildcard(require("./formatters")); + +var _builder = _interopRequireDefault(require("./builder")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } + +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, + statement, + statements, + expression, + program, + ast: smart.ast +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/literal.js b/node_modules/@babel/template/lib/literal.js new file mode 100644 index 00000000..6f614096 --- /dev/null +++ b/node_modules/@babel/template/lib/literal.js @@ -0,0 +1,81 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = literalTemplate; + +var _options = require("./options"); + +var _parse = _interopRequireDefault(require("./parse")); + +var _populate = _interopRequireDefault(require("./populate")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function literalTemplate(formatter, tpl, opts) { + const _buildLiteralData = buildLiteralData(formatter, tpl, opts), + metadata = _buildLiteralData.metadata, + names = _buildLiteralData.names; + + return arg => { + const defaultReplacements = arg.reduce((acc, replacement, i) => { + acc[names[i]] = replacement; + return acc; + }, {}); + return arg => { + const replacements = (0, _options.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((0, _populate.default)(metadata, replacements ? Object.assign(replacements, defaultReplacements) : defaultReplacements)); + }; + }; +} + +function buildLiteralData(formatter, tpl, opts) { + let names; + let nameSet; + let metadata; + let prefix = ""; + + do { + prefix += "$"; + const result = buildTemplateCode(tpl, prefix); + names = result.names; + nameSet = new Set(names); + metadata = (0, _parse.default)(formatter, formatter.code(result.code), { + parser: opts.parser, + placeholderWhitelist: new Set(result.names.concat(opts.placeholderWhitelist ? Array.from(opts.placeholderWhitelist) : [])), + placeholderPattern: opts.placeholderPattern, + preserveComments: opts.preserveComments + }); + } while (metadata.placeholders.some(placeholder => placeholder.isDuplicate && nameSet.has(placeholder.name))); + + 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 + }; +}
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/options.js b/node_modules/@babel/template/lib/options.js new file mode 100644 index 00000000..2a5a48a1 --- /dev/null +++ b/node_modules/@babel/template/lib/options.js @@ -0,0 +1,69 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.merge = merge; +exports.validate = validate; +exports.normalizeReplacements = normalizeReplacements; + +function _objectWithoutProperties(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]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } + +function merge(a, b) { + const _b$placeholderWhiteli = b.placeholderWhitelist, + placeholderWhitelist = _b$placeholderWhiteli === void 0 ? a.placeholderWhitelist : _b$placeholderWhiteli, + _b$placeholderPattern = b.placeholderPattern, + placeholderPattern = _b$placeholderPattern === void 0 ? a.placeholderPattern : _b$placeholderPattern, + _b$preserveComments = b.preserveComments, + preserveComments = _b$preserveComments === void 0 ? a.preserveComments : _b$preserveComments; + return { + parser: Object.assign({}, a.parser, b.parser), + placeholderWhitelist, + placeholderPattern, + preserveComments + }; +} + +function validate(opts) { + if (opts != null && typeof opts !== "object") { + throw new Error("Unknown template options."); + } + + const _ref = opts || {}, + placeholderWhitelist = _ref.placeholderWhitelist, + placeholderPattern = _ref.placeholderPattern, + preserveComments = _ref.preserveComments, + parser = _objectWithoutProperties(_ref, ["placeholderWhitelist", "placeholderPattern", "preserveComments"]); + + 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"); + } + + return { + parser, + placeholderWhitelist: placeholderWhitelist || undefined, + placeholderPattern: placeholderPattern == null ? undefined : placeholderPattern, + preserveComments: preserveComments == null ? false : preserveComments + }; +} + +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"); +}
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/parse.js b/node_modules/@babel/template/lib/parse.js new file mode 100644 index 00000000..ddaa69fb --- /dev/null +++ b/node_modules/@babel/template/lib/parse.js @@ -0,0 +1,154 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = parseAndBuildMetadata; + +function t() { + const data = _interopRequireWildcard(require("@babel/types")); + + t = function t() { + return data; + }; + + return data; +} + +function _babylon() { + const data = require("babylon"); + + _babylon = function _babylon() { + return data; + }; + + return data; +} + +function _codeFrame() { + const data = require("@babel/code-frame"); + + _codeFrame = function _codeFrame() { + return data; + }; + + return data; +} + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } + +const PATTERN = /^[_$A-Z0-9]+$/; + +function parseAndBuildMetadata(formatter, code, opts) { + const ast = parseWithCodeFrame(code, opts.parser); + const placeholderWhitelist = opts.placeholderWhitelist, + _opts$placeholderPatt = opts.placeholderPattern, + placeholderPattern = _opts$placeholderPatt === void 0 ? PATTERN : _opts$placeholderPatt, + preserveComments = opts.preserveComments; + t().removePropertiesDeep(ast, { + preserveComments + }); + formatter.validate(ast); + const placeholders = []; + const placeholderNames = new Set(); + t().traverse(ast, placeholderVisitorHandler, { + placeholders, + placeholderNames, + placeholderWhitelist, + placeholderPattern + }); + return { + ast, + placeholders, + placeholderNames + }; +} + +function placeholderVisitorHandler(node, ancestors, state) { + let name; + + if (t().isIdentifier(node) || t().isJSXIdentifier(node)) { + name = node.name; + } else if (t().isStringLiteral(node)) { + name = node.value; + } else { + return; + } + + if ((!state.placeholderPattern || !state.placeholderPattern.test(name)) && (!state.placeholderWhitelist || !state.placeholderWhitelist.has(name))) { + return; + } + + ancestors = ancestors.slice(); + const _ancestors = ancestors[ancestors.length - 1], + parent = _ancestors.node, + key = _ancestors.key; + let type; + + if (t().isStringLiteral(node)) { + type = "string"; + } else if (t().isNewExpression(parent) && key === "arguments" || t().isCallExpression(parent) && key === "arguments" || t().isFunction(parent) && key === "params") { + type = "param"; + } else if (t().isExpressionStatement(parent)) { + type = "statement"; + ancestors = ancestors.slice(0, -1); + } else { + type = "other"; + } + + state.placeholders.push({ + name, + type, + resolve: ast => resolveAncestors(ast, ancestors), + isDuplicate: state.placeholderNames.has(name) + }); + state.placeholderNames.add(name); +} + +function resolveAncestors(ast, ancestors) { + let parent = ast; + + for (let i = 0; i < ancestors.length - 1; i++) { + const _ancestors$i = ancestors[i], + key = _ancestors$i.key, + index = _ancestors$i.index; + + if (index === undefined) { + parent = parent[key]; + } else { + parent = parent[key][index]; + } + } + + const _ancestors2 = ancestors[ancestors.length - 1], + key = _ancestors2.key, + index = _ancestors2.index; + return { + parent, + key, + index + }; +} + +function parseWithCodeFrame(code, parserOpts) { + parserOpts = Object.assign({ + allowReturnOutsideFunction: true, + allowSuperOutsideMethod: true, + sourceType: "module" + }, parserOpts); + + try { + return (0, _babylon().parse)(code, parserOpts); + } catch (err) { + const loc = err.loc; + + if (loc) { + err.message += "\n" + (0, _codeFrame().codeFrameColumns)(code, { + start: loc + }); + err.code = "BABEL_TEMPLATE_PARSE_ERROR"; + } + + throw err; + } +}
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/populate.js b/node_modules/@babel/template/lib/populate.js new file mode 100644 index 00000000..a5746642 --- /dev/null +++ b/node_modules/@babel/template/lib/populate.js @@ -0,0 +1,132 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = populatePlaceholders; + +function t() { + const data = _interopRequireWildcard(require("@babel/types")); + + t = function t() { + return data; + }; + + return data; +} + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } + +function populatePlaceholders(metadata, replacements) { + const ast = t().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 => t().cloneNode(node)); + } else if (typeof replacement === "object") { + replacement = t().cloneNode(replacement); + } + } + + const _placeholder$resolve = placeholder.resolve(ast), + parent = _placeholder$resolve.parent, + key = _placeholder$resolve.key, + index = _placeholder$resolve.index; + + if (placeholder.type === "string") { + if (typeof replacement === "string") { + replacement = t().stringLiteral(replacement); + } + + if (!replacement || !t().isStringLiteral(replacement)) { + throw new Error("Expected string substitution"); + } + } else if (placeholder.type === "statement") { + if (index === undefined) { + if (!replacement) { + replacement = t().emptyStatement(); + } else if (Array.isArray(replacement)) { + replacement = t().blockStatement(replacement); + } else if (typeof replacement === "string") { + replacement = t().expressionStatement(t().identifier(replacement)); + } else if (!t().isStatement(replacement)) { + replacement = t().expressionStatement(replacement); + } + } else { + if (replacement && !Array.isArray(replacement)) { + if (typeof replacement === "string") { + replacement = t().identifier(replacement); + } + + if (!t().isStatement(replacement)) { + replacement = t().expressionStatement(replacement); + } + } + } + } else if (placeholder.type === "param") { + if (typeof replacement === "string") { + replacement = t().identifier(replacement); + } + + if (index === undefined) throw new Error("Assertion failure."); + } else { + if (typeof replacement === "string") { + replacement = t().identifier(replacement); + } + + if (Array.isArray(replacement)) { + throw new Error("Cannot replace single expression with an array."); + } + } + + if (index === undefined) { + t().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; + } + + t().validate(parent, key, items); + parent[key] = items; + } +}
\ No newline at end of file diff --git a/node_modules/@babel/template/lib/string.js b/node_modules/@babel/template/lib/string.js new file mode 100644 index 00000000..02ad4578 --- /dev/null +++ b/node_modules/@babel/template/lib/string.js @@ -0,0 +1,24 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = stringTemplate; + +var _options = require("./options"); + +var _parse = _interopRequireDefault(require("./parse")); + +var _populate = _interopRequireDefault(require("./populate")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +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)); + }; +}
\ No newline at end of file diff --git a/node_modules/@babel/template/node_modules/.bin/babylon b/node_modules/@babel/template/node_modules/.bin/babylon new file mode 120000 index 00000000..5acfefcc --- /dev/null +++ b/node_modules/@babel/template/node_modules/.bin/babylon @@ -0,0 +1 @@ +../../../../babylon/bin/babylon.js
\ No newline at end of file diff --git a/node_modules/@babel/template/package.json b/node_modules/@babel/template/package.json new file mode 100644 index 00000000..55eafdb4 --- /dev/null +++ b/node_modules/@babel/template/package.json @@ -0,0 +1,16 @@ +{ + "name": "@babel/template", + "version": "7.0.0-beta.47", + "description": "Generate an AST from a string template.", + "author": "Sebastian McKenzie <sebmck@gmail.com>", + "homepage": "https://babeljs.io/", + "license": "MIT", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-template", + "main": "lib/index.js", + "dependencies": { + "@babel/code-frame": "7.0.0-beta.47", + "@babel/types": "7.0.0-beta.47", + "babylon": "7.0.0-beta.47", + "lodash": "^4.17.5" + } +} |
