Visitar URL original
Transform discard binding by JLHwung · Pull Request #17277 · babel/babel · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bf7ae1f
transform discard binding
JLHwung Apr 30, 2025
2524782
refactor extract functions to utils.ts
JLHwung Apr 30, 2025
cbd6c81
expand scope integrity check to all binding identifiers
JLHwung Apr 30, 2025
6afa04c
update incomplete comment
JLHwung Apr 30, 2025
c2ba0eb
update test output fixtures
JLHwung Apr 30, 2025
90a1f2f
remove forXStatement skip
JLHwung May 1, 2025
71ab6f6
add more test cases
JLHwung May 1, 2025
344b8ed
feat: remove trailing void patterns on ignoreFunctionLength assumption
JLHwung May 2, 2025
1568180
make node 6 happy
JLHwung May 2, 2025
590d1d3
add more test cases
JLHwung May 2, 2025
6224ab7
skip some exec tests for Node < 12
JLHwung May 2, 2025
22dcd27
update tsconfig.json
JLHwung May 2, 2025
15b8881
update test fixtures
JLHwung May 5, 2025
d835e22
fix: handle integration issues
JLHwung May 5, 2025
58f76f7
run basic exec test on Node.js >= 16
JLHwung May 5, 2025
d663174
update test case
JLHwung May 5, 2025
a22cc8b
feat: pass syntaxType from plugin option
JLHwung May 5, 2025
f634080
skip basic test for Node.js 6
JLHwung May 5, 2025
50f72b6
add more test cases
JLHwung May 6, 2025
c79a32f
add new multiple-void test case
JLHwung May 27, 2025
ae6f845
fix: disable named evaluation for parenthesized rhs
JLHwung May 27, 2025
68c6d03
fix: handle using named evaluation
JLHwung May 27, 2025
bd03809
Revert "fix: disable named evaluation for parenthesized rhs"
JLHwung May 27, 2025
6777933
prefix anonymous function with 0,
JLHwung May 27, 2025
b203c7a
improve takeVoidPatternComments
JLHwung May 28, 2025
2bc97e3
use NodePath#splitExportDeclaration
JLHwung Jun 16, 2025
57d218e
update test fixtures
JLHwung Jun 16, 2025
61db5b3
update test options
JLHwung Jun 16, 2025
3e5cf7e
update test fixtures due to regenerator runtime changes
JLHwung Jun 16, 2025
effcb90
fix: remove VoidPattern from LVal
JLHwung Jun 25, 2025
e4e395c
fix: improve destructuring transform detection
JLHwung Jun 25, 2025
e6fabdf
fix incorrect typings
JLHwung Jun 25, 2025
9ac2065
update isAnonymousFunctionDefinition
JLHwung Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
prefix anonymous function with 0,
  • Loading branch information
JLHwung committed Jun 27, 2025
commit 6777933d989cb824d853a50a74a2f813af3f6864
8 changes: 3 additions & 5 deletions packages/babel-plugin-proposal-discard-binding/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,18 @@ export function removeTrailingVoidPatternsFromParams(
export function isAnonymousFunctionDefinition(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this check. There is no good reason for anybody to write using void = () => {}, but if they do we might as well inject (0,) and avoid having two different code paths for classes and functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if they do they will get a runtime error for sure. Added the function / arrow function check.

node: t.Node,
): node is t.ClassExpression {
return t.isClassExpression(node) && !node.id && !node.extra?.parenthesized;
return t.isClassExpression(node) && !node.id;
}

/**
* Handle using named evaluation, e.g. `using void = class { ... }`.
* Here we parenthesize the rhs to disable the named evaluation,
* Here we prefix the rhs with `0,` to disable the named evaluation,
* such that the `name` of the class in rhs is always an empty string
* @param path The init of the variable declarator
* @param state The plugin pass object
*/
export function handleUsingNamedEvaluation(path: NodePath<t.Expression>) {
if (isAnonymousFunctionDefinition(path.node)) {
const { extra = {} } = path.node;
extra.parenthesized = true;
path.node.extra = extra;
path.replaceWith(t.sequenceExpression([t.numericLiteral(0), path.node]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
let name;
using void = class { static _ = name = this.name; static [Symbol.dispose || Symbol.for("Symbol.dispose")]() {} };

expect(name).toBe("");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let name;
using void = class { static _ = name = this.name; static [Symbol.dispose || Symbol.for("Symbol.dispose")]() {} };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"minNodeVersion": "24.0.0",
"plugins": [["proposal-discard-binding", { "syntaxType": "void" }], "syntax-explicit-resource-management"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
let name;
using _ = (0, class {
static _ = name = this.name;
static [Symbol.dispose || Symbol.for("Symbol.dispose")]() {}
});
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
try {
var _usingCtx = babelHelpers.usingCtx();
let name;
const _ = _usingCtx.u(class {
const _ = _usingCtx.u((0, class {
static _ = name = this.name;
static [Symbol.dispose || Symbol.for("Symbol.dispose")]() {}
});
}));
} catch (_) {
_usingCtx.e = _;
} finally {
Expand Down