Merge pull request #6818 from loganfsmyth/async-warnings

Add some nice warnings if plugins happen to return promises instead of sync values.
This commit is contained in:
Logan Smyth 2017-11-13 12:42:58 -08:00 committed by GitHub
commit e44cef3473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 2 deletions

View File

@ -142,6 +142,7 @@ const readConfigJS = makeStrongCache((filepath, cache) => {
cache,
// Expose ".env()" so people can easily get the same env that we expose using the "env" key.
env: () => cache.using(() => getEnv()),
async: () => false,
});
} else {
cache.forever();
@ -153,6 +154,16 @@ const readConfigJS = makeStrongCache((filepath, cache) => {
);
}
if (typeof options.then === "function") {
throw new Error(
`You appear to be using an async configuration, ` +
`which your current version of Babel does not support. ` +
`We may add support for this in the future, ` +
`but if you're on the most recent version of @babel/core and still ` +
`seeing this error, then you'll need to synchronously return your config.`,
);
}
return {
filepath,
dirname: path.dirname(filepath),

View File

@ -189,6 +189,7 @@ const loadDescriptor = makeWeakCache(
const api = Object.assign(Object.create(context), {
cache,
env: () => cache.using(() => getEnv()),
async: () => false,
});
try {
@ -205,6 +206,15 @@ const loadDescriptor = makeWeakCache(
throw new Error("Plugin/Preset did not return an object.");
}
if (typeof item.then === "function") {
throw new Error(
`You appear to be using an async plugin, ` +
`which your current version of Babel does not support.` +
`If you're using a published plugin, ` +
`you may need to upgrade your @babel/core version.`,
);
}
return { value: item, options, dirname, alias };
},
);

View File

@ -38,6 +38,15 @@ export default function generateCode(
result = generate(ast, opts.generatorOpts, code);
} else if (results.length === 1) {
result = results[0];
if (typeof result.then === "function") {
throw new Error(
`You appear to be using an async parser plugin, ` +
`which your current version of Babel does not support. ` +
`If you're using a published plugin, ` +
`you may need to upgrade your @babel/core version.`,
);
}
} else {
throw new Error("More than one plugin attempted to override codegen.");
}

View File

@ -86,7 +86,18 @@ function transformFile(file: File, pluginPasses: PluginPasses): void {
for (const [plugin, pass] of passPairs) {
const fn = plugin.pre;
if (fn) fn.call(pass, file);
if (fn) {
const result = fn.call(pass, file);
if (isThenable(result)) {
throw new Error(
`You appear to be using an plugin with an async .pre, ` +
`which your current version of Babel does not support.` +
`If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`,
);
}
}
}
// merge all plugin visitors into a single visitor
@ -99,7 +110,26 @@ function transformFile(file: File, pluginPasses: PluginPasses): void {
for (const [plugin, pass] of passPairs) {
const fn = plugin.post;
if (fn) fn.call(pass, file);
if (fn) {
const result = fn.call(pass, file);
if (isThenable(result)) {
throw new Error(
`You appear to be using an plugin with an async .post, ` +
`which your current version of Babel does not support.` +
`If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`,
);
}
}
}
}
}
function isThenable(val: mixed): boolean {
return (
!!val &&
(typeof val === "object" || typeof val === "function") &&
typeof val.then === "function"
);
}

View File

@ -76,6 +76,14 @@ function parser(pluginPasses, options, code) {
if (results.length === 0) {
return parse(code, options.parserOpts);
} else if (results.length === 1) {
if (typeof results[0].then === "function") {
throw new Error(
`You appear to be using an async codegen plugin, ` +
`which your current version of Babel does not support. ` +
`If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`,
);
}
return results[0];
}
throw new Error("More than one plugin attempted to override parsing.");

View File

@ -28,6 +28,14 @@ export function _call(fns?: Array<Function>): boolean {
if (!node) return true;
const ret = fn.call(this.state, this, this.state);
if (ret && typeof ret === "object" && typeof ret.then === "function") {
throw new Error(
`You appear to be using an plugin with an async traversay visitors, ` +
`which your current version of Babel does not support.` +
`If you're using a published plugin, you may need to upgrade ` +
`your @babel/core version.`,
);
}
if (ret) {
throw new Error(`Unexpected return value from visitor method ${fn}`);
}