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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use crate::expr::{Expr, Literal, PropertyKey, PropertyKind, PropertyValue, Property, ObjectExpr};
use crate::pat::{Pat, ObjectPatPart};
use crate::{Class, Function, Identifier};

/// The declaration of a variable, function, class, import or export
#[derive(PartialEq, Debug, Clone)]
pub enum Decl {
    /// A variable declaration
    /// ```js
    /// var x, b;
    /// let y, a = 0;
    /// const q = 100
    /// ```
    Variable(VariableKind, Vec<VariableDecl>),
    /// A function declaration
    /// ```js
    /// function thing() {}
    /// ```
    Function(Function),
    /// A class declaration
    /// ```js
    /// class Thing {}
    /// ```
    Class(Class),
    /// An import declaration
    /// ```js
    /// import * as moment from 'moment';
    /// import Thing, {thing} from 'stuff';
    /// ```
    Import(Box<ModImport>),
    /// An export declaration
    /// ```js
    /// export function thing() {}
    /// ```
    Export(Box<ModExport>),
}

impl Decl {
    pub fn variable(kind: VariableKind, decls: Vec<VariableDecl>) -> Self {
        Decl::Variable(kind, decls)
    }
    pub fn function(f: Function) -> Self {
        Decl::Function(f)
    }
    pub fn class(class: Class) -> Self {
        Decl::Class(class)
    }
    pub fn import(imp: ModImport) -> Self {
        Decl::Import(Box::new(imp))
    }
    pub fn export(exp: ModExport) -> Self {
        Decl::Export(Box::new(exp))
    }
}

/// The identifier and optional value of a variable declaration
#[derive(PartialEq, Debug, Clone)]
pub struct VariableDecl {
    pub id: Pat,
    pub init: Option<Expr>,
}

impl VariableDecl {
    pub fn new(id: Pat, init: Option<Expr>) -> Self {
        VariableDecl {
            id,
            init,
        }
    }

    pub fn uninitialized(name: &str) -> Self {
        Self {
            id: Pat::Identifier(String::from(name)),
            init: None,
        }
    }

    pub fn with_value(name: &str, value: Expr) -> Self {
        Self {
            id: Pat::Identifier(String::from(name)),
            init: Some(value),
        }
    }

    pub fn destructed(names: &[&str], value: ObjectExpr) -> Self {
        let id = Pat::Object(
            names
                .iter()
                .map(|name| {
                    ObjectPatPart::Assignment(Property {
                        key: PropertyKey::Expr(Expr::ident(&name.to_string())),
                        value: PropertyValue::None,
                        kind: PropertyKind::Init,
                        method: false,
                        short_hand: true,
                        computed: false,
                        is_static: false,
                    })
                })
                .collect(),
        );
        Self {
            id,
            init: Some(Expr::Object(value)),
        }
    }

    pub fn destructed_with_rest(names: &[&str], rest: &str, value: ObjectExpr) -> Self {
        let mut props: Vec<ObjectPatPart> = names
            .iter()
            .map(|name| {
                ObjectPatPart::Assignment(Property {
                    key: PropertyKey::Expr(Expr::Ident(String::from(*name))),
                    value: PropertyValue::None,
                    kind: PropertyKind::Init,
                    computed: false,
                    method: false,
                    short_hand: true,
                    is_static: false,
                })
            })
            .collect();
        props.push(ObjectPatPart::Rest(Box::new(Pat::RestElement(
            Box::new(Pat::Identifier(String::from(rest))),
        ))));
        let id = Pat::Object(props);
        let init = Some(Expr::Object(value));
        Self { id, init }
    }
}

/// The kind of variable being defined (`var`/`let`/`const`)
#[derive(PartialEq, Clone, Debug, Copy)]
pub enum VariableKind {
    Var,
    Let,
    Const,
}

/// A module declaration, This would only be available
/// in an ES Mod, it would be either an import or
/// export at the top level
#[derive(PartialEq, Debug, Clone)]
pub enum ModDecl {
    Import(ModImport),
    Export(ModExport),
}

impl ModDecl {
    pub fn import(inner: ModImport) -> Self {
        ModDecl::Import(inner)
    }
    pub fn export(inner: ModExport) -> Self {
        ModDecl::Export(inner)
    }
}

/// A declaration that imports exported
/// members of another module
///
/// ```js
/// import {Thing} from './stuff.js';
/// ```
#[derive(PartialEq, Debug, Clone)]
pub struct ModImport {
    pub specifiers: Vec<ImportSpecifier>,
    pub source: Literal,
}

impl ModImport {
    pub fn new(specs: Vec<ImportSpecifier>, source: String) -> Self {
        Self {
            specifiers: specs,
            source: Literal::String(source),
        }
    }
}

/// The name of the thing being imported
#[derive(PartialEq, Debug, Clone)]
pub enum ImportSpecifier {
    /// A specifier in curly braces, this might
    /// have a local alias
    ///
    /// ```js
    /// import {Thing} from './stuff.js';
    /// import {People as Persons} from './places.js';
    /// ```
    Normal(Identifier, Option<Identifier>),
    /// A specifier that has been exported with the
    /// default keyword, this should not be wrapped in
    /// curly braces.
    /// ```js
    /// import DefaultThing from './stuff/js';
    /// ```
    Default(Identifier),
    /// Import all exported members from a module
    /// in a namespace.
    ///
    /// ```js
    /// import * as Moment from 'moment.js';
    /// ```
    Namespace(Identifier),
}

impl ImportSpecifier {
    pub fn normal(ident: Identifier, module: Option<Identifier>) -> Self {
        ImportSpecifier::Normal(ident, module)
    }
    pub fn default(ident: Identifier) -> Self {
        ImportSpecifier::Default(ident)
    }
    pub fn namespace(ident: Identifier) -> Self {
        ImportSpecifier::Namespace(ident)
    }
}

/// Something exported from this module
#[derive(PartialEq, Debug, Clone)]
pub enum ModExport {
    /// ```js
    /// export default function() {};
    /// //or
    /// export default 1;
    /// ```
    Default(DefaultExportDecl),
    ///```js
    /// export {foo} from 'mod';
    /// //or
    /// export {foo as bar} from 'mod';
    /// //or
    /// export var foo = 1;
    /// //or
    /// export function bar() {
    /// }
    /// ```
    Named(NamedExportDecl),
    /// ```js
    /// export * from 'mod';
    /// ```
    All(Literal),
}

impl ModExport {
    pub fn default(default: DefaultExportDecl) -> Self {
        ModExport::Default(default)
    }

    pub fn named(named: NamedExportDecl) -> Self {
        ModExport::Named(named)
    }
    pub fn all(lit: Literal) -> Self {
        ModExport::All(lit)
    }
}

/// An export that has a name
/// ```js
/// export function thing() {}
/// export {stuff} from 'place';
#[derive(PartialEq, Debug, Clone)]
pub enum NamedExportDecl {
    Decl(Decl),
    Specifier(Vec<ExportSpecifier>, Option<Literal>),
}

impl NamedExportDecl {
    pub fn decl(decl: Decl) -> Self {
        NamedExportDecl::Decl(decl)
    }
    pub fn specifier(exports: Vec<ExportSpecifier>, path: Option<Literal>) -> Self {
        NamedExportDecl::Specifier(exports, path)
    }
}

/// A default export
/// ```js
/// export default class Thing {}
/// ```
#[derive(PartialEq, Debug, Clone)]
pub enum DefaultExportDecl {
    Decl(Decl),
    Expr(Expr),
}

impl DefaultExportDecl {
    pub fn decl(decl: Decl) -> Self {
        DefaultExportDecl::Decl(decl)
    }
    pub fn expr(expr: Expr) -> Self {
        DefaultExportDecl::Expr(expr)
    }
}

/// The name of the thing being exported
/// this might include an alias
/// ```js
/// //no-alias
/// export {Thing} from 'place';
/// //aliased
/// export {Stuff as NewThing} from 'place'
/// ```
#[derive(PartialEq, Debug, Clone)]
pub struct ExportSpecifier {
    pub local: Identifier,
    pub exported: Option<Identifier>,
}

impl ExportSpecifier {
    pub fn new(local: Identifier, exported: Option<Identifier>) -> ExportSpecifier {
        ExportSpecifier {
            local,
            exported,
        }
    }
}