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

feat(traverse): record current block scope #8007

Merged
merged 1 commit into from
Dec 20, 2024

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Dec 19, 2024

Record "block" scope ID along with "hoist" scope ID in Traverse.

"Block" scope is the scope where a let statement would be inserted above current position in AST.

Block scope and current scope differ from each other inside classes. For example, if want to create a let temp var for foo() or bar() in example below, should use block scope not current scope. Current scope is the class itself, but the let statement will be inserted outside the class.

class C {
  [foo()]: bar();
}

All transforms which create let bindings should use block scope not current scope. We should add VarDeclarationsStore::insert_let method which uses block scope, to accompany insert_var (which uses hoist scope).

This is implemented in a rather hacky way, and we should improve it later. Notably, we're not considering for statements as block scopes because we currently have no way to insert let statements into them if they don't have a body block.

Copy link
Contributor Author

overlookmotel commented Dec 19, 2024


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.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Dec 19, 2024
Copy link

codspeed-hq bot commented Dec 19, 2024

CodSpeed Performance Report

Merging #8007 will degrade performances by 3.26%

Comparing 12-19-feat_traverse_record_current_block_scope (6b6444b) with main (059a5dd)

Summary

❌ 1 regressions
✅ 28 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main 12-19-feat_traverse_record_current_block_scope Change
semantic[cal.com.tsx] 27.1 ms 28 ms -3.26%

@overlookmotel overlookmotel force-pushed the 12-19-feat_traverse_record_current_block_scope branch from b9eab56 to 079096b Compare December 19, 2024 01:48
@overlookmotel overlookmotel marked this pull request as ready for review December 19, 2024 14:41
@overlookmotel overlookmotel force-pushed the 12-18-refactor_transformer_class-properties_do_not_pass_scopeid_into_insert_instance_inits_ branch from 57c6149 to 393fdd1 Compare December 19, 2024 20:50
@overlookmotel overlookmotel force-pushed the 12-19-feat_traverse_record_current_block_scope branch from 079096b to f63a36c Compare December 19, 2024 20:50
@overlookmotel overlookmotel force-pushed the 12-18-refactor_transformer_class-properties_do_not_pass_scopeid_into_insert_instance_inits_ branch from 393fdd1 to a7c1544 Compare December 19, 2024 22:23
@overlookmotel overlookmotel force-pushed the 12-19-feat_traverse_record_current_block_scope branch from f63a36c to 4f684f1 Compare December 19, 2024 22:23
@Dunqing
Copy link
Member

Dunqing commented Dec 20, 2024

I have taken a look at the whole stack. From the usages, it seems we can get current_block_scope by looking up the parent scope of current_scope_id because when you want to use current_block_scope, you are always at the class scope, so you only need to look up once and store it somewhere. That I understand correctly?

To clarify, I am not rejecting this API at all; on the contrary, I am happy to add various APIs in Traverse and TransformerCtx to solve annoying duplicating code. So If you find that can be fixed or refactored in other places by it, tell me!

@Dunqing Dunqing changed the base branch from 12-18-refactor_transformer_class-properties_do_not_pass_scopeid_into_insert_instance_inits_ to graphite-base/8007 December 20, 2024 03:12
@overlookmotel overlookmotel force-pushed the 12-19-feat_traverse_record_current_block_scope branch from 4f684f1 to 2e7b343 Compare December 20, 2024 03:13
@overlookmotel
Copy link
Contributor Author

overlookmotel commented Dec 20, 2024

it seems we can get current_block_scope by looking up the parent scope of current_scope_id because when you want to use current_block_scope, you are always at the class scope, so you only need to look up once and store it somewhere. That I understand correctly?

That's what I thought but actually no!

This gem was in one of Babel's tests:

class Outer extends class Inner {
  [foo()] = 123;
} {}

So when you're in the inner class, the parent scope is not where a let binding gets inserted - the parent scope is the scope of the outer class!

@Dunqing
Copy link
Member

Dunqing commented Dec 20, 2024

it seems we can get current_block_scope by looking up the parent scope of current_scope_id because when you want to use current_block_scope, you are always at the class scope, so you only need to look up once and store it somewhere. That I understand correctly?

That's what I thought but actually no!

This gem was in one of Babel's tests:

class Outer extends class Inner {
  [foo()] = 123;
} {}

So when you're in the inner class, the parent scope is not where a let binding gets inserted - the parent scope is the scope of the outer class!

Aha, I see the problem, fu** JavaScript!

@Dunqing Dunqing changed the base branch from graphite-base/8007 to main December 20, 2024 03:42
@Dunqing Dunqing force-pushed the 12-19-feat_traverse_record_current_block_scope branch from 2e7b343 to c1bc867 Compare December 20, 2024 03:42
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Dec 20, 2024
Copy link

graphite-app bot commented Dec 20, 2024

Merge activity

Record "block" scope ID along with "hoist" scope ID in `Traverse`.

"Block" scope is the scope where a `let` statement would be inserted above current position in AST.

