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