Skip to content

Commit

Permalink
Move try/catch to an helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 7, 2020
1 parent d213981 commit 17ebbf4
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/babel-core/src/config/full.js
Expand Up @@ -87,8 +87,9 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig(
const pluginDescriptorsByPass = [[]];
const passes = [];

try {
const ignored = yield* (function* recursePresetDescriptors(
const ignored = yield* enhanceError(
context,
function* recursePresetDescriptors(
rawPresets: Array<UnloadedDescriptor>,
pluginDescriptorsPass: Array<UnloadedDescriptor>,
) {
Expand Down Expand Up @@ -134,10 +135,15 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig(
});
}
}
})(presetsDescriptors, pluginDescriptorsByPass[0]);
},
)(presetsDescriptors, pluginDescriptorsByPass[0]);

if (ignored) return null;
if (ignored) return null;

const opts: Object = optionDefaults;
mergeOptions(opts, options);

yield* enhanceError(context, function* loadPluginDescriptors() {
pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);

for (const descs of pluginDescriptorsByPass) {
Expand All @@ -159,18 +165,7 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig(
}
}
}
} catch (e) {
// There are a few case where thrown errors will try to annotate themselves multiple times, so
// to keep things simple we just bail out if re-wrapping the message.
if (!/^\[BABEL\]/.test(e.message)) {
e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
}

throw e;
}

const opts: Object = optionDefaults;
mergeOptions(opts, options);
})();

opts.plugins = passes[0];
opts.presets = passes
Expand All @@ -185,6 +180,22 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig(
};
});

function enhanceError<T: Function>(context, fn: T): T {
return (function* (arg1, arg2) {
try {
return yield* fn(arg1, arg2);
} catch (e) {
// There are a few case where thrown errors will try to annotate themselves multiple times, so
// to keep things simple we just bail out if re-wrapping the message.
if (!/^\[BABEL\]/.test(e.message)) {
e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
}

throw e;
}
}: any);
}

/**
* Load a generic plugin/preset from the given descriptor loaded from the config object.
*/
Expand Down

0 comments on commit 17ebbf4

Please sign in to comment.