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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::borrow::Cow;
use crate::{
errors::parse_lexing_error, Expression, Keyword, ParseSettings, StatementPosition, TSXKeyword,
};
use super::{
ASTNode, ClassDeclaration, InterfaceDeclaration, ParseResult, Span, StatementFunction,
TSXToken, Token, TokenReader, TypeAlias, VariableStatement,
};
use visitable_derive::Visitable;
#[derive(Debug, PartialEq, Eq, Clone, Visitable)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
pub enum ExportStatement {
Variable { exported: Exportable, position: Span },
Default { expression: Box<Expression>, position: Span },
}
#[derive(Debug, PartialEq, Eq, Clone, Visitable)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
pub enum Exportable {
ClassDeclaration(ClassDeclaration<StatementPosition>),
FunctionDeclaration(StatementFunction),
VariableDeclaration(VariableStatement),
InterfaceDeclaration(InterfaceDeclaration),
TypeAlias(TypeAlias),
}
impl ASTNode for ExportStatement {
fn get_position(&self) -> Cow<Span> {
match self {
ExportStatement::Variable { position, .. }
| ExportStatement::Default { position, .. } => Cow::Borrowed(position),
}
}
fn from_reader(
reader: &mut impl TokenReader<TSXToken, Span>,
state: &mut crate::ParsingState,
settings: &ParseSettings,
) -> ParseResult<Self> {
Self::from_reader_2(reader, state, settings)
}
fn to_string_from_buffer<T: source_map::ToString>(
&self,
buf: &mut T,
settings: &crate::ToStringSettingsAndData,
depth: u8,
) {
match self {
ExportStatement::Variable { exported, .. } => match exported {
Exportable::ClassDeclaration(class_declaration) => {
class_declaration.to_string_from_buffer(buf, settings, depth);
}
Exportable::FunctionDeclaration(function_declaration) => {
function_declaration.to_string_from_buffer(buf, settings, depth);
}
Exportable::InterfaceDeclaration(interface_declaration) => {
interface_declaration.to_string_from_buffer(buf, settings, depth);
}
Exportable::VariableDeclaration(variable_dec_stmt) => {
buf.push_str("export ");
variable_dec_stmt.to_string_from_buffer(buf, settings, depth);
}
Exportable::TypeAlias(type_alias) => {
buf.push_str("export ");
type_alias.to_string_from_buffer(buf, settings, depth);
}
},
ExportStatement::Default { expression, position: _ } => {
buf.push_str("export default ");
expression.to_string_from_buffer(buf, settings, depth);
}
}
}
}
impl ExportStatement {
pub(crate) fn from_reader_2(
reader: &mut impl TokenReader<TSXToken, Span>,
state: &mut crate::ParsingState,
settings: &ParseSettings,
) -> ParseResult<Self> {
let start = reader.expect_next(TSXToken::Keyword(TSXKeyword::Export))?;
let is_default =
matches!(reader.peek(), Some(Token(TSXToken::Keyword(TSXKeyword::Default), _)));
if is_default {
reader.next();
let expression = Expression::from_reader(reader, state, settings)?;
let position = start.union(&expression.get_position());
Ok(Self::Default { expression: Box::new(expression), position })
} else {
match reader.peek().ok_or_else(parse_lexing_error)? {
Token(TSXToken::Keyword(TSXKeyword::Class), _) => {
let Token(_, pos) = reader.next().unwrap();
let keyword = Keyword::new(pos);
let class_declaration = ClassDeclaration::from_reader_sub_class_keyword(
reader, state, settings, keyword,
)?;
let position = start.union(&class_declaration.get_position());
Ok(Self::Variable {
exported: Exportable::ClassDeclaration(class_declaration),
position,
})
}
Token(TSXToken::Keyword(TSXKeyword::Function), _) => {
let function_declaration =
StatementFunction::from_reader(reader, state, settings)?;
let position = start.union(&function_declaration.get_position());
Ok(Self::Variable {
exported: Exportable::FunctionDeclaration(function_declaration),
position,
})
}
Token(
TSXToken::Keyword(TSXKeyword::Const) | TSXToken::Keyword(TSXKeyword::Let),
_,
) => {
let variable_declaration =
VariableStatement::from_reader(reader, state, settings)?;
let position = start.union(&variable_declaration.get_position());
Ok(Self::Variable {
exported: Exportable::VariableDeclaration(variable_declaration),
position,
})
}
Token(TSXToken::Keyword(TSXKeyword::Interface), _) => {
let interface_declaration =
InterfaceDeclaration::from_reader(reader, state, settings)?;
let position = start.union(&interface_declaration.get_position());
Ok(Self::Variable {
exported: Exportable::InterfaceDeclaration(interface_declaration),
position,
})
}
Token(TSXToken::Keyword(TSXKeyword::Type), _) => {
let type_alias = TypeAlias::from_reader(reader, state, settings)?;
let position = start.union(&type_alias.get_position());
Ok(Self::Variable { exported: Exportable::TypeAlias(type_alias), position })
}
Token(token, _) => {
unimplemented!("Token after export '{:?}'", token)
}
}
}
}
}