rspack_plugin_javascript 0.100.0-rc.3

rspack javascript plugin
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! The compat estree helpers for swc ecma ast

use rspack_util::atom::ModuleExportNameExt;
use swc_core::{
  atoms::Atom,
  common::{Span, Spanned},
  ecma::ast::{
    BlockStmt, BreakStmt, Class, ClassDecl, ClassExpr, ContinueStmt, DebuggerStmt, Decl,
    DoWhileStmt, EmptyStmt, ExportAll, ExportDecl, ExportDefaultDecl, ExportDefaultExpr,
    ExportSpecifier, Expr, ExprStmt, FnDecl, FnExpr, ForInStmt, ForOfStmt, ForStmt, Function,
    Ident, IfStmt, LabeledStmt, NamedExport, ObjectLit, ReturnStmt, Stmt, SwitchStmt, ThrowStmt,
    TryStmt, UsingDecl, VarDecl, VarDeclKind, VarDeclarator, WhileStmt, WithStmt,
  },
};

use crate::JS_DEFAULT_KEYWORD;

#[derive(Debug, Clone, Copy)]
pub enum ClassDeclOrExpr<'ast> {
  Decl(MaybeNamedClassDecl<'ast>),
  Expr(&'ast ClassExpr),
}

impl Spanned for ClassDeclOrExpr<'_> {
  fn span(&self) -> Span {
    match self {
      ClassDeclOrExpr::Decl(decl) => decl.span(),
      ClassDeclOrExpr::Expr(expr) => expr.span(),
    }
  }
}

impl ClassDeclOrExpr<'_> {
  pub fn ident(&self) -> Option<&Ident> {
    match self {
      ClassDeclOrExpr::Decl(decl) => decl.ident,
      ClassDeclOrExpr::Expr(expr) => expr.ident.as_ref(),
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportAllDeclaration<'ast> {
  /// `export * from 'm'`
  All(&'ast ExportAll),
  /// `export * as x from 'm'`
  NamedAll(&'ast NamedExport),
}

impl Spanned for ExportAllDeclaration<'_> {
  fn span(&self) -> Span {
    match self {
      ExportAllDeclaration::All(all) => all.span(),
      ExportAllDeclaration::NamedAll(all) => all.span(),
    }
  }
}

impl ExportAllDeclaration<'_> {
  pub fn source(&self) -> &Atom {
    match self {
      ExportAllDeclaration::All(e) => e
        .src
        .value
        .as_atom()
        .expect("ModuleExportName should be a valid utf8"),
      ExportAllDeclaration::NamedAll(e) => e
        .src
        .as_ref()
        .expect("ExportAllDeclaration::NamedAll (export * as x from 'm') must have src")
        .value
        .as_atom()
        .expect("ModuleExportName should be a valid utf8"),
    }
  }

  pub fn source_span(&self) -> Span {
    match self {
      ExportAllDeclaration::All(all) => all.src.span(),
      ExportAllDeclaration::NamedAll(all) => all
        .src
        .as_ref()
        .expect("ExportAllDeclaration::NamedAll (export * as x from 'm') must have src")
        .span(),
    }
  }

  pub fn exported_name_span(&self) -> Option<Span> {
    match self {
      ExportAllDeclaration::All(_) => None,
      ExportAllDeclaration::NamedAll(e) => Some(
        e.specifiers
          .first()
          .and_then(|e| e.as_namespace())
          .map(|e| e.name.span())
          .expect("ExportAllDeclaration::NamedAll (export * as x from 'm') must one specifier"),
      ),
    }
  }

  pub fn exported_name(&self) -> Option<&Atom> {
    match self {
      ExportAllDeclaration::All(_) => None,
      ExportAllDeclaration::NamedAll(e) => Some(
        e.specifiers
          .first()
          .and_then(|e| e.as_namespace())
          .map(|e| e.name.atom_ref())
          .expect("ExportAllDeclaration::NamedAll (export * as x from 'm') must one specifier"),
      ),
    }
  }

  pub fn get_with_obj(&self) -> Option<&ObjectLit> {
    match self {
      ExportAllDeclaration::All(e) => e.with.as_deref(),
      ExportAllDeclaration::NamedAll(e) => e.with.as_deref(),
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportNamedDeclaration<'ast> {
  /// `export var x = 1`
  /// `export class X {}`
  Decl(&'ast ExportDecl),
  /// `export { x } from 'm'`
  /// `export { x }`
  Specifiers(&'ast NamedExport),
}

impl Spanned for ExportNamedDeclaration<'_> {
  fn span(&self) -> Span {
    match self {
      ExportNamedDeclaration::Decl(decl) => decl.span(),
      ExportNamedDeclaration::Specifiers(export) => export.span(),
    }
  }
}

impl ExportNamedDeclaration<'_> {
  pub fn source(&self) -> Option<&Atom> {
    match self {
      Self::Decl(_) => None,
      Self::Specifiers(e) => e.src.as_ref().map(|s| {
        s.value
          .as_atom()
          .expect("ModuleExportName should be a valid utf8")
      }),
    }
  }

  pub fn source_span(&self) -> Option<Span> {
    match self {
      ExportNamedDeclaration::Decl(_) => None,
      ExportNamedDeclaration::Specifiers(e) => e.src.as_ref().map(|s| s.span()),
    }
  }

  pub fn declaration_span(&self) -> Option<Span> {
    match self {
      ExportNamedDeclaration::Decl(decl) => Some(decl.decl.span()),
      ExportNamedDeclaration::Specifiers(_) => None,
    }
  }

  pub fn get_with_obj(&self) -> Option<&ObjectLit> {
    match self {
      ExportNamedDeclaration::Decl(_) => None,
      ExportNamedDeclaration::Specifiers(e) => e.with.as_deref(),
    }
  }

  pub fn named_export_specifiers(
    named: &NamedExport,
  ) -> impl Iterator<Item = (Atom, Atom, Span)> + use<'_> {
    named.specifiers.iter().map(|spec| {
      match spec {
        ExportSpecifier::Namespace(_) => unreachable!("should handle ExportSpecifier::Namespace by ExportAllOrNamedAll::NamedAll in block_pre_walk_export_all_declaration"),
        ExportSpecifier::Default(s) => {
          (JS_DEFAULT_KEYWORD.clone(), s.exported.sym.clone(), s.exported.span())
        },
        ExportSpecifier::Named(n) => {
          let exported_name = n.exported.as_ref().unwrap_or(&n.orig);
          (n.orig.atom().into_owned(), exported_name.atom().into_owned(), exported_name.span())
        },
      }
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportDefaultDeclaration<'ast> {
  /// `export default class X {}`
  /// `export default class {}`
  /// `export default function x() {}`
  /// `export default function () {}`
  Decl(&'ast ExportDefaultDecl),
  /// `export default (class X {})`
  /// `export default 'x'`
  Expr(&'ast ExportDefaultExpr),
}

impl Spanned for ExportDefaultDeclaration<'_> {
  fn span(&self) -> Span {
    match self {
      ExportDefaultDeclaration::Decl(decl) => decl.span(),
      ExportDefaultDeclaration::Expr(expr) => expr.span(),
    }
  }
}

impl ExportDefaultDeclaration<'_> {
  fn declaration_span(&self) -> Span {
    match self {
      ExportDefaultDeclaration::Decl(decl) => decl.decl.span(),
      ExportDefaultDeclaration::Expr(expr) => expr.expr.span(),
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportDefaultExpression<'ast> {
  /// `export default function () {}`
  FnDecl(&'ast FnExpr),
  /// `export default class {}`
  ClassDecl(&'ast ClassExpr),
  /// `export default (class {})`
  /// `export default 'x'`
  Expr(&'ast Expr),
}

impl Spanned for ExportDefaultExpression<'_> {
  fn span(&self) -> Span {
    match self {
      ExportDefaultExpression::FnDecl(f) => f.span(),
      ExportDefaultExpression::ClassDecl(c) => c.span(),
      ExportDefaultExpression::Expr(e) => e.span(),
    }
  }
}

impl ExportDefaultExpression<'_> {
  pub fn ident(&self) -> Option<&Atom> {
    match self {
      ExportDefaultExpression::FnDecl(f) => f.ident.as_ref().map(|ident| &ident.sym),
      ExportDefaultExpression::ClassDecl(c) => c.ident.as_ref().map(|ident| &ident.sym),
      ExportDefaultExpression::Expr(_) => None,
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportImport<'ast> {
  All(ExportAllDeclaration<'ast>),
  Named(ExportNamedDeclaration<'ast>),
}

impl Spanned for ExportImport<'_> {
  fn span(&self) -> Span {
    match self {
      ExportImport::All(all) => all.span(),
      ExportImport::Named(named) => named.span(),
    }
  }
}

impl ExportImport<'_> {
  pub fn source(&self) -> &Atom {
    match self {
      ExportImport::All(e) => e.source(),
      ExportImport::Named(e) => e
        .source()
        .expect("ExportImport::Named (export { x } from 'm') should have src"),
    }
  }

  pub fn source_span(&self) -> Span {
    match self {
      ExportImport::All(all) => all.source_span(),
      ExportImport::Named(named) => named
        .source_span()
        .expect("ExportImport::Named (export { x } from 'm') should have src"),
    }
  }

  pub fn get_with_obj(&self) -> Option<&ObjectLit> {
    match self {
      ExportImport::All(e) => e.get_with_obj(),
      ExportImport::Named(e) => e.get_with_obj(),
    }
  }

  pub fn is_star_export(&self) -> bool {
    matches!(self, ExportImport::All(ExportAllDeclaration::All(_)))
  }
}

#[derive(Debug, Clone, Copy)]
pub enum ExportLocal<'ast> {
  Named(ExportNamedDeclaration<'ast>),
  Default(ExportDefaultDeclaration<'ast>),
}

impl Spanned for ExportLocal<'_> {
  fn span(&self) -> Span {
    match self {
      ExportLocal::Named(decl) => decl.span(),
      ExportLocal::Default(decl) => decl.span(),
    }
  }
}

impl ExportLocal<'_> {
  pub fn declaration_span(&self) -> Option<Span> {
    match self {
      ExportLocal::Named(named) => named.declaration_span(),
      ExportLocal::Default(default) => Some(default.declaration_span()),
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub struct MaybeNamedFunctionDecl<'ast> {
  span: Span,
  ident: Option<&'ast Ident>,
  function: &'ast Function,
}

impl Spanned for MaybeNamedFunctionDecl<'_> {
  fn span(&self) -> Span {
    self.span
  }
}

impl<'ast> From<&'ast FnDecl> for MaybeNamedFunctionDecl<'ast> {
  fn from(value: &'ast FnDecl) -> Self {
    Self {
      span: value.span(),
      ident: Some(&value.ident),
      function: &value.function,
    }
  }
}

impl<'ast> From<&'ast FnExpr> for MaybeNamedFunctionDecl<'ast> {
  fn from(f: &'ast FnExpr) -> Self {
    Self {
      span: f.span(),
      ident: f.ident.as_ref(),
      function: &f.function,
    }
  }
}

impl<'ast> MaybeNamedFunctionDecl<'ast> {
  pub fn ident(&self) -> Option<&'ast Ident> {
    self.ident
  }

  pub fn function(&self) -> &'ast Function {
    self.function
  }
}

#[derive(Debug, Clone, Copy)]
pub struct MaybeNamedClassDecl<'ast> {
  span: Span,
  ident: Option<&'ast Ident>,
  class: &'ast Class,
}

impl Spanned for MaybeNamedClassDecl<'_> {
  fn span(&self) -> Span {
    self.span
  }
}

impl<'ast> From<&'ast ClassDecl> for MaybeNamedClassDecl<'ast> {
  fn from(value: &'ast ClassDecl) -> Self {
    Self {
      span: value.span(),
      ident: Some(&value.ident),
      class: &value.class,
    }
  }
}

impl<'ast> From<&'ast ClassExpr> for MaybeNamedClassDecl<'ast> {
  fn from(value: &'ast ClassExpr) -> Self {
    Self {
      span: value.span(),
      ident: value.ident.as_ref(),
      class: &value.class,
    }
  }
}

impl MaybeNamedClassDecl<'_> {
  pub fn ident(&self) -> Option<&Ident> {
    self.ident
  }

  pub fn class(&self) -> &Class {
    self.class
  }
}

#[derive(Debug, Clone, Copy)]
pub enum Statement<'ast> {
  Block(&'ast BlockStmt),
  Empty(&'ast EmptyStmt),
  Debugger(&'ast DebuggerStmt),
  With(&'ast WithStmt),
  Return(&'ast ReturnStmt),
  Labeled(&'ast LabeledStmt),
  Break(&'ast BreakStmt),
  Continue(&'ast ContinueStmt),
  If(&'ast IfStmt),
  Switch(&'ast SwitchStmt),
  Throw(&'ast ThrowStmt),
  Try(&'ast TryStmt),
  While(&'ast WhileStmt),
  DoWhile(&'ast DoWhileStmt),
  For(&'ast ForStmt),
  ForIn(&'ast ForInStmt),
  ForOf(&'ast ForOfStmt),
  Expr(&'ast ExprStmt),
  // ClassDecl, don't put ClassExpr into it, unless it's DefaultDecl::ClassExpr
  // which is represented by ClassExpr but it actually is a ClassDecl without ident
  Class(MaybeNamedClassDecl<'ast>),
  // FnDecl, don't put FnExpr into it, unless it's DefaultDecl::FnExpr
  // which is represented by FnExpr but it actually is a FnDecl without ident
  Fn(MaybeNamedFunctionDecl<'ast>),
  Var(VariableDeclaration<'ast>),
}

impl Spanned for Statement<'_> {
  fn span(&self) -> Span {
    use Statement::*;
    match self {
      Block(d) => d.span(),
      Empty(d) => d.span(),
      Debugger(d) => d.span(),
      With(d) => d.span(),
      Return(d) => d.span(),
      Labeled(d) => d.span(),
      Break(d) => d.span(),
      Continue(d) => d.span(),
      If(d) => d.span(),
      Switch(d) => d.span(),
      Throw(d) => d.span(),
      Try(d) => d.span(),
      While(d) => d.span(),
      DoWhile(d) => d.span(),
      For(d) => d.span(),
      ForIn(d) => d.span(),
      ForOf(d) => d.span(),
      Expr(d) => d.span(),
      Class(d) => d.span(),
      Fn(d) => d.span(),
      Var(d) => d.span(),
    }
  }
}

impl<'ast> From<&'ast Stmt> for Statement<'ast> {
  fn from(value: &'ast Stmt) -> Self {
    use Statement::*;
    match value {
      Stmt::Block(d) => Block(d),
      Stmt::Empty(d) => Empty(d),
      Stmt::Debugger(d) => Debugger(d),
      Stmt::With(d) => With(d),
      Stmt::Return(d) => Return(d),
      Stmt::Labeled(d) => Labeled(d),
      Stmt::Break(d) => Break(d),
      Stmt::Continue(d) => Continue(d),
      Stmt::If(d) => If(d),
      Stmt::Switch(d) => Switch(d),
      Stmt::Throw(d) => Throw(d),
      Stmt::Try(d) => Try(d),
      Stmt::While(d) => While(d),
      Stmt::DoWhile(d) => DoWhile(d),
      Stmt::For(d) => For(d),
      Stmt::ForIn(d) => ForIn(d),
      Stmt::ForOf(d) => ForOf(d),
      Stmt::Expr(d) => Expr(d),
      Stmt::Decl(d) => d.into(),
    }
  }
}

impl<'ast> From<&'ast Decl> for Statement<'ast> {
  fn from(value: &'ast Decl) -> Self {
    use Statement::*;
    match value {
      Decl::Class(d) => Class(d.into()),
      Decl::Fn(d) => Fn(d.into()),
      Decl::Var(d) => Var(VariableDeclaration::VarDecl(d)),
      Decl::Using(d) => Var(VariableDeclaration::UsingDecl(d)),
      Decl::TsInterface(_) | Decl::TsTypeAlias(_) | Decl::TsEnum(_) | Decl::TsModule(_) => {
        unreachable!()
      }
    }
  }
}

impl<'ast> Statement<'ast> {
  pub fn as_function_decl(&self) -> Option<MaybeNamedFunctionDecl<'ast>> {
    match self {
      Statement::Fn(f) => Some(*f),
      _ => None,
    }
  }

  pub fn as_class_decl(&self) -> Option<MaybeNamedClassDecl<'ast>> {
    match self {
      Statement::Class(c) => Some(*c),
      _ => None,
    }
  }
}

#[derive(Debug, Clone, Copy)]
pub enum VariableDeclaration<'a> {
  VarDecl(&'a VarDecl),
  UsingDecl(&'a UsingDecl),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VariableDeclarationKind {
  Var,
  Let,
  Const,
  Using,
  AwaitUsing,
}

impl Spanned for VariableDeclaration<'_> {
  fn span(&self) -> Span {
    match self {
      VariableDeclaration::VarDecl(var_decl) => var_decl.span(),
      VariableDeclaration::UsingDecl(using_decl) => using_decl.span(),
    }
  }
}

impl<'a> VariableDeclaration<'a> {
  pub fn kind(&self) -> VariableDeclarationKind {
    match self {
      VariableDeclaration::VarDecl(v) => match v.kind {
        VarDeclKind::Var => VariableDeclarationKind::Var,
        VarDeclKind::Let => VariableDeclarationKind::Let,
        VarDeclKind::Const => VariableDeclarationKind::Const,
      },
      VariableDeclaration::UsingDecl(u) => {
        if u.is_await {
          VariableDeclarationKind::AwaitUsing
        } else {
          VariableDeclarationKind::Using
        }
      }
    }
  }

  pub fn declarators(&self) -> &'a [VarDeclarator] {
    match self {
      VariableDeclaration::VarDecl(v) => &v.decls,
      VariableDeclaration::UsingDecl(u) => &u.decls,
    }
  }
}