Block scope and current scope differ from each other inside classes. For example, if want to create a `let` temp var for `foo()` or `bar()` in example below, should use block scope not current scope. Current scope is the class itself, but the `let` statement will be inserted *outside* the class.

```js
class C {
  [foo()]: bar();
}
```

All transforms which create `let` bindings should use block scope not current scope. We should add `VarDeclarationsStore::insert_let` method which uses block scope, to accompany `insert_var` (which uses hoist scope).

This is implemented in a rather hacky way, and we should improve it later. Notably, we're not considering `for` statements as block scopes because we currently have no way to insert `let` statements into them if they don't have a body block.
@Dunqing Dunqing force-pushed the 12-19-feat_traverse_record_current_block_scope branch from c1bc867 to 6b6444b Compare December 20, 2024 03:50
@graphite-app graphite-app bot merged commit 6b6444b into main Dec 20, 2024
26 checks passed
@graphite-app graphite-app bot deleted the 12-19-feat_traverse_record_current_block_scope branch December 20, 2024 03:59
Boshen added a commit that referenced this pull request Dec 21, 2024
## [0.43.0] - 2024-12-21

- de4c772 traverse: [**BREAKING**] Rename `Ancestor::is_via_*` methods
to `is_parent_of_*` (#8031) (overlookmotel)

- ed75e42 semantic: [**BREAKING**] Make SymbolTable fields `pub(crate)`
instead of `pub` (#7999) (Boshen)

### Features

- 75b775c allocator: `Vec<u8>::into_string` (#8017) (overlookmotel)
- 8547e02 ast: Implement `allocator_api2` for `Allocator` (#8043)
(Boshen)
- 63a95e4 ast: Add `AstBulder::move_property_key` (#7998)
(overlookmotel)
- 897a1a8 transformer/class-properties: Exit faster from super
replacement visitor (#8028) (overlookmotel)
- 3ea4109 transformer/class-properties: Transform super update
expressions within static prop initializer (#7997) (Dunqing)
- cc57db3 transformer/class-properties: Transform super assignment
expressions within static prop initializer (#7991) (Dunqing)
- 6b6444b traverse: Record current block scope (#8007) (overlookmotel)

### Bug Fixes

- 043252d transformer/class-properties: Replace `this` and class name in
static blocks (#8035) (overlookmotel)
- 273795d transformer/class-properties: Run other transforms on static
properties, static blocks, and computed keys (#7982) (overlookmotel)

### Performance

- c0dd3f8 ast: `move_expression` and `move_statement` produce dummy with
no span (#7995) (overlookmotel)
- 862838f codegen: Remove useless to_owned (#8014) (Dunqing)
- 2736657 semantic: Allocate `UnresolvedReferences` in allocator (#8046)
(Boshen)
- 2e8872c semantic: Allocate child scope in allocator (#8045) (Boshen)
- 414e828 semantic: Allocate symbol data in Allocator (#8012) (Boshen)
- 7aebed0 semantic: Allocate `Bindings` in allocator (#8021) (Boshen)
- 0f9308f transformer/react-refresh: Reduce allocations (#8018)
(overlookmotel)
- 0deb9e6 transformer/react-refresh: Reserve capacity in hook key string
(#8016) (overlookmotel)
- 7b70347 transformer/react-refresh: Avoid allocating string in each
hook call (#8013) (Dunqing)

### Documentation

- df5c341 ast: Improve docs for `AstBuilder::move_*` methods (#7994)
(overlookmotel)

### Refactor

- f1adf9f semantic: `ScopeTree::rename_binding` remove old binding first
(#8020) (overlookmotel)
- 02f968d semantic: Change `Bindings` to a plain `FxHashMap` (#8019)
(Boshen)
- e7476a1 semantic: Remove `serialize` (#8015) (Boshen)
- 1cf7b83 semantic: Simplify handling namespace stack (#7987) (Dunqing)
- 48cb52b semantic: Remove resetting `current_reference_flags` in visit
functions (#7986) (Dunqing)
- 3250a47 semantic: Remove unused current_symbol_flags (#7985) (Dunqing)
- efe96ec semantic: Use `Stack` for function stack node ids (#7984)
(Dunqing)
- ac097e9 transformer/class-properties: Rename file (#8036)
(overlookmotel)
- 059a5dd transformer/class-properties: Do not pass `ScopeId` into
`insert_instance_inits` (#8001) (overlookmotel)
- 0a38eea transformer/class-properties: Use `temp_var_name_base` to
generate temp var names for `super` transform (#8004) (overlookmotel)
- d1b7181 transformer/class-properties: Rename var (#8006)
(overlookmotel)
- 5a23d72 transformer/class-properties: Remove outdated comment (#8000)
(overlookmotel)
- b3a5f3e transformer/class-properties: Mark
`transform_assignment_expression_if_super_member_assignment_target` as
inline (#7993) (Dunqing)

### Testing

- bcb33c0 semantic: Add a test for catch parameters reference (#7988)
(Dunqing)

Co-authored-by: Boshen <[email protected]>
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 C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants