1use is_macro::Is;
2use swc_atoms::Atom;
3use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP};
4
5use crate::{module_decl::ModuleDecl, stmt::Stmt};
6
7#[ast_node]
8#[derive(Eq, Hash, Is, EqIgnoreSpan)]
9#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
10#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
11pub enum Program {
12 #[tag("Module")]
13 Module(Module),
14 #[tag("Script")]
15 Script(Script),
16}
17
18impl Take for Program {
19 fn dummy() -> Self {
20 Program::Script(Script::default())
21 }
22}
23
24#[ast_node("Module")]
25#[derive(Eq, Hash, EqIgnoreSpan, Default)]
26#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
27pub struct Module {
28 pub span: Span,
29
30 pub body: Vec<ModuleItem>,
31
32 #[cfg_attr(feature = "serde-impl", serde(default, rename = "interpreter"))]
33 #[cfg_attr(
34 feature = "encoding-impl",
35 encoding(with = "cbor4ii::core::types::Maybe")
36 )]
37 pub shebang: Option<Atom>,
38}
39
40#[cfg(feature = "arbitrary")]
41#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
42impl<'a> arbitrary::Arbitrary<'a> for Module {
43 fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
44 let span = u.arbitrary()?;
45 let body = u.arbitrary()?;
46 Ok(Self {
47 span,
48 body,
49 shebang: None,
50 })
51 }
52}
53
54impl Take for Module {
55 fn dummy() -> Self {
56 Module {
57 span: DUMMY_SP,
58 body: Take::dummy(),
59 shebang: Take::dummy(),
60 }
61 }
62}
63
64#[ast_node("Script")]
65#[derive(Eq, Hash, EqIgnoreSpan, Default)]
66#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
67pub struct Script {
68 pub span: Span,
69
70 pub body: Vec<Stmt>,
71
72 #[cfg_attr(feature = "serde-impl", serde(default, rename = "interpreter"))]
73 #[cfg_attr(
74 feature = "encoding-impl",
75 encoding(with = "cbor4ii::core::types::Maybe")
76 )]
77 pub shebang: Option<Atom>,
78}
79
80#[cfg(feature = "arbitrary")]
81#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
82impl<'a> arbitrary::Arbitrary<'a> for Script {
83 fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
84 let span = u.arbitrary()?;
85 let body = u.arbitrary()?;
86 Ok(Self {
87 span,
88 body,
89 shebang: None,
90 })
91 }
92}
93
94impl Take for Script {
95 fn dummy() -> Self {
96 Script {
97 span: DUMMY_SP,
98 body: Take::dummy(),
99 shebang: Take::dummy(),
100 }
101 }
102}
103
104#[ast_node]
105#[derive(Eq, Hash, Is, EqIgnoreSpan)]
106#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
107#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
108pub enum ModuleItem {
109 #[tag("ImportDeclaration")]
110 #[tag("ExportDeclaration")]
111 #[tag("ExportNamedDeclaration")]
112 #[tag("ExportDefaultDeclaration")]
113 #[tag("ExportDefaultExpression")]
114 #[tag("ExportAllDeclaration")]
115 #[tag("TsImportEqualsDeclaration")]
116 #[tag("TsExportAssignment")]
117 #[tag("TsNamespaceExportDeclaration")]
118 ModuleDecl(ModuleDecl),
119 #[tag("*")]
120 Stmt(Stmt),
121}
122
123impl Take for ModuleItem {
124 fn dummy() -> Self {
125 Self::Stmt(Default::default())
126 }
127}