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
use std::sync::Arc;

use rnode::RNode;
use stc_ts_ast_rnode::RTsType;
use stc_ts_types::Type;
use swc_common::{errors::Handler, SourceMap, Span};
use swc_ecma_codegen::{text_writer::JsWriter, Emitter, Node};

#[derive(Clone)]
pub struct Debugger {
    pub cm: Arc<SourceMap>,
    pub handler: Arc<Handler>,
}

#[cfg(debug_assertions)]
impl Debugger {
    fn dump(&self, ty: &Type) -> String {
        let ty = RTsType::from(ty.clone());
        let ty = ty.into_orig();
        let mut buf = vec![];

        {
            let mut emitter = Emitter {
                cfg: swc_ecma_codegen::Config {
                    minify: false,
                    ..Default::default()
                },
                cm: self.cm.clone(),
                comments: None,
                wr: box JsWriter::new(self.cm.clone(), "\n", &mut buf, None),
            };

            ty.emit_with(&mut emitter).unwrap();
        }

        String::from_utf8_lossy(&buf).to_string()
    }

    pub fn dump_type(&self, span: Span, ty: &Type) {
        let ty_str = self.dump(ty);
        self.handler.struct_span_err(span, "Type").note(&ty_str).emit();
    }
}

#[cfg(not(debug_assertions))]
impl Debugger {
    pub fn dump_type(&self, _: Span, _: &Type) {}
}