Skip to content

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Jul 16, 2025

The root cause is that the variable was hoisted to a scope that has the same name binding

var a = 1;
try {
  throw 2
} catch (a) {
  var a = 3;
//    ^ Shouldn't hoist to the top scope because there is a same name variable.
}
console.log(a);

Ref: https://tc39.es/ecma262/#sec-variablestatements-in-catch-blocks

Note

The Block of a Catch clause may contain var declarations that bind a name that is also bound by the CatchParameter. At runtime, such bindings are instantiated in the VariableDeclarationEnvironment. They do not shadow the same-named bindings introduced by the CatchParameter and hence the Initializer for such var declarations will assign to the corresponding catch parameter rather than the var binding.

@github-actions github-actions bot added A-semantic Area - Semantic C-bug Category - Bug labels Jul 16, 2025
Copy link
Member Author

Dunqing commented Jul 16, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 16, 2025

CodSpeed Instrumentation Performance Report

Merging #12313 will not alter performance

Comparing 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly (cd98426) with main (fb8289c)

Summary

✅ 34 untouched benchmarks

@Dunqing Dunqing force-pushed the 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly branch from ed829b5 to 5b32885 Compare July 16, 2025 05:45
@Boshen Boshen changed the title fix(semantic): handle hoisting variable binding incorrectly fix(semantic): handle var hoisting in catch block Jul 16, 2025
@Boshen Boshen changed the title fix(semantic): handle var hoisting in catch block fix(semantic): handle var hoisting in catch block with same catch parameter name Jul 16, 2025
@Dunqing Dunqing force-pushed the 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly branch from 5b32885 to beda069 Compare July 16, 2025 05:53
@github-actions github-actions bot added the A-minifier Area - Minifier label Jul 16, 2025
@Dunqing Dunqing force-pushed the 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly branch from 85f6477 to 5992963 Compare July 16, 2025 06:39
@Boshen Boshen marked this pull request as ready for review July 16, 2025 06:50
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jul 16, 2025
Copy link
Member

Boshen commented Jul 16, 2025

Merge activity

…ameter name (#12313)

The root cause is that the variable was hoisted to a scope that has the same name binding

```js
var a = 1;
try {
  throw 2
} catch (a) {
  var a = 3;
//    ^ Shouldn't hoist to the top scope because there is a same name variable.
}
console.log(a);
```

Ref: https://tc39.es/ecma262/#sec-variablestatements-in-catch-blocks

Note

The [Block](https://tc39.es/ecma262/#prod-Block) of a [Catch](https://tc39.es/ecma262/#prod-Catch) clause may contain var declarations that bind a name that is also bound by the [CatchParameter](https://tc39.es/ecma262/#prod-CatchParameter). At runtime, such bindings are instantiated in the VariableDeclarationEnvironment. They do not shadow the same-named bindings introduced by the [CatchParameter](https://tc39.es/ecma262/#prod-CatchParameter) and hence the [Initializer](https://tc39.es/ecma262/#prod-Initializer) for such var declarations will assign to the corresponding catch parameter rather than the var binding.
@graphite-app graphite-app bot force-pushed the 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly branch from 5992963 to cd98426 Compare July 16, 2025 06:54
@graphite-app graphite-app bot merged commit cd98426 into main Jul 16, 2025
24 checks passed
@graphite-app graphite-app bot deleted the 07-16-fix_semantic_handle_hoisting_variable_binding_incorrectly branch July 16, 2025 07:02
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier A-semantic Area - Semantic C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants