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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use crate::error::SyntaxError;
use crate::error::SyntaxErrorType;
use crate::loc::Loc;
use crate::num::JsNumber;
use crate::operator::OperatorName;
use ahash::AHashMap;
use core::fmt::Debug;
use serde::Serialize;
use serde::Serializer;
use std::any::Any;
use std::any::TypeId;
use std::fmt;
use std::fmt::Formatter;

#[derive(Default)]
pub struct NodeAssocData {
  map: AHashMap<TypeId, Box<dyn Any>>,
}

impl NodeAssocData {
  pub fn get<T: Any>(&self) -> Option<&T> {
    let t = TypeId::of::<T>();
    self.map.get(&t).map(|v| v.downcast_ref().unwrap())
  }

  pub fn set<T: Any>(&mut self, v: T) {
    let t = TypeId::of::<T>();
    self.map.insert(t, Box::from(v));
  }
}

#[cfg(test)]
mod tests {
  use crate::ast::NodeAssocData;

  #[test]
  fn test_node_assoc_data() {
    struct MyType(u32);
    let mut assoc = NodeAssocData::default();
    assoc.set(MyType(32));
    let v = assoc.get::<MyType>().unwrap();
    assert_eq!(v.0, 32);
  }
}

pub struct Node {
  // A location is not a SourceRange; consider that after some transformations, it's possible to create entirely new nodes that don't exist at all in the source code. Also, sometimes we cannot be bothered to set a location, or can only provide an approximate/best-effort location.
  pub loc: Loc,
  pub stx: Box<Syntax>,
  pub assoc: NodeAssocData,
}

impl Node {
  pub fn new(loc: Loc, stx: Syntax) -> Node {
    Node {
      loc,
      stx: Box::new(stx),
      assoc: NodeAssocData::default(),
    }
  }

  /// Create an error at this node's location.
  pub fn error(&self, typ: SyntaxErrorType) -> SyntaxError {
    self.loc.error(typ, None)
  }

  pub fn as_ident(&self) -> &str {
    match self.stx.as_ref() {
      Syntax::IdentifierExpr { name } => name.as_str(),
      _ => unreachable!(),
    }
  }
}

impl Debug for Node {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    self.stx.fmt(f)
  }
}

impl Serialize for Node {
  fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
    self.stx.serialize(serializer)
  }
}

// These are for readability only, and do not increase type safety or define different structures.
type Declaration = Node;
type Expression = Node;
type Pattern = Node;
type Statement = Node;

#[derive(Eq, PartialEq, Clone, Copy, Debug, Serialize)]
pub enum VarDeclMode {
  Const,
  Let,
  Var,
}

#[derive(Debug, Serialize)]
pub enum ArrayElement {
  Single(Expression),
  Rest(Expression),
  Empty,
}

// WARNING: This enum must exist, and the two variants cannot be merged by representing Direct with an IdentifierExpr, as it's not a usage of a variable!
#[derive(Debug, Serialize)]
pub enum ClassOrObjectMemberKey {
  // Identifier, keyword, string, or number.
  // NOTE: This isn't used by ObjectMemberType::Shorthand.
  Direct(String),
  Computed(Expression),
}

#[derive(Debug, Serialize)]
pub enum ClassOrObjectMemberValue {
  Getter {
    function: Node, // Always Function. `params` is empty.
  },
  Method {
    function: Node, // Always Function.
  },
  Property {
    // Must be Some if object, as shorthands are covered by ObjectMemberType::Shorthand (and are initialised).
    initializer: Option<Expression>,
  },
  Setter {
    function: Node, // Always Function. `params` contains exactly one ParamDecl with no `default_value` or `rest`.
  },
}

#[derive(Debug, Serialize)]
pub enum ObjectMemberType {
  Valued {
    key: ClassOrObjectMemberKey,
    value: ClassOrObjectMemberValue,
  },
  Shorthand {
    identifier: Node, // Always IdentifierExpr.
  },
  Rest {
    value: Expression,
  },
}

#[derive(Debug, Serialize)]
pub struct ArrayPatternElement {
  pub target: Pattern,
  pub default_value: Option<Expression>,
}

#[derive(Debug, Serialize)]
pub struct ExportName {
  // For simplicity, we always set both fields; for shorthands, both nodes are identical.
  pub target: String,
  // IdentifierPattern.
  pub alias: Pattern,
}

#[derive(Debug, Serialize)]
pub enum ExportNames {
  // `import * as name`
  // `export * from "module"`
  // `export * as name from "module"`
  // IdentifierPattern.
  All(Option<Pattern>),
  // `import {a as b, c, default as e}`
  // `export {a as default, b as c, d}`
  // `export {default, a as b, c} from "module"`
  // `default` is still a name, so we don't use an enum.
  Specific(Vec<ExportName>),
}

#[derive(Debug, Serialize)]
pub struct VariableDeclarator {
  pub pattern: Pattern,
  pub initializer: Option<Expression>,
}

