1use crate::ast::{DeclKind, Expr, Param, Prop, Stmt, StmtKind, SwitchCase};
47use rustc_hash::{FxHashMap, FxHashSet};
48
49pub type SlotTable = FxHashMap<String, u16>;
51
52#[derive(Default)]
54pub struct Plan {
55 pub table: SlotTable,
57 pub numeric: FxHashSet<String>,
60 pub consts: FxHashSet<String>,
65}
66
67fn is_number_literal(e: &Expr) -> bool {
70 match e {
71 Expr::Number(_) => true,
72 Expr::Unary(crate::ast::UnOp::Neg, inner) => matches!(**inner, Expr::Number(_)),
73 _ => false,
74 }
75}
76
77pub fn plan(params: &[Param], body: &[Stmt], top_level: bool) -> Plan {
82 if !chunk_is_eligible(body) {
83 return Plan::default();
84 }
85 let mut escaping = FxHashSet::default();
88 for s in body {
89 collect_escaping_stmt(s, &mut escaping);
90 }
91 let mut p = Planner {
92 candidates: SlotTable::default(),
93 rejected: escaping,
94 numeric: FxHashSet::default(),
95 consts: FxHashSet::default(),
96 top_level,
97 next: 0,
98 lexical_log: Vec::new(),
99 closed: FxHashSet::default(),
100 };
101 for name in param_names(params) {
105 p.declare(&name);
106 }
107 for s in body {
108 p.walk_stmt(s);
109 }
110 for name in p.rejected {
111 p.candidates.remove(&name);
112 p.numeric.remove(&name);
113 p.consts.remove(&name);
114 }
115 Plan {
116 table: p.candidates,
117 numeric: p.numeric,
118 consts: p.consts,
119 }
120}
121
122pub fn param_names(params: &[Param]) -> Vec<String> {
125 params
126 .iter()
127 .filter(|p| !p.rest)
128 .filter_map(|p| match &p.pattern {
129 Expr::Ident(n) => Some(n.clone()),
130 _ => None,
131 })
132 .collect()
133}
134
135fn chunk_is_eligible(body: &[Stmt]) -> bool {
142 !mentions_eval_stmts(body) && body.iter().all(stmt_slot_safe)
143}
144
145fn mentions_eval_stmts(body: &[Stmt]) -> bool {
148 let mut names = FxHashSet::default();
149 for s in body {
150 collect_all_idents_stmt(s, &mut names);
151 }
152 names.contains("eval")
153}
154
155fn stmt_slot_safe(s: &Stmt) -> bool {
158 match &s.kind {
159 StmtKind::Try {
162 block,
163 handler,
164 finalizer,
165 } => {
166 block.iter().all(stmt_slot_safe)
167 && handler
168 .as_ref()
169 .map_or(true, |(_, b)| b.iter().all(stmt_slot_safe))
170 && finalizer
171 .as_ref()
172 .map_or(true, |b| b.iter().all(stmt_slot_safe))
173 }
174 StmtKind::Expr(e) | StmtKind::Throw(e) => expr_slot_safe(e),
175 StmtKind::Return(e) => e.as_ref().map_or(true, expr_slot_safe),
176 StmtKind::Decl { decls, .. } => decls
177 .iter()
178 .all(|d| d.init.as_ref().map_or(true, expr_slot_safe)),
179 StmtKind::Block(body) => body.iter().all(stmt_slot_safe),
180 StmtKind::If { test, cons, alt } => {
181 expr_slot_safe(test)
182 && stmt_slot_safe(cons)
183 && alt.as_deref().map_or(true, stmt_slot_safe)
184 }
185 StmtKind::While { test, body } | StmtKind::DoWhile { body, test } => {
186 expr_slot_safe(test) && stmt_slot_safe(body)
187 }
188 StmtKind::With { .. } => false,
191 StmtKind::For {
192 init,
193 test,
194 update,
195 body,
196 } => {
197 init.as_deref().map_or(true, stmt_slot_safe)
198 && test.as_ref().map_or(true, expr_slot_safe)
199 && update.as_ref().map_or(true, expr_slot_safe)
200 && stmt_slot_safe(body)
201 }
202 StmtKind::ForOf {
203 target,
204 iter,
205 body,
206 is_await,
207 ..
208 } => !*is_await && expr_slot_safe(target) && expr_slot_safe(iter) && stmt_slot_safe(body),
209 StmtKind::ForIn {
210 target,
211 object,
212 body,
213 ..
214 } => expr_slot_safe(target) && expr_slot_safe(object) && stmt_slot_safe(body),
215 StmtKind::Switch { disc, cases } => {
216 expr_slot_safe(disc)
217 && cases.iter().all(|c: &SwitchCase| {
218 c.test.as_ref().map_or(true, expr_slot_safe)
219 && c.body.iter().all(stmt_slot_safe)
220 })
221 }
222 StmtKind::Labeled { body, .. } => stmt_slot_safe(body),
223 StmtKind::Break(_) | StmtKind::Continue(_) | StmtKind::Empty => true,
224 StmtKind::Import(_) | StmtKind::Export(_) => false,
226 StmtKind::FuncDecl { .. } | StmtKind::ClassDecl(_) => true,
230 }
231}
232
233fn expr_slot_safe(e: &Expr) -> bool {
234 let all = |xs: &[Expr]| xs.iter().all(expr_slot_safe);
235 match e {
236 Expr::Yield { .. } | Expr::Await(..) => false,
237 Expr::Unary(_, inner) | Expr::Spread(inner) => expr_slot_safe(inner),
240 Expr::Template { exprs, .. } => all(exprs),
241 Expr::TaggedTemplate { tag, exprs, .. } => expr_slot_safe(tag) && all(exprs),
242 Expr::Array(items) | Expr::Sequence(items) => all(items),
243 Expr::Object(props) => props.iter().all(|p| match p {
244 Prop::KeyValue { key, value, .. } => expr_slot_safe(key) && expr_slot_safe(value),
245 Prop::Spread(x) => expr_slot_safe(x),
246 Prop::Accessor { .. } => true,
249 }),
250 Expr::Logical(_, l, r) | Expr::Binary(_, l, r) => expr_slot_safe(l) && expr_slot_safe(r),
251 Expr::Conditional { test, cons, alt } => {
252 expr_slot_safe(test) && expr_slot_safe(cons) && expr_slot_safe(alt)
253 }
254 Expr::Assign { target, value, .. } => expr_slot_safe(target) && expr_slot_safe(value),
255 Expr::Update { target, .. } => expr_slot_safe(target),
256 Expr::Call { func, args, .. }
257 | Expr::New {
258 callee: func, args, ..
259 } => expr_slot_safe(func) && all(args),
260 Expr::Member { object, .. } => expr_slot_safe(object),
261 Expr::Index { object, index, .. } => expr_slot_safe(object) && expr_slot_safe(index),
262 Expr::Function { .. } | Expr::Class(_) => true,
264 _ => true,
265 }
266}
267
268struct Planner {
269 candidates: SlotTable,
270 rejected: FxHashSet<String>,
271 numeric: FxHashSet<String>,
276 consts: FxHashSet<String>,
281 top_level: bool,
282 next: u16,
283 lexical_log: Vec<String>,
286 closed: FxHashSet<String>,
289}
290
291impl Planner {
292 fn declare(&mut self, name: &str) {
295 if self.rejected.contains(name) {
296 return;
297 }
298 if self.candidates.contains_key(name) {
299 self.reject(name);
300 return;
301 }
302 if self.next == u16::MAX {
305 self.reject(name);
306 return;
307 }
308 self.candidates.insert(name.to_string(), self.next);
309 self.next += 1;
310 }
311
312 fn reject(&mut self, name: &str) {
313 self.rejected.insert(name.to_string());
314 }
315
316 fn mention(&mut self, name: &str) {
319 if self.closed.contains(name) {
320 self.reject(name);
321 }
322 if !self.candidates.contains_key(name) {
323 self.reject(name);
324 }
325 }
326
327 fn declare_target(&mut self, target: &Expr, kind: Option<DeclKind>) {
329 self.declare_target_init(target, kind, None)
330 }
331
332 fn declare_target_init(&mut self, target: &Expr, kind: Option<DeclKind>, init: Option<&Expr>) {
335 let Expr::Ident(n) = target else {
336 self.reject_names_in(target);
339 return;
340 };
341 match kind {
342 Some(DeclKind::Var) if self.top_level => self.reject(n),
344 Some(k) => {
345 self.declare(n);
346 if k != DeclKind::Var {
347 self.lexical_log.push(n.clone());
348 }
349 if init.is_some_and(is_number_literal) {
350 self.numeric.insert(n.clone());
351 }
352 if k == DeclKind::Const {
353 self.consts.insert(n.clone());
354 }
355 }
356 None => self.mention(n),
359 }
360 }
361
362 fn reject_names_in(&mut self, e: &Expr) {
363 let mut names = Vec::new();
364 collect_idents(e, &mut names);
365 for n in names {
366 self.reject(&n);
367 }
368 }
369
370 fn walk_stmt(&mut self, s: &Stmt) {
373 let mark = self.lexical_log.len();
374 self.walk_stmt_inner(s);
375 if matches!(
376 s.kind,
377 StmtKind::Block(_)
378 | StmtKind::For { .. }
379 | StmtKind::ForOf { .. }
380 | StmtKind::ForIn { .. }
381 | StmtKind::Switch { .. }
382 ) {
383 self.closed.extend(self.lexical_log.drain(mark..));
384 }
385 }
386
387 fn walk_stmt_inner(&mut self, s: &Stmt) {
388 match &s.kind {
389 StmtKind::Decl { kind, decls } => {
390 for d in decls {
391 if let Some(init) = &d.init {
393 self.walk_expr(init);
394 }
395 match &d.init {
396 None => self.reject_names_in(&d.target),
400 Some(init) => self.declare_target_init(&d.target, Some(*kind), Some(init)),
401 }
402 }
403 }
404 StmtKind::Expr(e) | StmtKind::Throw(e) => self.walk_expr(e),
405 StmtKind::Return(e) => {
406 if let Some(e) = e {
407 self.walk_expr(e);
408 }
409 }
410 StmtKind::Block(body) => {
411 for s in body {
412 self.walk_stmt(s);
413 }
414 }
415 StmtKind::If { test, cons, alt } => {
416 self.walk_expr(test);
417 self.walk_stmt(cons);
418 if let Some(alt) = alt {
419 self.walk_stmt(alt);
420 }
421 }
422 StmtKind::While { test, body } => {
423 self.walk_expr(test);
424 self.walk_stmt(body);
425 }
426 StmtKind::With { object, body } => {
427 self.walk_expr(object);
428 self.walk_stmt(body);
429 }
430 StmtKind::DoWhile { body, test } => {
431 self.walk_stmt(body);
432 self.walk_expr(test);
433 }
434 StmtKind::For {
435 init,
436 test,
437 update,
438 body,
439 } => {
440 if let Some(init) = init {
441 self.walk_stmt(init);
442 }
443 if let Some(test) = test {
444 self.walk_expr(test);
445 }
446 self.walk_stmt(body);
447 if let Some(update) = update {
448 self.walk_expr(update);
449 }
450 }
451 StmtKind::ForOf {
452 decl_kind,
453 target,
454 iter,
455 body,
456 ..
457 } => {
458 self.walk_expr(iter);
459 self.declare_target(target, *decl_kind);
460 self.walk_stmt(body);
461 }
462 StmtKind::ForIn {
463 decl_kind,
464 target,
465 object,
466 body,
467 } => {
468 self.walk_expr(object);
469 self.declare_target(target, *decl_kind);
470 self.walk_stmt(body);
471 }
472 StmtKind::Switch { disc, cases } => {
473 self.walk_expr(disc);
474 for c in cases {
475 if let Some(t) = &c.test {
476 self.walk_expr(t);
477 }
478 for s in &c.body {
479 self.walk_stmt(s);
480 }
481 }
482 }
483 StmtKind::Labeled { body, .. } => self.walk_stmt(body),
484 StmtKind::Try { .. } | StmtKind::FuncDecl { .. } | StmtKind::ClassDecl(_) => {}
486 StmtKind::Break(_) | StmtKind::Continue(_) | StmtKind::Empty => {}
487 StmtKind::Import(_) | StmtKind::Export(_) => {}
488 }
489 }
490
491 fn walk_expr(&mut self, e: &Expr) {
492 match e {
493 Expr::Ident(n) => self.mention(n),
494 Expr::Assign { target, value, .. } => {
497 self.walk_expr(value);
498 match &**target {
499 Expr::Ident(n) => {
502 self.numeric.remove(n);
503 self.mention(n);
504 }
505 other => self.walk_expr(other),
506 }
507 }
508 Expr::Update { target, .. } => self.walk_expr(target),
509 Expr::Unary(crate::ast::UnOp::Delete, inner) => {
511 if let Expr::Ident(n) = &**inner {
512 self.reject(n);
513 } else {
514 self.walk_expr(inner);
515 }
516 }
517 Expr::Unary(_, inner) | Expr::Spread(inner) | Expr::Await(inner, _) => {
518 self.walk_expr(inner)
519 }
520 Expr::Yield { arg: Some(a), .. } => self.walk_expr(a),
521 Expr::Template { exprs, .. } => self.walk_all(exprs),
522 Expr::TaggedTemplate { tag, exprs, .. } => {
523 self.walk_expr(tag);
524 self.walk_all(exprs);
525 }
526 Expr::Array(items) | Expr::Sequence(items) => self.walk_all(items),
527 Expr::Object(props) => {
528 for p in props {
529 match p {
530 Prop::KeyValue { key, value, .. } => {
531 self.walk_expr(key);
532 self.walk_expr(value);
533 }
534 Prop::Spread(x) => self.walk_expr(x),
535 Prop::Accessor { key, func, .. } => {
536 self.walk_expr(key);
537 self.walk_expr(func);
538 }
539 }
540 }
541 }
542 Expr::Logical(_, l, r) | Expr::Binary(_, l, r) => {
543 self.walk_expr(l);
544 self.walk_expr(r);
545 }
546 Expr::Conditional { test, cons, alt } => {
547 self.walk_expr(test);
548 self.walk_expr(cons);
549 self.walk_expr(alt);
550 }
551 Expr::Call { func, args, .. }
552 | Expr::New {
553 callee: func, args, ..
554 } => {
555 self.walk_expr(func);
556 self.walk_all(args);
557 }
558 Expr::Member { object, .. } => self.walk_expr(object),
559 Expr::Index { object, index, .. } => {
560 self.walk_expr(object);
561 self.walk_expr(index);
562 }
563 Expr::Function { .. } | Expr::Class(_) => {}
566 _ => {}
567 }
568 }
569
570 fn walk_all(&mut self, items: &[Expr]) {
571 for e in items {
572 self.walk_expr(e);
573 }
574 }
575}
576
577fn collect_escaping_stmt(s: &Stmt, out: &mut FxHashSet<String>) {
586 match &s.kind {
587 StmtKind::FuncDecl { name, .. } => {
589 out.insert(name.clone());
590 collect_all_idents_stmt(s, out);
591 }
592 StmtKind::ClassDecl(c) => {
593 if let Some(n) = &c.name {
594 out.insert(n.clone());
595 }
596 collect_all_idents_stmt(s, out);
597 }
598 StmtKind::Try {
599 block,
600 handler,
601 finalizer,
602 } => {
603 for st in block {
604 collect_all_idents_stmt(st, out);
605 }
606 if let Some((bind, body)) = handler {
607 if let Some(p) = bind {
608 collect_all_idents_expr(p, out);
609 }
610 for st in body {
611 collect_all_idents_stmt(st, out);
612 }
613 }
614 if let Some(body) = finalizer {
615 for st in body {
616 collect_all_idents_stmt(st, out);
617 }
618 }
619 }
620 StmtKind::Expr(e) | StmtKind::Throw(e) => collect_escaping_expr(e, out),
621 StmtKind::Return(e) => {
622 if let Some(e) = e {
623 collect_escaping_expr(e, out);
624 }
625 }
626 StmtKind::Decl { decls, .. } => {
627 for d in decls {
628 if let Some(init) = &d.init {
629 collect_escaping_expr(init, out);
630 }
631 }
632 }
633 StmtKind::Block(body) => {
634 for st in body {
635 collect_escaping_stmt(st, out);
636 }
637 }
638 StmtKind::If { test, cons, alt } => {
639 collect_escaping_expr(test, out);
640 collect_escaping_stmt(cons, out);
641 if let Some(alt) = alt {
642 collect_escaping_stmt(alt, out);
643 }
644 }
645 StmtKind::While { test, body } | StmtKind::DoWhile { body, test } => {
646 collect_escaping_expr(test, out);
647 collect_escaping_stmt(body, out);
648 }
649 StmtKind::With { object, body } => {
650 collect_escaping_expr(object, out);
651 collect_escaping_stmt(body, out);
652 }
653 StmtKind::For {
654 init,
655 test,
656 update,
657 body,
658 } => {
659 if let Some(init) = init {
660 collect_escaping_stmt(init, out);
661 }
662 if let Some(test) = test {
663 collect_escaping_expr(test, out);
664 }
665 if let Some(update) = update {
666 collect_escaping_expr(update, out);
667 }
668 collect_escaping_stmt(body, out);
669 }
670 StmtKind::ForOf {
671 target, iter, body, ..
672 } => {
673 collect_escaping_expr(target, out);
674 collect_escaping_expr(iter, out);
675 collect_escaping_stmt(body, out);
676 }
677 StmtKind::ForIn {
678 target,
679 object,
680 body,
681 ..
682 } => {
683 collect_escaping_expr(target, out);
684 collect_escaping_expr(object, out);
685 collect_escaping_stmt(body, out);
686 }
687 StmtKind::Switch { disc, cases } => {
688 collect_escaping_expr(disc, out);
689 for c in cases {
690 if let Some(t) = &c.test {
691 collect_escaping_expr(t, out);
692 }
693 for st in &c.body {
694 collect_escaping_stmt(st, out);
695 }
696 }
697 }
698 StmtKind::Labeled { body, .. } => collect_escaping_stmt(body, out),
699 StmtKind::Break(_) | StmtKind::Continue(_) | StmtKind::Empty => {}
700 StmtKind::Import(_) | StmtKind::Export(_) => {}
701 }
702}
703
704fn collect_escaping_expr(e: &Expr, out: &mut FxHashSet<String>) {
705 match e {
706 Expr::Function { .. } | Expr::Class(_) => collect_all_idents_expr(e, out),
709 Expr::Ident(_) | Expr::Null | Expr::Undefined | Expr::True | Expr::False => {}
710 Expr::Unary(_, x)
711 | Expr::Spread(x)
712 | Expr::Await(x, _)
713 | Expr::Member { object: x, .. } => collect_escaping_expr(x, out),
714 Expr::Yield { arg: Some(a), .. } => collect_escaping_expr(a, out),
715 Expr::Template { exprs, .. } => exprs.iter().for_each(|x| collect_escaping_expr(x, out)),
716 Expr::TaggedTemplate { tag, exprs, .. } => {
717 collect_escaping_expr(tag, out);
718 exprs.iter().for_each(|x| collect_escaping_expr(x, out));
719 }
720 Expr::Array(items) | Expr::Sequence(items) => {
721 items.iter().for_each(|x| collect_escaping_expr(x, out))
722 }
723 Expr::Object(props) => {
724 for p in props {
725 match p {
726 Prop::KeyValue { key, value, .. } => {
727 collect_escaping_expr(key, out);
728 collect_escaping_expr(value, out);
729 }
730 Prop::Spread(x) => collect_escaping_expr(x, out),
731 Prop::Accessor { key, func, .. } => {
732 collect_escaping_expr(key, out);
733 collect_all_idents_expr(func, out);
734 }
735 }
736 }
737 }
738 Expr::Logical(_, l, r) | Expr::Binary(_, l, r) => {
739 collect_escaping_expr(l, out);
740 collect_escaping_expr(r, out);
741 }
742 Expr::Conditional { test, cons, alt } => {
743 collect_escaping_expr(test, out);
744 collect_escaping_expr(cons, out);
745 collect_escaping_expr(alt, out);
746 }
747 Expr::Assign { target, value, .. } => {
748 collect_escaping_expr(target, out);
749 collect_escaping_expr(value, out);
750 }
751 Expr::Update { target, .. } => collect_escaping_expr(target, out),
752 Expr::Call { func, args, .. }
753 | Expr::New {
754 callee: func, args, ..
755 } => {
756 collect_escaping_expr(func, out);
757 args.iter().for_each(|x| collect_escaping_expr(x, out));
758 }
759 Expr::Index { object, index, .. } => {
760 collect_escaping_expr(object, out);
761 collect_escaping_expr(index, out);
762 }
763 _ => {}
764 }
765}
766
767fn collect_all_idents_stmt(s: &Stmt, out: &mut FxHashSet<String>) {
769 match &s.kind {
770 StmtKind::FuncDecl { params, body, .. } => {
771 for p in params {
772 collect_all_idents_expr(&p.pattern, out);
773 if let Some(d) = &p.default {
774 collect_all_idents_expr(d, out);
775 }
776 }
777 body.iter().for_each(|st| collect_all_idents_stmt(st, out));
778 }
779 StmtKind::ClassDecl(c) => collect_class_idents(c, out),
780 StmtKind::Expr(e) | StmtKind::Throw(e) => collect_all_idents_expr(e, out),
781 StmtKind::Return(e) => {
782 if let Some(e) = e {
783 collect_all_idents_expr(e, out);
784 }
785 }
786 StmtKind::Decl { decls, .. } => {
787 for d in decls {
788 collect_all_idents_expr(&d.target, out);
789 if let Some(init) = &d.init {
790 collect_all_idents_expr(init, out);
791 }
792 }
793 }
794 StmtKind::Block(body) => body.iter().for_each(|st| collect_all_idents_stmt(st, out)),
795 StmtKind::If { test, cons, alt } => {
796 collect_all_idents_expr(test, out);
797 collect_all_idents_stmt(cons, out);
798 if let Some(alt) = alt {
799 collect_all_idents_stmt(alt, out);
800 }
801 }
802 StmtKind::While { test, body } | StmtKind::DoWhile { body, test } => {
803 collect_all_idents_expr(test, out);
804 collect_all_idents_stmt(body, out);
805 }
806 StmtKind::With { object, body } => {
807 collect_all_idents_expr(object, out);
808 collect_all_idents_stmt(body, out);
809 }
810 StmtKind::For {
811 init,
812 test,
813 update,
814 body,
815 } => {
816 if let Some(init) = init {
817 collect_all_idents_stmt(init, out);
818 }
819 if let Some(test) = test {
820 collect_all_idents_expr(test, out);
821 }
822 if let Some(update) = update {
823 collect_all_idents_expr(update, out);
824 }
825 collect_all_idents_stmt(body, out);
826 }
827 StmtKind::ForOf {
828 target, iter, body, ..
829 } => {
830 collect_all_idents_expr(target, out);
831 collect_all_idents_expr(iter, out);
832 collect_all_idents_stmt(body, out);
833 }
834 StmtKind::ForIn {
835 target,
836 object,
837 body,
838 ..
839 } => {
840 collect_all_idents_expr(target, out);
841 collect_all_idents_expr(object, out);
842 collect_all_idents_stmt(body, out);
843 }
844 StmtKind::Switch { disc, cases } => {
845 collect_all_idents_expr(disc, out);
846 for c in cases {
847 if let Some(t) = &c.test {
848 collect_all_idents_expr(t, out);
849 }
850 c.body
851 .iter()
852 .for_each(|st| collect_all_idents_stmt(st, out));
853 }
854 }
855 StmtKind::Labeled { body, .. } => collect_all_idents_stmt(body, out),
856 StmtKind::Try {
857 block,
858 handler,
859 finalizer,
860 } => {
861 block.iter().for_each(|st| collect_all_idents_stmt(st, out));
862 if let Some((bind, body)) = handler {
863 if let Some(p) = bind {
864 collect_all_idents_expr(p, out);
865 }
866 body.iter().for_each(|st| collect_all_idents_stmt(st, out));
867 }
868 if let Some(body) = finalizer {
869 body.iter().for_each(|st| collect_all_idents_stmt(st, out));
870 }
871 }
872 StmtKind::Break(_) | StmtKind::Continue(_) | StmtKind::Empty => {}
873 StmtKind::Import(_) | StmtKind::Export(_) => {}
874 }
875}
876
877fn collect_class_idents(c: &crate::ast::ClassNode, out: &mut FxHashSet<String>) {
878 if let Some(p) = &c.parent {
879 collect_all_idents_expr(p, out);
880 }
881 for m in &c.members {
882 collect_all_idents_expr(&m.key, out);
883 for p in &m.params {
884 collect_all_idents_expr(&p.pattern, out);
885 if let Some(d) = &p.default {
886 collect_all_idents_expr(d, out);
887 }
888 }
889 m.body
890 .iter()
891 .for_each(|st| collect_all_idents_stmt(st, out));
892 if let Some(init) = &m.field_init {
893 collect_all_idents_expr(init, out);
894 }
895 }
896}
897
898fn collect_all_idents_expr(e: &Expr, out: &mut FxHashSet<String>) {
899 let all = |xs: &[Expr], out: &mut FxHashSet<String>| {
900 xs.iter().for_each(|x| collect_all_idents_expr(x, out))
901 };
902 match e {
903 Expr::Ident(n) => {
904 out.insert(n.clone());
905 }
906 Expr::Class(c) => collect_class_idents(c, out),
907 Expr::Function { params, body, .. } => {
908 for p in params {
909 collect_all_idents_expr(&p.pattern, out);
910 if let Some(d) = &p.default {
911 collect_all_idents_expr(d, out);
912 }
913 }
914 match body {
915 crate::ast::FnBody::Block(stmts) => {
916 stmts.iter().for_each(|st| collect_all_idents_stmt(st, out))
917 }
918 crate::ast::FnBody::Expr(x) => collect_all_idents_expr(x, out),
919 }
920 }
921 Expr::Unary(_, x)
922 | Expr::Spread(x)
923 | Expr::Await(x, _)
924 | Expr::Member { object: x, .. } => collect_all_idents_expr(x, out),
925 Expr::Yield { arg: Some(a), .. } => collect_all_idents_expr(a, out),
926 Expr::Template { exprs, .. } => all(exprs, out),
927 Expr::TaggedTemplate { tag, exprs, .. } => {
928 collect_all_idents_expr(tag, out);
929 all(exprs, out);
930 }
931 Expr::Array(items) | Expr::Sequence(items) => all(items, out),
932 Expr::Object(props) => {
933 for p in props {
934 match p {
935 Prop::KeyValue { key, value, .. } => {
936 collect_all_idents_expr(key, out);
937 collect_all_idents_expr(value, out);
938 }
939 Prop::Spread(x) => collect_all_idents_expr(x, out),
940 Prop::Accessor { key, func, .. } => {
941 collect_all_idents_expr(key, out);
942 collect_all_idents_expr(func, out);
943 }
944 }
945 }
946 }
947 Expr::Logical(_, l, r) | Expr::Binary(_, l, r) => {
948 collect_all_idents_expr(l, out);
949 collect_all_idents_expr(r, out);
950 }
951 Expr::Conditional { test, cons, alt } => {
952 collect_all_idents_expr(test, out);
953 collect_all_idents_expr(cons, out);
954 collect_all_idents_expr(alt, out);
955 }
956 Expr::Assign { target, value, .. } => {
957 collect_all_idents_expr(target, out);
958 collect_all_idents_expr(value, out);
959 }
960 Expr::Update { target, .. } => collect_all_idents_expr(target, out),
961 Expr::Call { func, args, .. }
962 | Expr::New {
963 callee: func, args, ..
964 } => {
965 collect_all_idents_expr(func, out);
966 all(args, out);
967 }
968 Expr::Index { object, index, .. } => {
969 collect_all_idents_expr(object, out);
970 collect_all_idents_expr(index, out);
971 }
972 _ => {}
973 }
974}
975
976fn collect_idents(e: &Expr, out: &mut Vec<String>) {
978 match e {
979 Expr::Ident(n) => out.push(n.clone()),
980 Expr::Array(items) | Expr::Sequence(items) => {
981 for x in items {
982 collect_idents(x, out);
983 }
984 }
985 Expr::Object(props) => {
986 for p in props {
987 match p {
988 Prop::KeyValue { value, .. } => collect_idents(value, out),
989 Prop::Spread(x) => collect_idents(x, out),
990 Prop::Accessor { .. } => {}
991 }
992 }
993 }
994 Expr::Spread(inner) => collect_idents(inner, out),
995 Expr::Assign { target, .. } => collect_idents(target, out),
996 _ => {}
997 }
998}
999
1000pub fn mentions_arguments(body: &[Stmt]) -> bool {
1006 let mut names = FxHashSet::default();
1007 for s in body {
1008 collect_all_idents_stmt(s, &mut names);
1009 }
1010 names.contains("arguments") || names.contains("eval")
1011}