From ea44833867eb917c1ee2572a3a67f21bbb7f3d7f Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:46:38 +0200 Subject: [PATCH] Grow stack when visiting expressions --- crates/macros/Cargo.toml | 1 + crates/macros/src/lib.rs | 3 ++- packages/transformers/js/core/Cargo.toml | 1 + packages/transformers/js/core/src/collect.rs | 3 ++- packages/transformers/js/core/src/dependency_collector.rs | 3 ++- packages/transformers/js/core/src/env_replacer.rs | 3 ++- packages/transformers/js/core/src/fs.rs | 5 +++-- packages/transformers/js/core/src/global_replacer.rs | 3 ++- packages/transformers/js/core/src/hoist.rs | 3 ++- packages/transformers/js/core/src/modules.rs | 3 ++- packages/transformers/js/core/src/node_replacer.rs | 3 ++- packages/transformers/js/core/src/typeof_replacer.rs | 4 +++- 12 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/macros/Cargo.toml b/crates/macros/Cargo.toml index a099f21286b3..d67cdc7f3a33 100644 --- a/crates/macros/Cargo.toml +++ b/crates/macros/Cargo.toml @@ -16,6 +16,7 @@ swc_core = { version = "0.90.33", features = [ "ecma_ast", "ecma_parser", "ecma_visit", + "stacker" ] } serde = "1.0.123" napi-derive = { version = "2.12.5", optional = true } diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 7d5ea4f0b2a7..ee2713a355e4 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -1,6 +1,7 @@ use indexmap::IndexMap; use std::collections::{HashMap, HashSet}; use std::sync::Arc; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::common::util::take::Take; use swc_core::common::{SourceMap, Span, DUMMY_SP}; @@ -247,7 +248,7 @@ impl<'a> Fold for Macros<'a> { return Expr::Call(call); } - node.fold_children_with(self) + maybe_grow_default(|| node.fold_children_with(self)) } fn fold_var_decl(&mut self, mut node: VarDecl) -> VarDecl { diff --git a/packages/transformers/js/core/Cargo.toml b/packages/transformers/js/core/Cargo.toml index 8fce6284fa96..131a810ccda8 100644 --- a/packages/transformers/js/core/Cargo.toml +++ b/packages/transformers/js/core/Cargo.toml @@ -22,6 +22,7 @@ swc_core = { version = "0.90.33", features = [ "ecma_transforms_proposal", "ecma_transforms_react", "ecma_transforms_typescript", + "ecma_utils", "ecma_visit", "stacker" ] } diff --git a/packages/transformers/js/core/src/collect.rs b/packages/transformers/js/core/src/collect.rs index f26f09e240de..6d3ed5d87566 100644 --- a/packages/transformers/js/core/src/collect.rs +++ b/packages/transformers/js/core/src/collect.rs @@ -8,6 +8,7 @@ use std::collections::{HashMap, HashSet}; use swc_core::common::{sync::Lrc, Mark, Span, DUMMY_SP}; use swc_core::ecma::ast::*; use swc_core::ecma::atoms::{js_word, JsWord}; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{noop_visit_type, Visit, VisitWith}; macro_rules! collect_visit_fn { @@ -773,7 +774,7 @@ impl Visit for Collect { } } _ => { - node.visit_children_with(self); + maybe_grow_default(|| node.visit_children_with(self)); } } } diff --git a/packages/transformers/js/core/src/dependency_collector.rs b/packages/transformers/js/core/src/dependency_collector.rs index 329c09355d00..8c83343c2c27 100644 --- a/packages/transformers/js/core/src/dependency_collector.rs +++ b/packages/transformers/js/core/src/dependency_collector.rs @@ -2,6 +2,7 @@ use path_slash::PathBufExt; use std::collections::HashMap; use std::fmt; use std::path::Path; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use serde::{Deserialize, Serialize}; use swc_core::common::{Mark, SourceMap, Span, DUMMY_SP}; @@ -877,7 +878,7 @@ impl<'a> Fold for DependencyCollector<'a> { return ast::Expr::Ident(get_undefined_ident(self.unresolved_mark)); } - node.fold_children_with(self) + maybe_grow_default(|| node.fold_children_with(self)) } } diff --git a/packages/transformers/js/core/src/env_replacer.rs b/packages/transformers/js/core/src/env_replacer.rs index 1de14706b98c..53594fc0a203 100644 --- a/packages/transformers/js/core/src/env_replacer.rs +++ b/packages/transformers/js/core/src/env_replacer.rs @@ -4,6 +4,7 @@ use std::vec; use swc_core::common::{Mark, DUMMY_SP}; use swc_core::ecma::ast; use swc_core::ecma::atoms::JsWord; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith}; use crate::utils::*; @@ -163,7 +164,7 @@ impl<'a> Fold for EnvReplacer<'a> { } } - node.fold_children_with(self) + maybe_grow_default(|| node.fold_children_with(self)) } fn fold_var_decl(&mut self, node: VarDecl) -> VarDecl { diff --git a/packages/transformers/js/core/src/fs.rs b/packages/transformers/js/core/src/fs.rs index 5cb4ce1fe2b0..06ea1b3153d4 100644 --- a/packages/transformers/js/core/src/fs.rs +++ b/packages/transformers/js/core/src/fs.rs @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf}; use swc_core::common::{Mark, Span, DUMMY_SP}; use swc_core::ecma::ast::*; use swc_core::ecma::atoms::JsWord; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith, VisitWith}; pub fn inline_fs<'a>( @@ -61,7 +62,7 @@ impl<'a> Fold for InlineFS<'a> { } } - node.fold_children_with(self) + maybe_grow_default(|| node.fold_children_with(self)) } } @@ -218,7 +219,7 @@ struct Evaluator<'a> { impl<'a> Fold for Evaluator<'a> { fn fold_expr(&mut self, node: Expr) -> Expr { - let node = node.fold_children_with(self); + let node = maybe_grow_default(|| node.fold_children_with(self)); match &node { Expr::Ident(ident) => match ident.sym.to_string().as_str() { diff --git a/packages/transformers/js/core/src/global_replacer.rs b/packages/transformers/js/core/src/global_replacer.rs index 0af4d05a07c3..b73bcef16f05 100644 --- a/packages/transformers/js/core/src/global_replacer.rs +++ b/packages/transformers/js/core/src/global_replacer.rs @@ -1,6 +1,7 @@ use indexmap::IndexMap; use path_slash::PathBufExt; use std::path::Path; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::common::{Mark, SourceMap, SyntaxContext, DUMMY_SP}; use swc_core::ecma::ast::{self, ComputedPropName}; @@ -43,7 +44,7 @@ impl<'a> Fold for GlobalReplacer<'a> { }) } } - _ => node.fold_children_with(self), + _ => maybe_grow_default(|| node.fold_children_with(self)), }; if let Ident(id) = &mut node { diff --git a/packages/transformers/js/core/src/hoist.rs b/packages/transformers/js/core/src/hoist.rs index a951f20f7f2b..6f2ebd88b5d5 100644 --- a/packages/transformers/js/core/src/hoist.rs +++ b/packages/transformers/js/core/src/hoist.rs @@ -11,6 +11,7 @@ use std::hash::Hasher; use swc_core::common::{Mark, Span, SyntaxContext, DUMMY_SP}; use swc_core::ecma::ast::*; use swc_core::ecma::atoms::{js_word, JsWord}; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith}; use crate::id; @@ -740,7 +741,7 @@ impl<'a> Fold for Hoist<'a> { _ => {} } - node.fold_children_with(self) + maybe_grow_default(|| node.fold_children_with(self)) } fn fold_seq_expr(&mut self, node: SeqExpr) -> SeqExpr { diff --git a/packages/transformers/js/core/src/modules.rs b/packages/transformers/js/core/src/modules.rs index ef73b8d9eb61..87154d3484be 100644 --- a/packages/transformers/js/core/src/modules.rs +++ b/packages/transformers/js/core/src/modules.rs @@ -6,6 +6,7 @@ use swc_core::common::{Mark, Span, SyntaxContext, DUMMY_SP}; use swc_core::ecma::ast::*; use swc_core::ecma::atoms::{js_word, JsWord}; use swc_core::ecma::preset_env::{Feature, Versions}; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith}; use crate::fold_member_expr_skip_prop; @@ -613,7 +614,7 @@ impl Fold for ESMFold { node } } - _ => node.fold_children_with(self), + _ => maybe_grow_default(|| node.fold_children_with(self)), } } diff --git a/packages/transformers/js/core/src/node_replacer.rs b/packages/transformers/js/core/src/node_replacer.rs index 58b654674407..ade3285b044e 100644 --- a/packages/transformers/js/core/src/node_replacer.rs +++ b/packages/transformers/js/core/src/node_replacer.rs @@ -5,6 +5,7 @@ use std::path::Path; use swc_core::common::{Mark, SourceMap, SyntaxContext, DUMMY_SP}; use swc_core::ecma::ast; use swc_core::ecma::atoms::JsWord; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith}; use crate::dependency_collector::{DependencyDescriptor, DependencyKind}; @@ -44,7 +45,7 @@ impl<'a> Fold for NodeReplacer<'a> { }) } } - _ => node.fold_children_with(self), + _ => maybe_grow_default(|| node.fold_children_with(self)), }; if let Ident(id) = &mut node { diff --git a/packages/transformers/js/core/src/typeof_replacer.rs b/packages/transformers/js/core/src/typeof_replacer.rs index 3b7ed37fd7e6..9ccd19b1b0ff 100644 --- a/packages/transformers/js/core/src/typeof_replacer.rs +++ b/packages/transformers/js/core/src/typeof_replacer.rs @@ -2,6 +2,7 @@ use crate::utils::is_unresolved; use swc_core::common::Mark; use swc_core::ecma::ast::{Expr, Lit, Str, UnaryOp}; use swc_core::ecma::atoms::js_word; +use swc_core::ecma::utils::stack_size::maybe_grow_default; use swc_core::ecma::visit::{Fold, FoldWith}; pub struct TypeofReplacer { @@ -40,6 +41,7 @@ impl Fold for TypeofReplacer { } } } - node.fold_children_with(self) + + maybe_grow_default(|| node.fold_children_with(self)) } }