#[derive(Debug, Serialize)]
pub enum ForInit {
  None,
  Expression(Expression),
  Declaration(Declaration),
}

#[derive(Debug, Serialize)]
pub enum LiteralTemplatePart {
  Substitution(Expression),
  String(String),
}

#[derive(Debug, Serialize)]
#[serde(tag = "$t")]
pub enum Syntax {
  // Patterns.
  IdentifierPattern {
    name: String,
  },
  // `const fn = (a: any, b: any, ...{ length, ...c }: any[]) => void 0` is allowed.
  ArrayPattern {
    // Unnamed elements can exist.
    elements: Vec<Option<ArrayPatternElement>>,
    rest: Option<Pattern>,
  },
  // For an object pattern, `...` must be followed by an identifier.
  // `const fn = ({ a: { b = c } = d, ...e }: any) => void 0` is possible.
  ObjectPattern {
    // List of ObjectPatternProperty nodes.
    properties: Vec<Node>,
    // This must be IdentifierPattern, anything else is illegal.
    rest: Option<Pattern>,
  },
  // Not really a pattern but functions similarly; separated out for easy replacement when minifying.
  ClassOrFunctionName {
    name: String,
  },

  // Functions.
  // This common type exists for better downstream usage, as one type is easier to match on and wrangle than many different types (ArrowFunctionExpr, ClassMember::Method, FunctionDecl, etc.).
  Function {
    arrow: bool,
    async_: bool,
    generator: bool,
    parameters: Vec<Declaration>, // Always ParamDecl.
    body: Node, // Could be Expression if arrow function. Otherwise, it's FunctionBody.
  },
  // A function body is different from a block statement, as the scopes are different. This doesn't mean much at the parser level, but helps with downstream usages.
  FunctionBody {
    body: Vec<Statement>,
  },

  // Declarations.
  ClassDecl {
    export: bool,
    export_default: bool,
    name: Option<Node>, // Always ClassOrFunctionName. Name can only be omitted in a default export, although a default export class can still have a name.
    extends: Option<Expression>,
    members: Vec<Node>, // Always ClassMember.
  },
  FunctionDecl {
    export: bool,
    export_default: bool,
    name: Option<Node>, // Always ClassOrFunctionName. Name can only be omitted in a default export, although a default export function can still have a name.
    function: Node,     // Always Function.
  },
  ParamDecl {
    rest: bool,
    pattern: Pattern,
    default_value: Option<Expression>,
  },
  VarDecl {
    export: bool,
    mode: VarDeclMode,
    declarators: Vec<VariableDeclarator>,
  },

  // Expressions.
  ArrowFunctionExpr {
    parenthesised: bool,
    function: Node, // Always Function.
  },
  BinaryExpr {
    parenthesised: bool,
    operator: OperatorName,
    left: Expression,
    right: Expression,
  },
  CallExpr {
    optional_chaining: bool,
    parenthesised: bool,
    callee: Expression,
    arguments: Vec<Node>,
  },
  ClassExpr {
    parenthesised: bool,
    name: Option<Node>,
    extends: Option<Expression>,
    members: Vec<Node>, // Always ClassMember.
  },
  ConditionalExpr {
    parenthesised: bool,
    test: Expression,
    consequent: Expression,
    alternate: Expression,
  },
  ComputedMemberExpr {
    optional_chaining: bool,
    object: Expression,
    member: Expression,
  },
  FunctionExpr {
    parenthesised: bool,
    name: Option<Node>,
    function: Node,
  },
  IdentifierExpr {
    name: String,
  },
  ImportExpr {
    module: Expression,
  },
  ImportMeta {},
  JsxAttribute {
    name: Expression,          // JsxName
    value: Option<Expression>, // JsxExpressionContainer or JsxText
  },
  JsxElement {
    // When an element name starts with a lowercase ASCII character, it's a built-in component like '<div>' or '<span>'.
    // For easier differentiation, we use IdentifierExpr for user-defined components as they are references to symbols and built-in components are not.
    // https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized
    name: Option<Expression>, // IdentifierExpr or JsxName or JsxMemberExpression; None if fragment
    attributes: Vec<Expression>, // JsxAttribute or JsxSpreadAttribute; always empty if fragment
    children: Vec<Expression>, // JsxElement or JsxExpressionContainer or JsxText
  },
  JsxExpressionContainer {
    value: Expression,
  },
  JsxMemberExpression {
    // This is a separate property to indicate it's required and for easier pattern matching.
    base: Node, // Always IdentifierExpr
    path: Vec<String>,
  },
  JsxName {
    namespace: Option<String>,
    name: String,
  },
  JsxSpreadAttribute {
    value: Expression,
  },
  JsxText {
    value: String,
  },
  LiteralArrayExpr {
    elements: Vec<ArrayElement>,
  },
  LiteralBigIntExpr {
    value: String,
  },
  LiteralBooleanExpr {
    value: bool,
  },
  LiteralNull {},
  LiteralNumberExpr {
    value: JsNumber,
  },
  LiteralObjectExpr {
    // List of ObjectMember nodes.
    members: Vec<Node>,
  },
  LiteralRegexExpr {
    value: String, // Including delimiter slashes and any flags.
  },
  LiteralStringExpr {
    value: String,
  },
  LiteralTemplateExpr {
    parts: Vec<LiteralTemplatePart>,
  },
  // Dedicated special type to easily distinguish when analysing and minifying. Also done to avoid using IdentifierExpr as right, which is incorrect (not a variable usage).
  MemberExpr {
    parenthesised: bool,
    optional_chaining: bool,
    left: Expression,
    right: String,
  },
  SuperExpr {},
  ThisExpr {},
  TaggedTemplateExpr {
    function: Expression,
    parts: Vec<LiteralTemplatePart>,
  },
  UnaryExpr {
    parenthesised: bool,
    operator: OperatorName,
    argument: Expression,
  },
  UnaryPostfixExpr {
    parenthesised: bool,
    operator: OperatorName,
    argument: Expression,
  },

