forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0da4f7e
commit 3f640b2
Showing
5 changed files
with
73 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @flow | ||
|
||
import gensync, { type Gensync } from "gensync"; | ||
|
||
export default function maybeAsync<T, Args: any[]>( | ||
fn: (...args: Args) => T, | ||
message: string, | ||
): Gensync<Args, T> { | ||
return gensync({ | ||
sync(...args) { | ||
const result = fn.apply(this, args); | ||
if (isThenable(result)) throw new Error(message); | ||
return result; | ||
}, | ||
async(...args) { | ||
return Promise.resolve(fn.apply(this, args)); | ||
}, | ||
}); | ||
} | ||
|
||
function isThenable(val: mixed): boolean { | ||
return ( | ||
!!val && | ||
(typeof val === "object" || typeof val === "function") && | ||
!!val.then && | ||
typeof val.then === "function" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/babel-core/test/fixtures/plugins/async-pre-disallowed/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const wait = t => new Promise(res => setTimeout(res, t)); | ||
|
||
const code = "function f() {}"; | ||
|
||
transform(code, { | ||
plugins: [ | ||
function() { | ||
return { | ||
async pre() { | ||
await wait(50); | ||
} | ||
} | ||
} | ||
] | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/babel-core/test/fixtures/plugins/async-pre-disallowed/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"throws": "You appear to be using an plugin with an async .pre, but Babel has been called synchronously." | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/babel-core/test/fixtures/plugins/async-pre/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const wait = t => new Promise(res => setTimeout(res, t)); | ||
|
||
const code = "function f() {}"; | ||
|
||
transform(code, { | ||
plugins: [ | ||
function() { | ||
return { | ||
async pre() { | ||
await wait(50); | ||
} | ||
} | ||
} | ||
] | ||
}, (err, res) => {}); |