Skip to content

Commit

Permalink
refactor(transformer/arrow-functions): use IndexMap for super get…
Browse files Browse the repository at this point in the history
…ter/setters (#7317)

Generate getter/setter declarations in same order as Babel by using `IndexMap` instead of `HashMap` to store `super` getter/setter method details.
  • Loading branch information
overlookmotel committed Nov 17, 2024
1 parent 061207d commit 3555cf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 12 additions & 5 deletions crates/oxc_transformer/src/common/arrow_function_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@
//! The Implementation based on
//! <https://github.com/babel/babel/blob/d20b314c14533ab86351ecf6ca6b7296b66a57b3/packages/babel-traverse/src/path/conversion.ts#L170-L247>

use rustc_hash::{FxHashMap, FxHashSet};
use std::hash::BuildHasherDefault;

use indexmap::IndexMap;
use rustc_hash::{FxHashSet, FxHasher};

use oxc_allocator::{Box as ArenaBox, String as ArenaString, Vec as ArenaVec};
use oxc_ast::{ast::*, NONE};
Expand All @@ -102,6 +105,8 @@ use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};

use crate::EnvOptions;

type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;

/// Mode for arrow function conversion
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArrowFunctionConverterMode {
Expand Down Expand Up @@ -129,7 +134,9 @@ pub struct ArrowFunctionConverter<'a> {
this_var_stack: SparseStack<BoundIdentifier<'a>>,
arguments_var_stack: SparseStack<BoundIdentifier<'a>>,
renamed_arguments_symbol_ids: FxHashSet<SymbolId>,
super_methods: Option<FxHashMap<Atom<'a>, SuperMethodInfo<'a>>>,
// TODO(improve-on-babel): `FxHashMap` would suffice here. Iteration order is not important.
// Only using `FxIndexMap` for predictable iteration order to match Babel's output.
super_methods: Option<FxIndexMap<Atom<'a>, SuperMethodInfo<'a>>>,
}

impl<'a> ArrowFunctionConverter<'a> {
Expand Down Expand Up @@ -187,7 +194,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
self.arguments_var_stack.push(None);
if self.is_async_only() && func.r#async && Self::is_class_method_like_ancestor(ctx.parent())
{
self.super_methods = Some(FxHashMap::default());
self.super_methods = Some(FxIndexMap::default());
}
}

Expand Down Expand Up @@ -1163,7 +1170,7 @@ impl<'a> ArrowFunctionConverter<'a> {
let is_class_method_like = Self::is_class_method_like_ancestor(ctx.parent());
let declarations_count = usize::from(arguments.is_some())
+ if is_class_method_like {
self.super_methods.as_ref().map_or(0, FxHashMap::len)
self.super_methods.as_ref().map_or(0, FxIndexMap::len)
} else {
0
}
Expand All @@ -1183,7 +1190,7 @@ impl<'a> ArrowFunctionConverter<'a> {
// `_superprop_getSomething = () => super.getSomething;`
if is_class_method_like {
if let Some(super_methods) = self.super_methods.as_mut() {
declarations.extend(super_methods.drain().map(|(_, super_method)| {
declarations.extend(super_methods.drain(..).map(|(_, super_method)| {
Self::generate_super_method(target_scope_id, super_method, ctx)
}));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const Obj = {
value: 0,
method() {
var _superprop_getObject = () => super.object,
_superprop_set = (_prop, _value) => super[_prop] = _value,
_superprop_setValue = _value2 => super.value = _value2;
var _superprop_setValue = (_value) => (super.value = _value),
_superprop_set = (_prop, _value2) => (super[_prop] = _value2),
_superprop_getObject = () => super.object;
return babelHelpers.asyncToGenerator(function* () {
_superprop_setValue(true);
() => {
_superprop_set('value', true);
_superprop_set("value", true);
_superprop_getObject().value = true;
};
})();
}
};
};

0 comments on commit 3555cf9

Please sign in to comment.