oxc/crates/oxc_syntax/src
overlookmotel 46c02aee61 feat(traverse): add scope flags to TraverseCtx (#3229)
Add scope flags to `TraverseCtx`.

Closes #3189.

`walk_*` functions build a stack of `ScopeFlags` as AST is traversed, and they can be queried from within visitors with `ctx.scope()`, `ctx.ancestor_scope()` and `ctx.find_scope()`.

The codegen which generates `walk_*` functions gets the info about which AST types have scopes, and how to check for strict mode from the `#[visited_node]` attrs on AST type definitions in `oxc_ast`.

A few notes:

Each scope inherits the strict mode flag from the level before it in the stack, so if you need to know "am I in strict mode context here?", `ctx.scope().is_strict_mode()` will tell you - no need to travel back up the stack to find out.

Scopes do *not* inherit any other flags from level before it. So `ctx.scope()` in a block nested in a function will return `ScopeFlags::empty()` not `ScopeFlags::Function`.

I had to add an extra flag `ScopeFlags::Method`. The reason for this is to deal with when a `Function` is actually a `MethodDefinition`, and to avoid creating 2 scopes in this case. The principle I'm trying to follow is to encode as little logic in the codegen as possible, as it's rather hidden away. Instead the codegen follows a standard logic for every node, guided by attributes which are visible next to the types in `oxc_ast`. This hopefully makes how `Traverse`'s visitors are generated less mysterious, and easier to change.

The case of `Function` within `MethodDefinition` is a weird one and would not be possible to implement without encoding a magic "special case" within the codegen without this extra `ScopeFlags::Method` variant. Its existence does not alter the operation of any other code in Oxc which uses `ScopeFlags`.

In my view `ScopeFlags` might benefit from a little bit of an overhaul anyway. I believe we could pack more information into the bits and make it more useful.
2024-05-11 04:39:42 +00:00
..
class.rs feat(semantic): add static property, ElementKind::Getter, ElementKind::Setter in ClassTable (#2445) 2024-02-20 13:07:48 +08:00
identifier.rs refactor(parser): make is_identifier methods consistent 2024-01-23 11:05:17 +08:00
keyword.rs feat(Codegen): Improve codegen (#2460) 2024-02-21 14:41:57 +08:00
lib.rs refactor(syntax): move number related functions to number module (#3130) 2024-04-29 18:54:35 +08:00
module_graph_visitor.rs feat(syntax): module graph visitor. (#3062) 2024-04-22 10:10:27 +08:00
module_record.rs refactor(syntax): use FxHashMap for ModuleRecord::request_modules (#3124) 2024-04-29 03:37:34 +00:00
node.rs fix(semantic): correctly resolve identifiers inside parameter initializers (#3046) 2024-04-21 23:38:31 +08:00
number.rs feat(syntax): add ToJsInt32 trait for f64 (#3132) 2024-04-29 21:13:04 +08:00
operator.rs chore: silence erroneous RA warnings for Tsify (#2731) 2024-03-15 12:42:12 +00:00
precedence.rs fix(codegen): add parenthesis in binary expression by precedence (#2067) 2024-01-17 23:01:42 +08:00
reference.rs feat: merge features serde and wasm to serialize (#2716) 2024-03-14 17:13:12 +08:00
scope.rs feat(traverse): add scope flags to TraverseCtx (#3229) 2024-05-11 04:39:42 +00:00
symbol.rs feat: merge features serde and wasm to serialize (#2716) 2024-03-14 17:13:12 +08:00
xml_entities.rs feat(transformer/jsx): escape xhtml in jsx attributes (#1088) 2023-10-29 15:16:50 +08:00