Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformer-optional-catch-binding): the unused binding is not in the correct scope #5066

Conversation

Dunqing
Copy link
Member

@Dunqing Dunqing commented Aug 22, 2024

Blocked by #5008

In the SemanticBuilder, we insert all CatchClause parameters in the BlockStatement scope, a child-scope of CatchClause

// Move all bindings from catch clause param scope to catch clause body scope
// to make it easier to resolve references and check redeclare errors
if self.scope.get_flags(parent_scope_id).is_catch_clause() {
let parent_bindings =
self.scope.get_bindings_mut(parent_scope_id).drain(..).collect::<Bindings>();
parent_bindings.values().for_each(|symbol_id| {
self.symbols.set_scope_id(*symbol_id, self.current_scope_id);
});
*self.scope.get_bindings_mut(self.current_scope_id) = parent_bindings;
}

So we should do the same thing in this plugin, but because CatchClause has no parameters, SemanticBuilder doesn't create scope for CatchClause. This cause we cannot find the BlockStatement scope_id.

This PR has changed to the correct logic. Just to wait #5008 solved

Copy link

graphite-app bot commented Aug 22, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

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

@Dunqing Dunqing changed the title fix(transformer-optional-catch-binding): the unused binding is not in the correct scope fix(transformer-optional-catch-binding): the unused binding is not in the correct scope Aug 22, 2024
@Dunqing Dunqing marked this pull request as ready for review August 22, 2024 08:05
@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Aug 22, 2024
Copy link

codspeed-hq bot commented Aug 22, 2024

CodSpeed Performance Report

Merging #5066 will not alter performance

Comparing 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope (47e69a8) with main (eba5033)

Summary

✅ 29 untouched benchmarks

@Dunqing Dunqing force-pushed the 08-22-refactor_transformer_es2019_move_all_entry_points_to_implementation_of_traverse_trait branch from 1b3f034 to 394f436 Compare August 22, 2024 08:20
@Dunqing Dunqing force-pushed the 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope branch from 858f510 to 6e172a7 Compare August 22, 2024 08:20
@overlookmotel
Copy link
Collaborator

Personally I'm not a fan of the way we move the bindings from catch param up into catch block. I understand it's to make identifying illegal declarations easier, but it's confusing/surprising.

We only have to check for illegal declarations in one place (syntax checker) whereas we have to deal with this non-standard scoping everywhere else. So on balance, in my view the downsides probably outweigh the upsides.

If we add scopes for function bodies (#5017), that'd require a mechanism in syntax checker to know when need to check parent scope for a clashing binding anyway, and could use the same mechanism for catch too.

@Dunqing Dunqing marked this pull request as draft August 22, 2024 08:35
@overlookmotel overlookmotel changed the base branch from 08-22-refactor_transformer_es2019_move_all_entry_points_to_implementation_of_traverse_trait to graphite-base/5066 August 22, 2024 08:35
@Dunqing
Copy link
Member Author

Dunqing commented Aug 22, 2024

Personally I'm not a fan of the way we move the bindings from catch param up into catch block. I understand it's to make identifying illegal declarations easier, but it's confusing/surprising.

We only have to check for illegal declarations in one place (syntax checker) whereas we have to deal with this non-standard scoping everywhere else. So on balance, in my view the downsides probably outweigh the upsides.

Yes, I agree with your point. I am not a fan of the way as well! But so far I haven't come up with a better way to address this

If we add scopes for function bodies (#5017), that'd require a mechanism in syntax checker to know when need to check parent scope for a clashing binding anyway, and could use the same mechanism for catch too.

Yes, I am looking forward we having a good mechanism to solve this. But we're stuck with the status quo for now 🥲.

@overlookmotel
Copy link
Collaborator

#5109 unblocks this.

@Dunqing Dunqing force-pushed the 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope branch from 6e172a7 to b214350 Compare August 23, 2024 09:14
@Dunqing Dunqing changed the base branch from graphite-base/5066 to main August 23, 2024 09:14
@Dunqing Dunqing marked this pull request as ready for review August 23, 2024 09:27
@Dunqing
Copy link
Member Author

Dunqing commented Aug 23, 2024

#5109 unblocks this.

All tests passed, Thanks!

Copy link
Collaborator

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

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

Great to see some of the scope tests being taken off the "fail" list. Everything I've been doing over past 2 days has been just adding more and more to that list. Now the tide is turning!

@github-actions github-actions bot added A-linter Area - Linter A-semantic Area - Semantic A-ast Area - AST A-ast-tools Area - AST tools labels Aug 23, 2024
@Dunqing Dunqing force-pushed the 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope branch from 4e47705 to ef0dedb Compare August 23, 2024 12:39
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Aug 23, 2024
Copy link

graphite-app bot commented Aug 23, 2024

Merge activity

  • Aug 23, 8:46 AM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Aug 23, 8:46 AM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Aug 23, 9:12 AM EDT: overlookmotel merged this pull request with the Graphite merge queue.

…in the correct scope (#5066)

Blocked by #5008

In the SemanticBuilder, we insert all CatchClause parameters in the BlockStatement scope, a child-scope of CatchClause

https://github.com/oxc-project/oxc/blob/858f510d59cb9ea29cca36c31fdf037b0d627f2c/crates/oxc_semantic/src/builder.rs#L709-L718

So we should do the same thing in this plugin, but because CatchClause has no parameters, SemanticBuilder doesn't create scope for `CatchClause`. This cause we cannot find the `BlockStatement` scope_id.

This PR has changed to the correct logic. Just to wait #5008 solved
@overlookmotel overlookmotel force-pushed the 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope branch from ef0dedb to 47e69a8 Compare August 23, 2024 13:02
@graphite-app graphite-app bot merged commit 47e69a8 into main Aug 23, 2024
25 checks passed
@graphite-app graphite-app bot deleted the 08-22-fix_transformer-optional-catch-binding_the_unused_binding_is_not_in_the_correct_scope branch August 23, 2024 13:12
@oxc-bot oxc-bot mentioned this pull request Sep 3, 2024
Boshen added a commit that referenced this pull request Sep 3, 2024
## [0.26.0] - 2024-09-03

- 1aa49af ast: [**BREAKING**] Remove
`JSXMemberExpressionObject::Identifier` variant (#5358) (Dunqing)

- 01cc2ce semantic: [**BREAKING**] Add `ScopeTree:get_child_ids` API
behind a runtime flag (#5403) (Boshen)

- b1d0075 napi/transform: [**BREAKING**] Align output API `sourceText`
-> `code` with babel (#5398) (Boshen)

- 32f7300 ast: [**BREAKING**] Add `JSXElementName::IdentifierReference`
and `JSXMemberExpressionObject::IdentifierReference` (#5223) (Dunqing)

- 23e8456 traverse: [**BREAKING**] `TraverseCtx::ancestor` with level 0
= equivalent to `parent` (#5294) (overlookmotel)

- 582ce9e traverse: [**BREAKING**] `TraverseCtx::ancestor` return
`Ancestor::None` if out of bounds (#5286) (overlookmotel)

- 234a24c ast: [**BREAKING**] Merge `UsingDeclaration` into
`VariableDeclaration` (#5270) (Kevin Deng 三咲智子)

- c100826 semantic: [**BREAKING**] Always create a scope for `for`
statements (#5110) (overlookmotel)

- d304d6f semantic: [**BREAKING**] Always create a scope for
`CatchClause` (#5109) (overlookmotel)

### Features

- 180b1a1 ast: Add `Function::name()` (#5361) (DonIsaac)
- 5505749 ast: Add `accessibility` field to `AccessorProperty` (#5290)
(Dunqing)
- 49cd5db ast,parser: Add `definite` flag to `AccessorProperty` node
(#5182) (DonIsaac)
- c2fa725 ast,parser: Parse `TSTypeAnnotations` on `AccessorProperty`
(#5179) (DonIsaac)
- 292d162 codegen: Print missing fields for `AccessorProperty` (#5291)
(Dunqing)
- 72740b3 isolated_declaration: Support sourcemap option (#5170)
(dalaoshu)
- f81e8a1 linter: Add `oxc/no-async-endpoint-handlers` (#5364)
(DonIsaac)
- 9c22ce9 linter: Add hyperlinks to diagnostic messages (#5318)
(DonIsaac)
- d22bd20 module_lexer: Distinguish for types-only imports and exports
(#5184) (Kevin Deng 三咲智子)
- 7dfd51a parser: Report class properties that are both definite and
optional (#5181) (DonIsaac)
- a563968 parser: Report errors on optional accessor properties (#5180)
(DonIsaac)
- 46b641b regular_expression: Validate max quantifier value (#5218)
(leaysgur)
- be4642f semantic: Transform checker check child scope IDs (#5410)
(overlookmotel)
- 6e969f9 semantic: Add `ScopeTree::delete_root_unresolved_reference`
(#5305) (overlookmotel)
- 1b20ceb span: Add `CompactStr::to_compact_string` method (#5385)
(Boshen)
- 5a137f0 span/source-type: Add SourceType factory methods (#5242)
(DonIsaac)
- f5e05db span/source-type: Impl `Display` and `Error` for
`UnknownExtension` (#5240) (DonIsaac)
- d04857b transformer: Support `Targets::from_query` method (#5336)
(Dunqing)
- 3d4a64c transformer: Make `Targets` public (#5335) (Dunqing)
- 0eb7602 transformer: Support `TransformOptions::from_preset_env` API
(#5323) (Dunqing)
- 08dc0ad transformer: Add `object-spread` plugin (#3133) (magic-akari)
- 01c0c3e transformer: Add remaining options to transformer options
(#5169) (Boshen)
- 056c667 transformer/arrow-functions: The output that uses `this`
inside blocks doesn't match Babel (#5188) (Dunqing)
- 0abfc50 transformer/typescript: Support `rewrite_import_extensions`
option (#5399) (Dunqing)

### Bug Fixes

- 8ebc23f ast: Serialize `TSParenthesizedType` with camelCase (#5199)
(Kevin Deng 三咲智子)
- 5c4c001 codegen: Print `export @decorator declare abstract class Foo`
correctly (#5303) (Boshen)
- 7b1546b codegen: Do not print comments when `--minify` (Boshen)
- ff7fa98 diagnostics: Improve "file is too long to fit on the screen"
(#5120) (Boshen)
- 8a17807 parser: Treat JSX element tags starting with `_` or `$` as
`IdentifierReference`s (#5343) (overlookmotel)
- d4c06ef parser: Revert "check for `@flow` with recoverable errors as
well" (#5297) (overlookmotel)
- e1d8b92 parser: Check for `@flow` with recoverable errors as well
(Boshen)
- e6fd52e parser: Change unterminated regex error to be non-recoverable
(#5285) (Boshen)
- 1686920 parser: Span for invalid regex flags (#5225) (leaysgur)
- cffce11 regular_expression: Prevent panic on too large number (#5282)
(leaysgur)
- 293413f semantic: Implicit return `UpdateExpression` in
`ArrowFunctionExpression` does not as read reference (#5161) (Dunqing)
- a6bb3b1 span/source-type: Consider `.cjs` and `.cts` files as
`ModuleKind::Script` (#5239) (DonIsaac)
- 35f03db transformer: `ArrowfunctionExpression`'s expression is true
but has more than one body statement (#5366) (Dunqing)
- 8d6b05c transformer: Class property with typescript value should not
be removed (#5298) (Boshen)
- 47e69a8 transformer-optional-catch-binding: The `unused` binding is
not in the correct scope (#5066) (Dunqing)
- 94ff94c transformer/arrow-functions: Reaches `unreachable` when
`<this.foo>` is inside an arrow function (#5356) (Dunqing)
- f8bb022 transformer/arrow-functions: Remove
`SymbolFlags::ArrowFunction` (#5190) (Dunqing)
- d9ba5ad transformer/arrow-functions: Correct scope for `_this` (#5189)
(Dunqing)
- 3acb3f6 transformer/react: Mismatch output caused by incorrect
transformation ordering (#5255) (Dunqing)
- 5754c89 transformer/typescript: Remove accessibility from
`AccessorProperty` (#5292) (Dunqing)
- d594818 traverse: `insert_scope_below` update child scopes records
(#5409) (overlookmotel)
- 25d6e20 traverse: Add missing visitors to `ChildScopeCollector`
(#5118) (overlookmotel)

### Performance

- 292f217 ast: Optimize `JSXIdentifier::is_reference` (#5344)
(overlookmotel)
- 12a7607 codegen: Inline `Codegen::print_list` (#5221) (overlookmotel)
- fb847bd codegen: Slightly faster `print_list` (#5192) (Boshen)
- a1523c6 transformer: Remove an allocation from arrow functions
transform (#5412) (overlookmotel)

### Documentation

- 8334bd4 transformer: Add documentation for `Targets::get_targets`
(#5337) (Dunqing)
- d51a954 transformer: Add documentation for arrow-functions plugin
(#5186) (Dunqing)

### Refactor

- c2d8c9e ast: Reduce allocations in
`AstBuilder::move_assignment_target` (#5367) (overlookmotel)
- 946c867 ast: Box `TSThisParameter` (#5325) (overlookmotel)
- 960e1d5 ast: Rename `IdentifierReference::new_with_reference_id`
(#5157) (overlookmotel)
- f63b568 ast: Remove `#[non_exhaustive]` attr from `AstBuilder` (#5130)
(overlookmotel)
- d4c3778 codegen: Rename vars (#5222) (overlookmotel)
- 543cad6 codegen: Remove some pub APIs (Boshen)
- cd63336 diagnostic: Change how diagnostic codes are rendered (#5317)
(DonIsaac)
- d236554 parser: Move `JSXIdentifier` conversion code into parser
(#5345) (overlookmotel)
- bc59dd2 parser: Improve example for `byte_search!` macro usage (#5234)
(overlookmotel)
- a3ddfdd parser: Improve lexer pointer maths (#5233) (overlookmotel)
- 3ae94b8 semantic: Change `build_module_record` to accept &Path instead
of PathBuf (Boshen)
- 05d25e2 semantic: Combine add scope methods (#5262) (overlookmotel)
- fdedc0f semantic: Transform checker: rename `SemanticData` to
`Scoping` (#5261) (overlookmotel)
- 1086109 semantic: Transform checker do not output spans in errors
(#5260) (overlookmotel)
- af5713e semantic: Transform checker continue checks if missing IDs
(#5259) (overlookmotel)
- 943454f semantic: Update transform checker for no conditional scopes
(#5252) (overlookmotel)
- 892a7fa semantic: Replace `ref` syntax (#5253) (overlookmotel)
- cbb4725 semantic: Add comment to transform checker (#5250)
(overlookmotel)
- a17cf33 semantic: Remove `ScopeTree::child_ids` (#5232) (Boshen)
- d5a4940 semantic: Rewrite handling of label statement errors (#5138)
(Boshen)
- 94f60e7 span/source-type: Make `SourceType` factories `const` (#5241)
(DonIsaac)
- 0de844d transformer: Remove unnecessary code from JSX transform
(#5339) (overlookmotel)
- 5136f01 transformer: Remove unnecessary type annotation (#5131)
(overlookmotel)
- 260c9d2 transformer/es2015: Move all entry points to implementation of
Traverse trait (#5187) (Dunqing)
- 1645115 transformer/object-reset-spread: Make plugin initialization
unconditional (#5319) (Dunqing)
- d2666fe transformer/object-rest-spread: Move plugin-relates files to
`object_rest_spread` mod (#5320) (Dunqing)
- 7e2a7af transformer/react: Remove `CalculateSignatureKey`
implementation from refresh (#5289) (Dunqing)
- b43a394 traverse: Correct code comments (#5293) (overlookmotel)
- d71f0ed traverse: Inline all passthrough methods (#5279)
(overlookmotel)
- 188ce07 traverse: Improve safety via type system (#5277)
(overlookmotel)
- 0f4a8b3 traverse: Add debug asserts for safety invariants (#5272)
(overlookmotel)
- 341e42a traverse: Make `Ancestor` an owned type (#5269)
(overlookmotel)
- eba5033 traverse: Codegen `ChildScopeCollector` (#5119)
(overlookmotel)
- f771d7c traverse: Remove unnecessary imports (#5116) (overlookmotel)
- c6590ae traverse: Move generated files into separate folder (#5115)
(overlookmotel)
- fc2e9ad traverse: Remove support for `#[scope(if(...))]` attr (#5114)
(overlookmotel)
- 1ba11a3 traverse: Refactor `ChildScopeCollector` (#5112)
(overlookmotel)
- 40e2f6e traverse: Remove unnecessary branch in `ChildScopeCollector`
(#5111) (overlookmotel)
- b39c0d6 wasm: Add `source_type` for parser, replace class options with
plain object (#5217) (Kevin Deng 三咲智子)

### Testing

- 7009177 parser: Fix incorrect flow error test (Boshen)
- be7b8c6 semantic: Add `JSXIdentifierReference`-related tests (#5224)
(Dunqing)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-ast Area - AST A-ast-tools Area - AST tools A-linter Area - Linter A-semantic Area - Semantic A-transformer Area - Transformer / Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants