Make tryCombinations throws AggregateError(like) error (#14388)
This commit is contained in:
1 parent
b6766551c7
commit
5d04f01c82
6 files changed
+42
-30
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user