Make tryCombinations throws AggregateError(like) error (#14388)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-02-21 16:34:42 +08:00
1 parent b6766551c7
commit 5d04f01c82
6 files changed
+42 -30

No files matched your search

+4 -3
View File
@@ -72,9 +72,10 @@ function parse(text, options = {}) {
(sourceType) => () => parseWithOptions(text, sourceType)
);
const { result: ast, error } = tryCombinations(combinations);
if (!ast) {
let ast;
try {
ast = tryCombinations(combinations);
} catch ({ errors: [error] }) {
throw createParseError(error);
}
+8 -6
View File
@@ -145,13 +145,15 @@ function createParse({ isExpression = false, optionsCombinations }) {
/** @type {Parse} */
const parseFunction = isExpression ? parseExpression : babelParse;
let { result: ast, error } = tryCombinations(
combinations.map(
(options) => () => parseWithOptions(parseFunction, text, options)
)
);
if (!ast) {
let ast;
try {
ast = tryCombinations(
combinations.map(
(options) => () => parseWithOptions(parseFunction, text, options)
)
);
} catch ({ errors: [error] }) {
throw createBabelParseError(error);
}
+4 -3
View File
@@ -45,9 +45,10 @@ function parse(text, options = {}) {
espreeParse(text, { ...parseOptions, sourceType })
);
const { result: ast, error } = tryCombinations(combinations);
if (!ast) {
let ast;
try {
ast = tryCombinations(combinations);
} catch ({ errors: [error] }) {
throw createParseError(error);
}
+4 -2
View File
@@ -91,9 +91,11 @@ function parse(text, options = {}) {
const combinations = (sourceType ? [sourceType] : ["module", "script"]).map(
(sourceType) => () => parseWithOptions(text, sourceType)
);
const { result: ast, error } = tryCombinations(combinations);
if (!ast) {
let ast;
try {
ast = tryCombinations(combinations);
} catch ({ errors: [error] }) {
throw createParseError(error);
}
+14 -9
View File
@@ -41,15 +41,20 @@ function parse(text, options = {}) {
const textToParse = replaceHashbang(text);
const jsx = isProbablyJsx(text);
const { result, error } = tryCombinations([
// Try passing with our best guess first.
() => parseWithNodeMaps(textToParse, { ...parseOptions, jsx }),
// But if we get it wrong, try the opposite.
() => parseWithNodeMaps(textToParse, { ...parseOptions, jsx: !jsx }),
]);
if (!result) {
// Suppose our guess is correct, throw the first error
let result;
try {
result = tryCombinations([
// Try passing with our best guess first.
() => parseWithNodeMaps(textToParse, { ...parseOptions, jsx }),
// But if we get it wrong, try the opposite.
() => parseWithNodeMaps(textToParse, { ...parseOptions, jsx: !jsx }),
]);
} catch ({
errors: [
// Suppose our guess is correct, throw the first error
error,
],
}) {
throw createParseError(error);
}
+8 -7
View File
@@ -1,15 +1,16 @@
function tryCombinations(combinations) {
let firstError;
for (const [index, fn] of combinations.entries()) {
const errors = [];
for (const fn of combinations) {
try {
return { result: fn() };
return fn();
} catch (error) {
if (index === 0) {
firstError = error;
}
errors.push(error);
}
}
return { error: firstError };
// TODO: Use `AggregateError` when we drop Node.js v14
// throw new AggregateError(errors, "All combinations failed");
throw Object.assign(new Error("All combinations failed"), { errors });
}
export default tryCombinations;