-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer/class-properties): create temp var for class where re…
…quired
- Loading branch information
1 parent
3547117
commit 29fed4e
Showing
8 changed files
with
555 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
crates/oxc_transformer/src/es2022/class_properties/class_bindings.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use oxc_syntax::symbol::{SymbolFlags, SymbolId}; | ||
use oxc_traverse::{BoundIdentifier, TraverseCtx}; | ||
|
||
#[derive(Default, Clone)] | ||
pub(super) struct ClassBindings<'a> { | ||
/// Binding for class name, if class has name | ||
pub name: Option<BoundIdentifier<'a>>, | ||
/// Temp var for class. | ||
/// e.g. `_Class` in `_Class = class {}, _Class.x = 1, _Class` | ||
pub temp: Option<BoundIdentifier<'a>>, | ||
} | ||
|
||
impl<'a> ClassBindings<'a> { | ||
/// Get `SymbolId` for name binding. | ||
pub fn name_symbol_id(&self) -> Option<SymbolId> { | ||
self.name.as_ref().map(|binding| binding.symbol_id) | ||
} | ||
|
||
/// Create a binding for temp var, if there isn't one already. | ||
pub fn get_or_init_temp_binding(&mut self, ctx: &mut TraverseCtx<'a>) -> &BoundIdentifier<'a> { | ||
self.temp.get_or_insert_with(|| { | ||
// Base temp binding name on class name, or "Class" if no name. | ||
// TODO(improve-on-babel): If class name var isn't mutated, no need for temp var for | ||
// class declaration. Can just use class binding. | ||
let name = self.name.as_ref().map_or("Class", |binding| binding.name.as_str()); | ||
ctx.generate_uid_in_current_scope(name, SymbolFlags::FunctionScopedVariable) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.