Skip to content

Commit

Permalink
invert if check for iife to reduce indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Aug 27, 2024
1 parent 2e74ed6 commit 1349c3f
Showing 1 changed file with 72 additions and 71 deletions.
143 changes: 72 additions & 71 deletions turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,83 +1242,84 @@ impl VisitAstPath for Analyzer<'_> {
}
}

if self.check_iife(n, ast_path) {
return;
}

// special behavior of IIFEs
if !self.check_iife(n, ast_path) {
{
{
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::CallExpr(n, CallExprField::Callee));
n.callee.visit_with_ast_path(self, &mut ast_path);
}

let args = n
.args
.iter()
.enumerate()
.map(|(i, arg)| {
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::CallExpr(n, CallExprField::Callee));
n.callee.visit_with_ast_path(self, &mut ast_path);
}
let args = n
.args
.iter()
.enumerate()
.map(|(i, arg)| {
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::CallExpr(n, CallExprField::Args(i)));
if arg.spread.is_none() {
let value = self.eval_context.eval(&arg.expr);

let block_path = match &*arg.expr {
Expr::Fn(FnExpr { .. }) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Fn));
path.push(AstParentKind::FnExpr(FnExprField::Function));
path.push(AstParentKind::Function(FunctionField::Body));
Some(path)
}
Expr::Arrow(ArrowExpr {
body: box BlockStmtOrExpr::BlockStmt(_),
..
}) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Arrow));
path.push(AstParentKind::ArrowExpr(ArrowExprField::Body));
path.push(AstParentKind::BlockStmtOrExpr(
BlockStmtOrExprField::BlockStmt,
));
Some(path)
}
Expr::Arrow(ArrowExpr {
body: box BlockStmtOrExpr::Expr(_),
..
}) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Arrow));
path.push(AstParentKind::ArrowExpr(ArrowExprField::Body));
path.push(AstParentKind::BlockStmtOrExpr(
BlockStmtOrExprField::Expr,
));
Some(path)
}
_ => None,
};
if let Some(path) = block_path {
let old_effects = take(&mut self.effects);
arg.visit_with_ast_path(self, &mut ast_path);
let effects = replace(&mut self.effects, old_effects);
EffectArg::Closure(
value,
EffectsBlock {
effects,
range: AstPathRange::Exact(path),
},
)
} else {
arg.visit_with_ast_path(self, &mut ast_path);
EffectArg::Value(value)
ast_path.with_guard(AstParentNodeRef::CallExpr(n, CallExprField::Args(i)));
if arg.spread.is_none() {
let value = self.eval_context.eval(&arg.expr);

let block_path = match &*arg.expr {
Expr::Fn(FnExpr { .. }) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Fn));
path.push(AstParentKind::FnExpr(FnExprField::Function));
path.push(AstParentKind::Function(FunctionField::Body));
Some(path)
}
Expr::Arrow(ArrowExpr {
body: box BlockStmtOrExpr::BlockStmt(_),
..
}) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Arrow));
path.push(AstParentKind::ArrowExpr(ArrowExprField::Body));
path.push(AstParentKind::BlockStmtOrExpr(
BlockStmtOrExprField::BlockStmt,
));
Some(path)
}
Expr::Arrow(ArrowExpr {
body: box BlockStmtOrExpr::Expr(_),
..
}) => {
let mut path = as_parent_path(&ast_path);
path.push(AstParentKind::ExprOrSpread(ExprOrSpreadField::Expr));
path.push(AstParentKind::Expr(ExprField::Arrow));
path.push(AstParentKind::ArrowExpr(ArrowExprField::Body));
path.push(AstParentKind::BlockStmtOrExpr(BlockStmtOrExprField::Expr));
Some(path)
}
_ => None,
};
if let Some(path) = block_path {
let old_effects = take(&mut self.effects);
arg.visit_with_ast_path(self, &mut ast_path);
let effects = replace(&mut self.effects, old_effects);
EffectArg::Closure(
value,
EffectsBlock {
effects,
range: AstPathRange::Exact(path),
},
)
} else {
arg.visit_with_ast_path(self, &mut ast_path);
EffectArg::Spread
EffectArg::Value(value)
}
})
.collect();
self.check_call_expr_for_effects(n, args, ast_path);
}
} else {
arg.visit_with_ast_path(self, &mut ast_path);
EffectArg::Spread
}
})
.collect();
self.check_call_expr_for_effects(n, args, ast_path);
}

fn visit_new_expr<'ast: 'r, 'r>(
Expand Down

0 comments on commit 1349c3f

Please sign in to comment.