Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
async-do-expressions
transform (#13117)
Co-authored-by: Brian Ng <bng412@gmail.com>
- Loading branch information
1 parent
1989353
commit e9e5cab
Showing
27 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-async-do-expressions/.npmignore
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 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-proposal-async-do-expressions/README.md
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,19 @@ | ||
# @babel/plugin-proposal-async-do-expressions | ||
|
||
> Transforms async do expressions to ES2021 | ||
See our website [@babel/plugin-proposal-async-do-expressions](https://babeljs.io/docs/en/babel-plugin-proposal-async-do-expressions) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-async-do-expressions | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-proposal-async-do-expressions --dev | ||
``` |
36 changes: 36 additions & 0 deletions
36
packages/babel-plugin-proposal-async-do-expressions/package.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,36 @@ | ||
{ | ||
"name": "@babel/plugin-proposal-async-do-expressions", | ||
"version": "7.13.11", | ||
"description": "Transform async do expressions to ES2021", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-proposal-async-do-expressions" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js" | ||
}, | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"@babel/helper-hoist-variables": "workspace:^7.13.0", | ||
"@babel/helper-plugin-utils": "workspace:^7.13.0", | ||
"@babel/plugin-syntax-async-do-expressions": "workspace:^7.13.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.13.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "workspace:*", | ||
"@babel/helper-plugin-test-runner": "workspace:*", | ||
"@babel/traverse": "workspace:*", | ||
"@babel/types": "workspace:*" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-async-do-expressions" | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/babel-plugin-proposal-async-do-expressions/src/index.ts
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,51 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import syntaxAsyncDoExpressions from "@babel/plugin-syntax-async-do-expressions"; | ||
import hoistVariables from "@babel/helper-hoist-variables"; | ||
import type * as t from "@babel/types"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
export default declare(({ types: t, assertVersion }) => { | ||
assertVersion("^7.13.0"); | ||
|
||
return { | ||
name: "proposal-async-do-expressions", | ||
inherits: syntaxAsyncDoExpressions, | ||
visitor: { | ||
DoExpression: { | ||
exit(path: NodePath<t.DoExpression>) { | ||
if (!path.is("async")) { | ||
// non-async do expressions are handled by proposal-do-expressions | ||
return; | ||
} | ||
const { scope } = path; | ||
// Hoist variable declaration to containing function scope | ||
// `async do { var x = 1; x }` -> `var x; (async() => { x = 1; return x })()` | ||
hoistVariables( | ||
path, | ||
(id: t.Identifier) => { | ||
scope.push({ id: t.cloneNode(id) }); | ||
}, | ||
"var", | ||
); | ||
const bodyPath = path.get("body"); | ||
|
||
// add implicit returns to all ending expression statements | ||
const completionRecords = bodyPath.getCompletionRecords(); | ||
|
||
for (const p of completionRecords) { | ||
if (p.isExpressionStatement()) { | ||
p.replaceWith(t.returnStatement(p.node.expression)); | ||
} | ||
} | ||
|
||
path.replaceWith( | ||
t.callExpression( | ||
t.arrowFunctionExpression([], bodyPath.node, /* async */ true), | ||
[], | ||
), | ||
); | ||
}, | ||
}, | ||
}, | ||
}; | ||
}); |
4 changes: 4 additions & 0 deletions
4
...bel-plugin-proposal-async-do-expressions/test/fixtures/async-do-expressions/basic/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,4 @@ | ||
const x = async do { 21 + 21 }; | ||
|
||
expect(x).toBeInstanceOf(Promise); | ||
x.then((v) => expect(v).toBe(42)).catch(err => { throw err }); |
1 change: 1 addition & 0 deletions
1
...el-plugin-proposal-async-do-expressions/test/fixtures/async-do-expressions/basic/input.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 @@ | ||
const x = async do { 21 + 21 }; |
3 changes: 3 additions & 0 deletions
3
...l-plugin-proposal-async-do-expressions/test/fixtures/async-do-expressions/basic/output.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,3 @@ | ||
const x = (async () => { | ||
return 21 + 21; | ||
})(); |
1 change: 1 addition & 0 deletions
1
...sal-async-do-expressions/test/fixtures/async-do-expressions/empty-statement-list/input.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 @@ | ||
async do {} |
1 change: 1 addition & 0 deletions
1
...al-async-do-expressions/test/fixtures/async-do-expressions/empty-statement-list/output.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 @@ | ||
(async () => {})(); |
6 changes: 6 additions & 0 deletions
6
...sions/test/fixtures/async-do-expressions/hoist-variables-assgined-in-sync-context/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,6 @@ | ||
const x = async do { var y = 21; y + y } | ||
|
||
expect(y).toBe(21); | ||
x.then((resolved) => { | ||
expect(resolved).toBe(42); | ||
}).catch(err => { throw err }); |
1 change: 1 addition & 0 deletions
1
...ions/test/fixtures/async-do-expressions/hoist-variables-assgined-in-sync-context/input.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 @@ | ||
const x = async do { var y = 21; y + y } |
6 changes: 6 additions & 0 deletions
6
...ons/test/fixtures/async-do-expressions/hoist-variables-assgined-in-sync-context/output.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,6 @@ | ||
var y; | ||
|
||
const x = (async () => { | ||
y = 21; | ||
return y + y; | ||
})(); |
7 changes: 7 additions & 0 deletions
7
...ions/test/fixtures/async-do-expressions/hoist-variables-assigned-in-async-context/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,7 @@ | ||
const x = async do { await Promise.resolve(); var y = 21; y + y } | ||
|
||
expect(y).toBe(undefined); | ||
x.then((resolved) => { | ||
expect(y).toBe(21); | ||
expect(resolved).toBe(42); | ||
}).catch(err => { throw err }); |
1 change: 1 addition & 0 deletions
1
...ons/test/fixtures/async-do-expressions/hoist-variables-assigned-in-async-context/input.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 @@ | ||
const x = async do { await Promise.resolve(); var y = 21; y + y } |
7 changes: 7 additions & 0 deletions
7
...ns/test/fixtures/async-do-expressions/hoist-variables-assigned-in-async-context/output.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,7 @@ | ||
var y; | ||
|
||
const x = (async () => { | ||
await Promise.resolve(); | ||
y = 21; | ||
return y + y; | ||
})(); |
4 changes: 4 additions & 0 deletions
4
...posal-async-do-expressions/test/fixtures/async-do-expressions/lexical-declaration/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,4 @@ | ||
const x = async do { let x = 21; x + x }; | ||
|
||
expect(x).toBeInstanceOf(Promise); | ||
x.then((v) => expect(v).toBe(42)).catch(err => { throw err }); |
1 change: 1 addition & 0 deletions
1
...osal-async-do-expressions/test/fixtures/async-do-expressions/lexical-declaration/input.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 @@ | ||
const x = async do { let x = 21; x + x }; |
5 changes: 5 additions & 0 deletions
5
...sal-async-do-expressions/test/fixtures/async-do-expressions/lexical-declaration/output.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,5 @@ | ||
const x = (async () => { | ||
let x = 21; | ||
return x + x; | ||
})(); | ||
|
5 changes: 5 additions & 0 deletions
5
...-proposal-async-do-expressions/test/fixtures/async-do-expressions/throw-statement/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,5 @@ | ||
const x = async do { throw new Error("sync") }; | ||
|
||
expect(x).toBeInstanceOf(Promise); | ||
x.then(() => { throw new Error("expected an error: sync is thrown.") }) | ||
.catch(err => { expect(err.message).toEqual("sync") }); |
1 change: 1 addition & 0 deletions
1
...proposal-async-do-expressions/test/fixtures/async-do-expressions/throw-statement/input.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 @@ | ||
const x = async do { throw new Error("sync") }; |
3 changes: 3 additions & 0 deletions
3
...roposal-async-do-expressions/test/fixtures/async-do-expressions/throw-statement/output.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,3 @@ | ||
const x = (async () => { | ||
throw new Error("sync"); | ||
})(); |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-proposal-async-do-expressions/test/fixtures/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,4 @@ | ||
{ | ||
"plugins": ["proposal-async-do-expressions"], | ||
"minNodeVersion": "8.0.0" | ||
} |
2 changes: 2 additions & 0 deletions
2
...bel-plugin-proposal-async-do-expressions/test/fixtures/sync-do-expressions/basic/input.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,2 @@ | ||
// sync do expressions are not handled by this plugin | ||
(do {}); |
4 changes: 4 additions & 0 deletions
4
...plugin-proposal-async-do-expressions/test/fixtures/sync-do-expressions/basic/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,4 @@ | ||
{ | ||
"plugins": ["proposal-async-do-expressions"], | ||
"minNodeVersion": "6.0.0" | ||
} |
2 changes: 2 additions & 0 deletions
2
...el-plugin-proposal-async-do-expressions/test/fixtures/sync-do-expressions/basic/output.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,2 @@ | ||
// sync do expressions are not handled by this plugin | ||
(do {}); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-async-do-expressions/test/index.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,3 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
|
||
runner(import.meta.url); |
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