-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] Implement transform support for
using
declarations
- Loading branch information
1 parent
500e298
commit 52ce528
Showing
30 changed files
with
424 additions
and
0 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
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,31 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
function dispose_SuppressedError(suppressed, error) { | ||
dispose_SuppressedError = function SuppressedError(suppressed, error) { | ||
this.suppressed = suppressed; | ||
this.error = error; | ||
this.stack = new Error().stack; | ||
}; | ||
dispose_SuppressedError.prototype = Object.create(Error.prototype, { | ||
constructor: { | ||
value: dispose_SuppressedError, | ||
writable: true, | ||
configurable: true, | ||
}, | ||
}); | ||
return new dispose_SuppressedError(suppressed, error); | ||
} | ||
|
||
export default function _dispose(stack, error, hasError, SuppressedError) { | ||
while (stack.length > 0) { | ||
const r = stack.pop(); | ||
try { | ||
r.d.call(r.v); | ||
} catch (e) { | ||
error = hasError | ||
? new (SuppressedError || dispose_SuppressedError)(e, error) | ||
: e; | ||
hasError = true; | ||
} | ||
} | ||
if (hasError) throw error; | ||
} |
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,13 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
export default function _using(stack, value) { | ||
if (value !== null && value !== void 0) { | ||
// core-js-pure uses Symbol.for("Symbol.dispose"). | ||
var dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")]; | ||
if (typeof dispose !== "function") { | ||
throw new TypeError(`Property [Symbol.dispose] is not a function.`); | ||
} | ||
stack.push({ v: value, d: dispose }); | ||
} | ||
return value; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-explicit-resource-management/.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-explicit-resource-management/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-explicit-resource-managements | ||
|
||
> Compile `using` declarations to ES2015 | ||
See our website [@babel/plugin-proposal-explicit-resource-managements](https://babeljs.io/docs/en/babel-plugin-proposal-explicit-resource-managements) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-explicit-resource-managements | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-proposal-explicit-resource-managements --dev | ||
``` |
47 changes: 47 additions & 0 deletions
47
packages/babel-plugin-proposal-explicit-resource-management/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,47 @@ | ||
{ | ||
"name": "@babel/plugin-proposal-explicit-resource-management", | ||
"version": "7.18.6", | ||
"description": "Compile `using` declarations to ES2015", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-proposal-explicit-resource-management" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-explicit-resource-management", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "workspace:^", | ||
"@babel/plugin-syntax-explicit-resource-management": "workspace:^" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0-0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "workspace:^", | ||
"@babel/helper-plugin-test-runner": "workspace:^" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"conditions": { | ||
"USE_ESM": [ | ||
{ | ||
"type": "module" | ||
}, | ||
null | ||
] | ||
}, | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "commonjs" | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/babel-plugin-proposal-explicit-resource-management/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,85 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import syntacExplicitResourceManagement from "@babel/plugin-syntax-explicit-resource-management"; | ||
import { types as t, template } from "@babel/core"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
export default declare(api => { | ||
// TOOD: assert version 7.22.0 | ||
api.assertVersion(7); | ||
|
||
return { | ||
name: "proposal-explicit-resource-management", | ||
inherits: syntacExplicitResourceManagement, | ||
|
||
visitor: { | ||
VariableDeclaration(path) { | ||
if (path.node.kind === "using") { | ||
throw path.buildCodeFrameError( | ||
`"using" declaration at the top-level of modules is not supported yet.`, | ||
); | ||
} | ||
}, | ||
ForOfStatement(path: NodePath<t.ForOfStatement>) { | ||
const { left } = path.node; | ||
if (!t.isVariableDeclaration(left, { kind: "using" })) return; | ||
|
||
const { id } = left.declarations[0]; | ||
const tmpId = path.scope.generateUidIdentifierBasedOnNode(id); | ||
left.declarations[0].id = tmpId; | ||
left.kind = "const"; | ||
|
||
path.ensureBlock(); | ||
path.node.body.body.unshift( | ||
t.variableDeclaration("using", [ | ||
t.variableDeclarator(id, t.cloneNode(tmpId)), | ||
]), | ||
); | ||
}, | ||
BlockStatement(path, state) { | ||
let stackId: t.Identifier | null = null; | ||
for (const node of path.node.body) { | ||
if (!t.isVariableDeclaration(node, { kind: "using" })) continue; | ||
node.kind = "const"; | ||
stackId ??= path.scope.generateUidIdentifier("stack"); | ||
|
||
node.declarations.forEach(decl => { | ||
decl.init = t.callExpression(state.addHelper("using"), [ | ||
t.cloneNode(stackId), | ||
decl.init, | ||
]); | ||
}); | ||
} | ||
if (!stackId) return; | ||
|
||
const errorId = path.scope.generateUidIdentifier("error"); | ||
const hasErrorId = path.scope.generateUidIdentifier("hasError"); | ||
|
||
const replacement = template.statement.ast` | ||
try { | ||
var ${stackId} = []; | ||
${path.node.body} | ||
} catch (_) { | ||
var ${errorId} = _; | ||
var ${hasErrorId} = true; | ||
} finally { | ||
${state.addHelper("dispose")}( | ||
${t.cloneNode(stackId)}, | ||
${t.cloneNode(errorId)}, | ||
${t.cloneNode(hasErrorId)}, | ||
// Pass SuppressedError so that it can be used with "pure" | ||
// polyfills that do not compile the contents of runtime | ||
// helpers. | ||
typeof SuppressedError !== undefined && SuppressedError | ||
); | ||
} | ||
`; | ||
|
||
if (path.parentPath.isFunction()) { | ||
path.replaceWith(t.blockStatement([replacement])); | ||
} else { | ||
path.replaceWith(replacement); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-explicit-resource-management/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,3 @@ | ||
{ | ||
"plugins": ["proposal-explicit-resource-management"] | ||
} |
4 changes: 4 additions & 0 deletions
4
...in-proposal-explicit-resource-management/test/fixtures/transform-sync/bare-block/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,4 @@ | ||
{ | ||
using x = obj; | ||
doSomethingWith(x); | ||
} |
10 changes: 10 additions & 0 deletions
10
...n-proposal-explicit-resource-management/test/fixtures/transform-sync/bare-block/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,10 @@ | ||
try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, obj); | ||
doSomethingWith(x); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
3 changes: 3 additions & 0 deletions
3
...roposal-explicit-resource-management/test/fixtures/transform-sync/for-await-head/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,3 @@ | ||
for await (using x of y) { | ||
doSomethingWith(x); | ||
} |
3 changes: 3 additions & 0 deletions
3
...sal-explicit-resource-management/test/fixtures/transform-sync/for-await-head/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 @@ | ||
{ | ||
"sourceType": "module" | ||
} |
10 changes: 10 additions & 0 deletions
10
...posal-explicit-resource-management/test/fixtures/transform-sync/for-await-head/output.mjs
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,10 @@ | ||
for await (const _x of y) try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, _x); | ||
doSomethingWith(x); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
3 changes: 3 additions & 0 deletions
3
...ugin-proposal-explicit-resource-management/test/fixtures/transform-sync/for-head/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,3 @@ | ||
for (using x of y) { | ||
doSomethingWith(x); | ||
} |
10 changes: 10 additions & 0 deletions
10
...gin-proposal-explicit-resource-management/test/fixtures/transform-sync/for-head/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,10 @@ | ||
for (const _x of y) try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, _x); | ||
doSomethingWith(x); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
4 changes: 4 additions & 0 deletions
4
...proposal-explicit-resource-management/test/fixtures/transform-sync/function-body/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,4 @@ | ||
if (test) { | ||
using x = obj; | ||
doSomethingWith(x); | ||
} |
10 changes: 10 additions & 0 deletions
10
...roposal-explicit-resource-management/test/fixtures/transform-sync/function-body/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,10 @@ | ||
if (test) try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, obj); | ||
doSomethingWith(x); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
4 changes: 4 additions & 0 deletions
4
...lugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/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,4 @@ | ||
function fn() { | ||
using x = obj; | ||
return doSomethingWith(x); | ||
} |
12 changes: 12 additions & 0 deletions
12
...ugin-proposal-explicit-resource-management/test/fixtures/transform-sync/if-body/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,12 @@ | ||
function fn() { | ||
try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, obj); | ||
return doSomethingWith(x); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...oposal-explicit-resource-management/test/fixtures/transform-sync/multiple-nested/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,11 @@ | ||
{ | ||
using x = obj; | ||
{ | ||
using y = call(() => { | ||
using z = obj; | ||
return z; | ||
}); | ||
stmt; | ||
} | ||
stmt; | ||
} |
31 changes: 31 additions & 0 deletions
31
...posal-explicit-resource-management/test/fixtures/transform-sync/multiple-nested/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,31 @@ | ||
try { | ||
var _stack = []; | ||
const x = babelHelpers.using(_stack, obj); | ||
try { | ||
var _stack2 = []; | ||
const y = babelHelpers.using(_stack2, call(() => { | ||
try { | ||
var _stack3 = []; | ||
const z = babelHelpers.using(_stack3, obj); | ||
return z; | ||
} catch (_) { | ||
var _error3 = _; | ||
var _hasError3 = true; | ||
} finally { | ||
babelHelpers.dispose(_stack3, _error3, _hasError3, typeof SuppressedError !== undefined && SuppressedError); | ||
} | ||
})); | ||
stmt; | ||
} catch (_) { | ||
var _error2 = _; | ||
var _hasError2 = true; | ||
} finally { | ||
babelHelpers.dispose(_stack2, _error2, _hasError2, typeof SuppressedError !== undefined && SuppressedError); | ||
} | ||
stmt; | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
9 changes: 9 additions & 0 deletions
9
...al-explicit-resource-management/test/fixtures/transform-sync/multiple-same-level/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,9 @@ | ||
{ | ||
stmt; | ||
using x = obj; | ||
stmt; | ||
using y = obj, z = obj; | ||
stmt; | ||
using w = obj; | ||
doSomethingWith(x, z); | ||
} |
16 changes: 16 additions & 0 deletions
16
...l-explicit-resource-management/test/fixtures/transform-sync/multiple-same-level/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,16 @@ | ||
try { | ||
var _stack = []; | ||
stmt; | ||
const x = babelHelpers.using(_stack, obj); | ||
stmt; | ||
const y = babelHelpers.using(_stack, obj), | ||
z = babelHelpers.using(_stack, obj); | ||
stmt; | ||
const w = babelHelpers.using(_stack, obj); | ||
doSomethingWith(x, z); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
} finally { | ||
babelHelpers.dispose(_stack, _error, _hasError, typeof SuppressedError !== undefined && SuppressedError); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-explicit-resource-management/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); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-explicit-resource-management/test/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 @@ | ||
{ "type": "module" } |
Oops, something went wrong.