oxc/crates
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
..
oxc Release crates v0.12.5 2024-04-22 12:52:17 +08:00
oxc_allocator deps(allocator): disable serde dep by default (#3120) 2024-04-28 22:17:32 +08:00
oxc_ast feat(traverse): add scope flags to TraverseCtx (#3229) 2024-05-11 04:39:42 +00:00
oxc_ast_macros feat(transform): oxc_traverse crate (#3169) 2024-05-06 09:37:04 +08:00
oxc_cli feat(linter): add --format github for github check annotation (#3191) 2024-05-07 20:55:10 +08:00
oxc_codegen fix(parser): correctly parse cls.fn<C> = x (#3208) 2024-05-09 10:23:45 +08:00
oxc_diagnostics feat(linter): add --format github for github check annotation (#3191) 2024-05-07 20:55:10 +08:00
oxc_index chore(deps): update dependency rust to v1.78.0 (#3151) 2024-05-03 00:01:42 +08:00
oxc_js_regex
oxc_language_server chore: apply cargo autoinherit (#2826) 2024-03-26 23:57:50 +08:00
oxc_linter improvement(semantic/cfg): add explicit break block element. (#3223) 2024-05-10 22:22:30 +08:00
oxc_macros refactor(linter): reduce llvm lines generated by RuleEnum::read_json (#3207) 2024-05-08 21:53:06 +08:00
oxc_minifier refactor(syntax): move number related functions to number module (#3130) 2024-04-29 18:54:35 +08:00
oxc_module_lexer refactor(ast): squash nested enums (#3115) 2024-04-28 20:40:37 +08:00
oxc_parser fix(parser): correctly parse cls.fn<C> = x (#3208) 2024-05-09 10:23:45 +08:00
oxc_prettier fix(parser): correctly parse cls.fn<C> = x (#3208) 2024-05-09 10:23:45 +08:00
oxc_semantic improvement(semantic/cfg): add explicit break block element. (#3223) 2024-05-10 22:22:30 +08:00
oxc_sourcemap feat(sourcemap): add "rayon" feature (#3198) 2024-05-07 23:47:36 +08:00
oxc_span refactor(ast): add is_strict methods (#3227) 2024-05-11 04:39:36 +00:00
oxc_syntax feat(traverse): add scope flags to TraverseCtx (#3229) 2024-05-11 04:39:42 +00:00
oxc_transformer fix(transformer): correctly jsx-self inside arrow-function (#3224) 2024-05-11 00:48:18 +00:00
oxc_traverse feat(traverse): add scope flags to TraverseCtx (#3229) 2024-05-11 04:39:42 +00:00
oxc_wasm refactor(transformer): remove the requirement of Semantic (#3140) 2024-04-30 12:48:21 +08:00