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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{module_decl::ModuleDecl, stmt::Stmt};
use swc_atoms::JsWord;
use swc_common::{ast_node, Span};

#[ast_node]
pub enum Program {
    #[tag("Module")]
    Module(Module),
    #[tag("Script")]
    Script(Script),
}

#[ast_node("Module")]
pub struct Module {
    pub span: Span,

    pub body: Vec<ModuleItem>,

    #[serde(default, rename = "interpreter")]
    pub shebang: Option<JsWord>,
}

#[ast_node("Script")]
pub struct Script {
    pub span: Span,

    pub body: Vec<Stmt>,

    #[serde(default, rename = "interpreter")]
    pub shebang: Option<JsWord>,
}

#[ast_node]
pub enum ModuleItem {
    #[tag("ImportDeclaration")]
    #[tag("ExportDeclaration")]
    #[tag("ExportNamedDeclaration")]
    #[tag("ExportDefaultDeclaration")]
    #[tag("ExportDefaultExpression")]
    #[tag("ExportAllDeclaration")]
    #[tag("TsImportEqualsDeclaration")]
    #[tag("TsExportAssignment")]
    #[tag("TsNamespaceExportDeclaration")]
    ModuleDecl(ModuleDecl),
    #[tag("*")]
    Stmt(Stmt),
}

// This test ensures that FnDecl can be deserialized if type field is present
test_de!(
    asi,
    ModuleItem,
    r#"{
      "type": "FunctionDeclaration",
      "identifier": {
        "type": "Identifier",
        "span": {
          "start": 9,
          "end": 10,
          "ctxt": 0
        },
        "value": "x",
        "optional": false
      },
      "declare": false,
      "params": [],
      "span": {
        "start": 0,
        "end": 34,
        "ctxt": 0
      },
      "body": {
        "type": "BlockStatement",
        "span": {
          "start": 13,
          "end": 34,
          "ctxt": 0
        },
        "stmts": [
          {
            "type": "VariableDeclaration",
            "span": {
              "start": 17,
              "end": 22,
              "ctxt": 0
            },
            "kind": "let",
            "declare": false,
            "declarations": [
              {
                "type": "VariableDeclarator",
                "span": {
                  "start": 21,
                  "end": 22,
                  "ctxt": 0
                },
                "id": {
                  "type": "Identifier",
                  "span": {
                    "start": 21,
                    "end": 22,
                    "ctxt": 0
                  },
                  "value": "x",
                  "optional": false
                },
                "definite": false
              }
            ]
          },
          {
            "type": "JSXElement",
            "span": {
              "start": 25,
              "end": 32,
              "ctxt": 0
            },
            "opening": {
              "type": "JSXOpeningElement",
              "name": {
                "type": "Identifier",
                "span": {
                  "start": 26,
                  "end": 29,
                  "ctxt": 0
                },
                "value": "div",
                "optional": false
              },
              "span": {
                "start": 26,
                "end": 32,
                "ctxt": 0
              },
              "selfClosing": true
            },
            "children": [],
            "closing": null
          }
        ]
      },
      "generator": false,
      "async": false
    }"#
);