Skip to content

Fix hoisting of function declarations in loops and switch#17691

Merged
nicolo-ribaudo merged 7 commits intobabel:mainfrom
liuxingbaoyu:fix-block-scoped-functions
Jan 23, 2026
Merged

Fix hoisting of function declarations in loops and switch#17691
nicolo-ribaudo merged 7 commits intobabel:mainfrom
liuxingbaoyu:fix-block-scoped-functions

Conversation

@liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Dec 28, 2025

Q                       A
Fixed Issues? Fixes #14960, Fixes #17694
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR fixes the regression introduced in #16398.
FunctionDeclaration behaves differently in different versions of the specification.

@babel-bot
Copy link
Collaborator

babel-bot commented Dec 28, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60575

@liuxingbaoyu
Copy link
Member Author

ok 45657 test/staging/sm/Function/10.2.1.1.6.js default # (success)

ok 47141 test/staging/sm/lexical-environment/block-scoped-functions-annex-b-arguments.js default # (success)

ok 47149 test/staging/sm/lexical-environment/block-scoped-functions-annex-b-same-name.js default # (success)

ok 47151 test/staging/sm/lexical-environment/block-scoped-functions-annex-b.js default # (success)

# 48156 tests, 41911 successes
# 0 new failures, 4 new successes

@liuxingbaoyu liuxingbaoyu force-pushed the fix-block-scoped-functions branch from 3d7e951 to 26e4d3a Compare December 28, 2025 01:03
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 28, 2025

Open in StackBlitz

commit: 70e15ee

@liuxingbaoyu
Copy link
Member Author

In Babel 8, we no longer need #16475, so I reverted it.

@liuxingbaoyu liuxingbaoyu force-pushed the fix-block-scoped-functions branch from dd171d3 to 0159501 Compare January 6, 2026 19:12
Comment on lines 1 to 9
{
"plugins": [
"transform-block-scoped-functions",
"transform-block-scoping",
"syntax-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-block-scoped-functions",
"transform-arrow-functions"
]
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, we should transform the new features first.
However, this is the order we've already used in preset-env, and it's simpler.

@liuxingbaoyu liuxingbaoyu force-pushed the fix-block-scoped-functions branch from 0159501 to 906140c Compare January 6, 2026 19:17
@liuxingbaoyu liuxingbaoyu force-pushed the fix-block-scoped-functions branch from 906140c to 70e15ee Compare January 6, 2026 22:56
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release label Jan 23, 2026
@nicolo-ribaudo nicolo-ribaudo changed the title fix: Refactor transform-block-scoped-functions Fix hosting of function declarations in blocks Jan 23, 2026
};
if (true) {
function _run() {
var _run = function () {
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary for something, or just a side-effect of the new transform?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a side effect.

@nicolo-ribaudo nicolo-ribaudo changed the title Fix hosting of function declarations in blocks Fix hosting of function declarations in loops and switch Jan 23, 2026
@JLHwung JLHwung changed the title Fix hosting of function declarations in loops and switch Fix hoisting of function declarations in loops and switch Jan 23, 2026
@nicolo-ribaudo nicolo-ribaudo merged commit 96bbc35 into babel:main Jan 23, 2026
54 checks passed
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issues encountered when converting babel to ES5 syntax [Bug]: functions defined inside a switch case block will be trasformed uncorrectly

4 participants