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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
pub use self::{basic_impl::JsWriter, semicolon::omit_trailing_semi};
use super::*;
use swc_common::Span;
use swc_ecma_parser::JscTarget;

mod basic_impl;
mod semicolon;

/// TODO
pub type Symbol = Str;

/// Ecmascript writer.
///
/// Ported from `EmitWriteJs`.
pub trait WriteJs {
    /// Returns javascript target which should be used while generating code.
    ///
    /// This defaults to [JscTarget::Es2020] because it preserves input as much
    /// as possible.
    ///
    /// Implementor **should return same value** regardless how much time it is
    /// called.
    fn target(&self) -> JscTarget {
        JscTarget::Es2020
    }

    fn increase_indent(&mut self) -> Result;
    fn decrease_indent(&mut self) -> Result;

    /// This *may* write semicolon.
    fn write_semi(&mut self) -> Result;

    fn write_space(&mut self) -> Result;
    fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result;
    fn write_operator(&mut self, s: &str) -> Result;
    fn write_param(&mut self, s: &str) -> Result;
    fn write_property(&mut self, s: &str) -> Result;

    fn write_line(&mut self) -> Result;

    fn write_lit(&mut self, span: Span, s: &str) -> Result;
    fn write_comment(&mut self, span: Span, s: &str) -> Result;

    fn write_str_lit(&mut self, span: Span, s: &str) -> Result;
    fn write_str(&mut self, s: &str) -> Result;

    fn write_symbol(&mut self, span: Span, s: &str) -> Result;

    fn write_punct(&mut self, s: &'static str) -> Result;
}

impl<W> WriteJs for Box<W>
where
    W: ?Sized + WriteJs,
{
    fn increase_indent(&mut self) -> Result {
        (**self).increase_indent()
    }
    fn decrease_indent(&mut self) -> Result {
        (**self).decrease_indent()
    }

    fn write_semi(&mut self) -> Result {
        (**self).write_semi()
    }
    fn write_space(&mut self) -> Result {
        (**self).write_space()
    }
    fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result {
        (**self).write_keyword(span, s)
    }
    fn write_operator(&mut self, s: &str) -> Result {
        (**self).write_operator(s)
    }
    fn write_param(&mut self, s: &str) -> Result {
        (**self).write_param(s)
    }
    fn write_property(&mut self, s: &str) -> Result {
        (**self).write_property(s)
    }

    fn write_line(&mut self) -> Result {
        (**self).write_line()
    }

    fn write_lit(&mut self, span: Span, s: &str) -> Result {
        (**self).write_lit(span, s)
    }

    fn write_str_lit(&mut self, span: Span, s: &str) -> Result {
        (**self).write_str_lit(span, s)
    }
    fn write_str(&mut self, s: &str) -> Result {
        (**self).write_str(s)
    }

    fn write_symbol(&mut self, span: Span, s: &str) -> Result {
        (**self).write_symbol(span, s)
    }

    fn write_comment(&mut self, span: Span, s: &str) -> Result {
        (**self).write_comment(span, s)
    }

    fn write_punct(&mut self, s: &'static str) -> Result {
        (**self).write_punct(s)
    }

    fn target(&self) -> JscTarget {
        (**self).target()
    }
}