rspack_plugin_javascript 0.103.3

rspack javascript plugin
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
//! Domain-level classifications used by the JavaScript parser hooks.
//!
//! Every payload is a SWC Next typed handle. Callers must use the parser-owned
//! [`Ast`] passed alongside the handle to read fields.

use rspack_intern::Atom;
use rspack_util::swc::AstSubRangeExt;
use swc_next_ecma_ast::{
  Ast, BindingIdentifier, BindingPattern, ExportAllDeclaration as SwcExportAllDeclaration,
  ExportDefaultDeclaration as SwcExportDefaultDeclaration, ExportDefaultDeclarationKind,
  ExportNamedDeclaration as SwcExportNamedDeclaration, Expr, ExprData, FormalParameterPattern,
  FormalParameterPatternData, FormalParameters, GetSpan, IdentifierReference, ImportAttribute,
  JsxMemberExpression, MemberExpression, ModuleExportName, ModuleExportNameData,
  SimpleAssignmentTarget, SimpleAssignmentTargetData, Span, SubRange,
};

/// An identifier visited by pattern hooks, either in a binding or an assignment.
#[derive(Debug, Clone, Copy)]
pub enum PatternIdentifier {
  Binding(BindingIdentifier),
  Reference(IdentifierReference),
}

impl PatternIdentifier {
  pub fn name<'a>(self, ast: &'a Ast<'_>) -> &'a str {
    ast.get_utf8(match self {
      Self::Binding(identifier) => identifier.name(ast),
      Self::Reference(identifier) => identifier.name(ast),
    })
  }

  pub fn span(self, ast: &Ast<'_>) -> Span {
    match self {
      Self::Binding(identifier) => identifier.span(ast),
      Self::Reference(identifier) => identifier.span(ast),
    }
  }
}

/// The hook-facing representation of an ECMAScript or JSX member expression.
///
/// SWC Next keeps those node kinds distinct. Parser plugins mostly need the
/// complete source span, while the few ECMAScript-only paths can explicitly
/// request the underlying node instead of manufacturing an incompatible AST
/// handle for JSX.
#[derive(Debug, Clone, Copy)]
pub enum HookMemberExpression {
  Ecma(MemberExpression),
  Jsx(JsxMemberExpression),
}

impl HookMemberExpression {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    match self {
      Self::Ecma(expression) => expression.span(ast),
      Self::Jsx(expression) => expression.span(ast),
    }
  }

  pub fn ecma(self) -> Option<MemberExpression> {
    match self {
      Self::Ecma(expression) => Some(expression),
      Self::Jsx(_) => None,
    }
  }
}

impl From<MemberExpression> for HookMemberExpression {
  fn from(expression: MemberExpression) -> Self {
    Self::Ecma(expression)
  }
}

impl From<JsxMemberExpression> for HookMemberExpression {
  fn from(expression: JsxMemberExpression) -> Self {
    Self::Jsx(expression)
  }
}

fn wtf8_to_atom(value: &swc_next_allocator::wtf8::Wtf8) -> Atom {
  Atom::from(value.to_string_lossy().as_ref())
}

/// Convert a formal parameter pattern into the `BindingPattern` shape used by
/// the parser plugins.
///
/// SWC Next models formal parameters with their own node kinds, so the binding
/// forms have to be projected back into `BindingPattern`.
pub fn formal_parameter_pattern(ast: &Ast<'_>, pattern: FormalParameterPattern) -> BindingPattern {
  match ast.formal_parameter_pattern_data(pattern) {
    FormalParameterPatternData::BindingIdentifier(node) => BindingPattern::BindingIdentifier(node),
    FormalParameterPatternData::ArrayPattern(node) => BindingPattern::ArrayPattern(node),
    FormalParameterPatternData::ObjectPattern(node) => BindingPattern::ObjectPattern(node),
  }
}

/// Iterate the binding patterns represented by a function's formal parameters,
/// together with the default value of each parameter.
///
/// SWC Next stores ordinary parameters and the rest parameter separately. Keeping
/// this detail here avoids reproducing the same reconstruction in parser plugins.
/// The rest parameter is represented by its argument pattern because callers only
/// consumed the former rest element to reach that pattern.
pub fn formal_parameter_patterns_with_defaults<'a>(
  ast: &'a Ast<'_>,
  params: FormalParameters,
) -> impl Iterator<Item = (BindingPattern, Option<Expr>)> + 'a {
  let rest = params
    .rest(ast)
    .map(|rest| (formal_parameter_pattern(ast, rest.argument(ast)), None));
  ast
    .nodes(params.items(ast))
    .filter_map(move |item| item.as_formal_parameter(ast))
    .map(move |parameter| {
      (
        formal_parameter_pattern(ast, parameter.pattern(ast)),
        parameter.initializer(ast),
      )
    })
    .chain(rest)
}

/// Iterate the binding patterns represented by a function's formal parameters.
pub fn formal_parameter_patterns<'a>(
  ast: &'a Ast<'_>,
  params: FormalParameters,
) -> impl Iterator<Item = BindingPattern> + 'a {
  formal_parameter_patterns_with_defaults(ast, params).map(|(pattern, _)| pattern)
}

/// Returns whether all formal parameters are plain identifier bindings.
///
/// This intentionally rejects rest parameters, destructuring and default values.
/// Those cases need their parameter initialization semantics to be analyzed
/// separately.
pub fn formal_parameters_are_simple_identifiers(ast: &Ast<'_>, params: FormalParameters) -> bool {
  params.rest(ast).is_none()
    && ast.nodes(params.items(ast)).all(|item| {
      item.as_formal_parameter(ast).is_some_and(|parameter| {
        parameter.initializer(ast).is_none()
          && parameter.pattern(ast).as_binding_identifier(ast).is_some()
      })
    })
}

#[derive(Debug, Clone, Copy)]
pub struct ExportAllDeclaration(pub SwcExportAllDeclaration);

impl ExportAllDeclaration {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    self.0.span(ast)
  }

  pub fn source(self, ast: &Ast<'_>) -> Atom {
    let source = self.0.source(ast);
    wtf8_to_atom(ast.get_wtf8(source.value(ast)))
  }

  pub fn exported_name_span(self, ast: &Ast<'_>) -> Option<Span> {
    self.0.exported(ast).map(|name| name.span(ast))
  }

  pub fn exported_name(self, ast: &Ast<'_>) -> Option<Atom> {
    self
      .0
      .exported(ast)
      .map(|name| module_export_name_to_atom(ast, name))
  }

  pub fn attributes(self, ast: &Ast<'_>) -> SubRange<ImportAttribute> {
    self.0.attributes(ast)
  }

  pub fn is_star_export(self, ast: &Ast<'_>) -> bool {
    self.0.exported(ast).is_none()
  }
}

#[derive(Debug, Clone, Copy)]
pub struct ExportNamedDeclaration(pub SwcExportNamedDeclaration);

impl ExportNamedDeclaration {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    self.0.span(ast)
  }

  pub fn source(self, ast: &Ast<'_>) -> Option<Atom> {
    self
      .0
      .source(ast)
      .map(|source| wtf8_to_atom(ast.get_wtf8(source.value(ast))))
  }

  pub fn declaration_span(self, ast: &Ast<'_>) -> Option<Span> {
    self
      .0
      .declaration(ast)
      .map(|declaration| declaration.span(ast))
  }

  pub fn attributes(self, ast: &Ast<'_>) -> SubRange<ImportAttribute> {
    self.0.attributes(ast)
  }

  pub fn named_export_specifiers<'a>(
    self,
    ast: &'a Ast<'_>,
  ) -> impl Iterator<Item = (Atom, Atom, Span)> + 'a {
    ast.nodes(self.0.specifiers(ast)).map(move |specifier| {
      let local = specifier.local(ast);
      let exported = specifier.exported(ast);
      (
        module_export_name_to_atom(ast, local),
        module_export_name_to_atom(ast, exported),
        exported.span(ast),
      )
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub struct ExportDefaultDeclaration(pub SwcExportDefaultDeclaration);

impl ExportDefaultDeclaration {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    self.0.span(ast)
  }

  pub fn declaration_span(self, ast: &Ast<'_>) -> Span {
    self.0.declaration(ast).span(ast)
  }

  pub fn declaration(self, ast: &Ast<'_>) -> ExportDefaultDeclarationKind {
    self.0.declaration(ast)
  }
}

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

impl ExportImport {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    match self {
      Self::All(node) => node.span(ast),
      Self::Named(node) => node.span(ast),
    }
  }

  pub fn source(self, ast: &Ast<'_>) -> Atom {
    match self {
      Self::All(node) => node.source(ast),
      Self::Named(node) => node
        .source(ast)
        .expect("re-export declaration must have a source"),
    }
  }

  pub fn attributes(self, ast: &Ast<'_>) -> SubRange<ImportAttribute> {
    match self {
      Self::All(node) => node.attributes(ast),
      Self::Named(node) => node.attributes(ast),
    }
  }

  pub fn is_star_export(self, ast: &Ast<'_>) -> bool {
    matches!(self, Self::All(node) if node.is_star_export(ast))
  }
}

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

impl ExportLocal {
  pub fn span(self, ast: &Ast<'_>) -> Span {
    match self {
      Self::Named(node) => node.span(ast),
      Self::Default(node) => node.span(ast),
    }
  }

  pub fn declaration_span(self, ast: &Ast<'_>) -> Option<Span> {
    match self {
      Self::Named(node) => node.declaration_span(ast),
      Self::Default(node) => Some(node.declaration_span(ast)),
    }
  }
}

pub fn module_export_name_to_atom(ast: &Ast<'_>, name: ModuleExportName) -> Atom {
  match ast.module_export_name_data(name) {
    ModuleExportNameData::IdentifierName(identifier) => {
      Atom::from(ast.get_utf8(identifier.name(ast)))
    }
    ModuleExportNameData::StringLiteral(string) => wtf8_to_atom(ast.get_wtf8(string.value(ast))),
  }
}

/// Unwraps TypeScript assertion wrappers (`as`, `satisfies`, type assertion,
/// non-null) to the underlying identifier reference, if any.
///
/// Assignment and update walkers must use this when recording mutations so
/// typed targets such as `(x as number) = 1` still mark `x` as mutated.
pub fn simple_assignment_target_identifier(
  ast: &Ast<'_>,
  target: SimpleAssignmentTarget,
) -> Option<IdentifierReference> {
  match ast.simple_assignment_target_data(target) {
    SimpleAssignmentTargetData::IdentifierReference(identifier) => Some(identifier),
    SimpleAssignmentTargetData::MemberExpression(_) => None,
    SimpleAssignmentTargetData::TsAsExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    SimpleAssignmentTargetData::TsSatisfiesExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    SimpleAssignmentTargetData::TsTypeAssertion(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    SimpleAssignmentTargetData::TsNonNullExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
  }
}

fn expr_assignment_target_identifier(ast: &Ast<'_>, expr: Expr) -> Option<IdentifierReference> {
  match ast.expr_data(expr) {
    ExprData::IdentifierReference(identifier) => Some(identifier),
    ExprData::TsAsExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    ExprData::TsSatisfiesExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    ExprData::TsTypeAssertion(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    ExprData::TsNonNullExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    ExprData::ParenthesizedExpression(expression) => {
      expr_assignment_target_identifier(ast, expression.expression(ast))
    }
    _ => None,
  }
}

#[cfg(test)]
mod tests {
  use rspack_util::swc::AstSubRangeExt;
  use swc_next_allocator::Allocator;
  use swc_next_ecma_ast::{ExprData, Lang, SourceType, StmtData};
  use swc_next_ecma_parser::{CommentMode, Options, Parser, TokenParserConfig};

  use super::simple_assignment_target_identifier;

  #[test]
  fn unwraps_typed_assignment_target_identifiers() {
    let allocator = Allocator::new();
    let source = "\
let x = 0;
(x as number) = 1;
(x!) = 2;
(<number>x) = 3;
(x satisfies number) = 4;
";
    let parsed = Parser::init(
      &allocator,
      source,
      Options {
        source_type: SourceType::Script,
        lang: Lang::Ts,
        preserve_parens: false,
        comments: CommentMode::Flat,
      },
      TokenParserConfig,
    )
    .parse();
    assert!(
      parsed.diagnostics.is_empty(),
      "parse diagnostics: {:?}",
      parsed.diagnostics
    );

    // SAFETY: This test only reads handles obtained from the parsed AST.
    let ast = unsafe { parsed.ast.ast() };
    let program = ast.root_program();
    let mut names = Vec::new();
    for statement in ast.nodes(program.body(ast)) {
      let StmtData::ExpressionStatement(expression_statement) = ast.stmt_data(statement) else {
        continue;
      };
      let ExprData::AssignmentExpression(assignment) =
        ast.expr_data(expression_statement.expression(ast))
      else {
        continue;
      };
      let Some(simple) = assignment.left(ast).as_simple_assignment_target(ast) else {
        continue;
      };
      // Direct identifier lookup must fail for typed wrappers…
      assert!(
        simple.as_identifier_reference(ast).is_none(),
        "typed target should not be a bare identifier"
      );
      // …but unwrapping recovers the binding name.
      let identifier = simple_assignment_target_identifier(ast, simple)
        .expect("typed assignment target should unwrap to an identifier");
      names.push(ast.get_utf8(identifier.name(ast)));
    }

    assert_eq!(names, ["x", "x", "x", "x"]);
  }
}