  // Statements.
  BlockStmt {
    body: Vec<Statement>,
  },
  BreakStmt {
    label: Option<String>,
  },
  ContinueStmt {
    label: Option<String>,
  },
  DebuggerStmt {},
  DoWhileStmt {
    condition: Expression,
    body: Statement,
  },
  EmptyStmt {},
  ExportDefaultExprStmt {
    expression: Expression,
  },
  ExportListStmt {
    names: ExportNames,
    from: Option<String>,
  },
  ExpressionStmt {
    expression: Expression,
  },
  IfStmt {
    test: Expression,
    consequent: Statement,
    alternate: Option<Statement>,
  },
  ImportStmt {
    // IdentifierPattern.
    default: Option<Pattern>,
    names: Option<ExportNames>,
    module: String,
  },
  ForStmt {
    init: ForInit,
    condition: Option<Expression>,
    post: Option<Expression>,
    body: Statement, // Won't be BlockStmt, but ForBody instead. (However, could be another type of statement.)
  },
  ForInStmt {
    // for-in and for-of statements can have `x`/`[x]`/`{x:a}`/etc. on the lhs or `var x`/`var [x]`/etc. on the lhs. But for the latter, while it's technically a Decl, it's always a VarDecl with exactly one declaration that has no initialiser. If you strip down VarDecl to this, it's basically just a VarDeclMode and a Pattern. Therefore, we can represent both a destructuring expr or a decl on the lhs with an Option<VarDeclMode> and a Pattern.
    decl_mode: Option<VarDeclMode>,
    pat: Pattern,
    rhs: Expression,
    body: Statement, // Won't be BlockStmt, but ForBody instead. (However, could be another type of statement.)
  },
  ForOfStmt {
    await_: bool,
    // See comment in ForInStmt.
    decl_mode: Option<VarDeclMode>,
    pat: Pattern,
    rhs: Expression,
    body: Statement, // Won't be BlockStmt, but ForBody instead. (However, could be another type of statement.)
  },
  LabelStmt {
    name: String,
    statement: Statement,
  },
  ReturnStmt {
    value: Option<Expression>,
  },
  SwitchStmt {
    test: Expression,
    branches: Vec<Node>,
  },
  ThrowStmt {
    value: Expression,
  },
  TryStmt {
    wrapped: Statement,
    // One of these must be present.
    catch: Option<Node>,
    finally: Option<Statement>,
  },
  WhileStmt {
    condition: Expression,
    body: Statement,
  },

  // Others.
  TopLevel {
    body: Vec<Statement>,
  },
  CallArg {
    spread: bool,
    value: Expression,
  },
  CatchBlock {
    parameter: Option<Pattern>,
    body: Vec<Statement>, // We don't want to use BlockStmt as the new block scope starts with the parameter, not the braces. This differentiation ensures BlockStmt specifically means a new scope, helpful for downstream usages. See also: FunctionBody.
  },
  ClassMember {
    key: ClassOrObjectMemberKey,
    static_: bool,
    value: ClassOrObjectMemberValue,
  },
  // Similar purpose to CatchBlock and FunctionBody. (The scope for a `for` statement starts before the braces, so don't mix with BlockStmt.)
  ForBody {
    body: Vec<Statement>,
  },
  // This is a node instead of an enum so that we can replace it when minifying e.g. expanding shorthand to `key: value`.
  ObjectMember {
    typ: ObjectMemberType,
  },
  ObjectPatternProperty {
    key: ClassOrObjectMemberKey,
    // If `shorthand`, `key` is Direct and `target` is IdentifierPattern of same name. This way, there is always an IdentifierPattern that exists and can be visited, instead of also having to consider ClassOrObjectMemberKey::Direct as identifier if shorthand.
    target: Pattern,
    shorthand: bool,
    default_value: Option<Expression>,
  },
  SwitchBranch {
    // If None, it's `default`.
    case: Option<Expression>,
    body: Vec<Statement>,
  },
}