swc_ecma_transforms_optimization/
debug.rs1#![allow(unused)]
2
3use std::{fmt::Debug, mem::forget};
4
5use swc_ecma_ast::*;
6use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
7
8#[cfg_attr(not(debug_assertions), inline(always))]
10pub fn debug_assert_valid<N>(node: &N)
11where
12 N: VisitWith<AssertValid>,
13{
14 #[cfg(debug_assertions)]
15 node.visit_with(&mut AssertValid);
16}
17
18#[cfg(debug_assertions)]
19struct Ctx<'a> {
20 v: &'a dyn Debug,
21}
22
23#[cfg(debug_assertions)]
24impl Drop for Ctx<'_> {
25 fn drop(&mut self) {
26 eprintln!("Context: {:?}", self.v);
27 }
28}
29
30pub struct AssertValid;
31
32impl Visit for AssertValid {
33 noop_visit_type!(fail);
34
35 #[cfg(debug_assertions)]
36 fn visit_expr(&mut self, n: &Expr) {
37 let ctx = Ctx { v: n };
38 n.visit_children_with(self);
39 forget(ctx);
40 }
41
42 #[cfg(debug_assertions)]
43 fn visit_invalid(&mut self, _: &Invalid) {
44 panic!("Invalid node found");
45 }
46
47 #[cfg(debug_assertions)]
48 fn visit_number(&mut self, n: &Number) {
49 assert!(!n.value.is_nan(), "NaN should be an identifier");
50 }
51
52 #[cfg(debug_assertions)]
53 fn visit_setter_prop(&mut self, p: &SetterProp) {
54 p.body.visit_with(self);
55 }
56
57 #[cfg(debug_assertions)]
58 fn visit_stmt(&mut self, n: &Stmt) {
59 let ctx = Ctx { v: n };
60 n.visit_children_with(self);
61 forget(ctx);
62 }
63
64 #[cfg(debug_assertions)]
65 fn visit_tpl(&mut self, l: &Tpl) {
66 l.visit_children_with(self);
67
68 assert_eq!(l.exprs.len() + 1, l.quasis.len());
69 }
70
71 #[cfg(debug_assertions)]
72 fn visit_var_declarators(&mut self, v: &[VarDeclarator]) {
73 v.visit_children_with(self);
74
75 if v.is_empty() {
76 panic!("Found empty var declarators");
77 }
78 }
79
80 #[cfg(debug_assertions)]
81 fn visit_seq_expr(&mut self, v: &SeqExpr) {
82 v.visit_children_with(self);
83
84 }
93}