Suggested changes to arrow functions transform #8385
Labels
A-transformer
Area - Transformer / Transpiler
C-cleanup
Category - technical debt or refactoring. Solution not expected to change behavior
Continuing from conversation on #8335 (comment):
I said:
@Dunqing replied:
2 suggestions to improve on #8335 based on that conversation:
Defensive coding
As I said above, you cannot rely on the node not being changed by other transforms between
enter_*
andexit_*
visitors.In this case,
self.is_async_only() && Self::is_class_method_like_ancestor(ctx.parent())
is guaranteed to be the same inenter_function
andexit_function
. BUT it's possible thatfunc.r#async
isfalse
inenter_function
but has been changed totrue
by another transform beforeexit_function
. That would cause a panic because stack goes out of sync with traversal.So I suggest instead of:
oxc/crates/oxc_transformer/src/common/arrow_function_converter.rs
Lines 216 to 242 in 335065d
Then in
exit_function
, do the same.i.e. Don't rely on
func.r#async
being the same inenter_function
andexit_function
to ensure stack stays in sync with traversal.This does have a small perf cost, because you push to the stack for every function now. But that cost is very small because pushing
None
to aSparseStack
is only a 1-byte write, andSparseStack::push
is very optimized. So personally I think the small perf cost is worth it for the greater safety.However... of course it's a problem if some other transform has converted a non-async class method to an async one between
enter_function
andexit_function
because our transform won't work correctly. But we can catch that inexit_function
with a debug assertion:Don't pass
super_methods
toinsert_variable_statement_at_the_top_of_statements
I think you can achieve your goal of not having to pass
super_methods
toinsert_variable_statement_at_the_top_of_statements
by changing:oxc/crates/oxc_transformer/src/common/arrow_function_converter.rs
Lines 1110 to 1114 in 335065d
to:
Then call
self.super_methods_stack.pop()
inexit_function
after callinginsert_variable_statement_at_the_top_of_statements
. At that point the popped value will always beNone
, and you can discard it.You can also do the same with
this_var
andarguments_var
.The text was updated successfully, but these errors were encountered: