This commit is contained in:
David Kale
2020-09-08 13:25:36 -04:00
parent e4246d2b5b
commit 91fcbb0108
4227 changed files with 416837 additions and 457884 deletions

View File

@@ -5,49 +5,27 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = parseAndBuildMetadata;
function t() {
const data = _interopRequireWildcard(require("@babel/types"));
var t = _interopRequireWildcard(require("@babel/types"));
t = function () {
return data;
};
var _parser = require("@babel/parser");
return data;
}
var _codeFrame = require("@babel/code-frame");
function _parser() {
const data = require("@babel/parser");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
_parser = function () {
return data;
};
return data;
}
function _codeFrame() {
const data = require("@babel/code-frame");
_codeFrame = function () {
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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const PATTERN = /^[_$A-Z0-9]+$/;
function parseAndBuildMetadata(formatter, code, opts) {
const ast = parseWithCodeFrame(code, opts.parser);
const {
placeholderWhitelist,
placeholderPattern,
preserveComments,
syntacticPlaceholders
} = opts;
t().removePropertiesDeep(ast, {
const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders);
t.removePropertiesDeep(ast, {
preserveComments
});
formatter.validate(ast);
@@ -62,7 +40,7 @@ function parseAndBuildMetadata(formatter, code, opts) {
const isLegacyRef = {
value: undefined
};
t().traverse(ast, placeholderVisitorHandler, {
t.traverse(ast, placeholderVisitorHandler, {
syntactic,
legacy,
isLegacyRef,
@@ -76,9 +54,11 @@ function parseAndBuildMetadata(formatter, code, opts) {
}
function placeholderVisitorHandler(node, ancestors, state) {
var _state$placeholderWhi;
let name;
if (t().isPlaceholder(node)) {
if (t.isPlaceholder(node)) {
if (state.syntacticPlaceholders === false) {
throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false.");
} else {
@@ -87,10 +67,10 @@ function placeholderVisitorHandler(node, ancestors, state) {
}
} else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) {
return;
} else if (t().isIdentifier(node) || t().isJSXIdentifier(node)) {
} else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
name = node.name;
state.isLegacyRef.value = true;
} else if (t().isStringLiteral(node)) {
} else if (t.isStringLiteral(node)) {
name = node.value;
state.isLegacyRef.value = true;
} else {
@@ -101,7 +81,7 @@ function placeholderVisitorHandler(node, ancestors, state) {
throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
}
if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && (!state.placeholderWhitelist || !state.placeholderWhitelist.has(name))) {
if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !((_state$placeholderWhi = state.placeholderWhitelist) == null ? void 0 : _state$placeholderWhi.has(name))) {
return;
}
@@ -112,16 +92,16 @@ function placeholderVisitorHandler(node, ancestors, state) {
} = ancestors[ancestors.length - 1];
let type;
if (t().isStringLiteral(node) || t().isPlaceholder(node, {
if (t.isStringLiteral(node) || t.isPlaceholder(node, {
expectedNode: "StringLiteral"
})) {
type = "string";
} else if (t().isNewExpression(parent) && key === "arguments" || t().isCallExpression(parent) && key === "arguments" || t().isFunction(parent) && key === "params") {
} else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
type = "param";
} else if (t().isExpressionStatement(parent) && !t().isPlaceholder(node)) {
} else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
type = "statement";
ancestors = ancestors.slice(0, -1);
} else if (t().isStatement(node) && t().isPlaceholder(node)) {
} else if (t.isStatement(node) && t.isPlaceholder(node)) {
type = "statement";
} else {
type = "other";
@@ -167,22 +147,28 @@ function resolveAncestors(ast, ancestors) {
};
}
function parseWithCodeFrame(code, parserOpts) {
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: (parserOpts.plugins || []).concat("placeholders")
plugins
});
try {
return (0, _parser().parse)(code, parserOpts);
return (0, _parser.parse)(code, parserOpts);
} catch (err) {
const loc = err.loc;
if (loc) {
err.message += "\n" + (0, _codeFrame().codeFrameColumns)(code, {
err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, {
start: loc
});
err.code = "BABEL_TEMPLATE_PARSE_ERROR";