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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
//! Turn a JavaScript AST, coming from MD(X), into a component.
//!
//! Port of <https://github.com/mdx-js/mdx/blob/main/packages/mdx/lib/plugin/recma-document.js>,
//! by the same author.
extern crate swc_ecma_ast;
use crate::test_utils::{
hast_util_to_swc::Program,
swc_utils::{bytepos_to_point, prefix_error_with_point, span_to_position},
};
use micromark::{
unist::{Point, Position},
Location,
};
/// JSX runtimes.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum JsxRuntime {
/// Automatic runtime.
///
/// With the automatic runtime, some module is expected to exist somewhere.
/// That modules is expected to expose a certain API.
/// The compiler adds an import of that module and compiles JSX away to
/// function calls that use that API.
#[default]
Automatic,
/// Classic runtime.
///
/// With the classic runtime, you define two values yourself in each file,
/// which are expected to work a certain way.
/// The compiler compiles JSX away to function calls using those two values.
Classic,
}
/// Configuration.
#[derive(Debug, PartialEq, Eq)]
pub struct Options {
/// Pragma for JSX (used in classic runtime).
///
/// Default: `React.createElement`.
pub pragma: Option<String>,
/// Pragma for JSX fragments (used in classic runtime).
///
/// Default: `React.Fragment`.
pub pragma_frag: Option<String>,
/// Where to import the identifier of `pragma` from (used in classic runtime).
///
/// Default: `react`.
pub pragma_import_source: Option<String>,
/// Place to import automatic JSX runtimes from (used in automatic runtime).
///
/// Default: `react`.
pub jsx_import_source: Option<String>,
/// JSX runtime to use.
///
/// Default: `automatic`.
pub jsx_runtime: Option<JsxRuntime>,
}
impl Default for Options {
/// Use the automatic JSX runtime with React.
fn default() -> Self {
Self {
pragma: None,
pragma_frag: None,
pragma_import_source: None,
jsx_import_source: None,
jsx_runtime: Some(JsxRuntime::default()),
}
}
}
#[allow(dead_code)]
pub fn mdx_plugin_recma_document(
program: &mut Program,
options: &Options,
location: Option<&Location>,
) -> Result<(), String> {
// New body children.
let mut replacements = vec![];
// Inject JSX configuration comment.
if let Some(runtime) = &options.jsx_runtime {
let mut pragmas = vec![];
let react = &"react".into();
let create_element = &"React.createElement".into();
let fragment = &"React.Fragment".into();
if *runtime == JsxRuntime::Automatic {
pragmas.push("@jsxRuntime automatic".into());
pragmas.push(format!(
"@jsxImportSource {}",
if let Some(jsx_import_source) = &options.jsx_import_source {
jsx_import_source
} else {
react
}
));
} else {
pragmas.push("@jsxRuntime classic".into());
pragmas.push(format!(
"@jsx {}",
if let Some(pragma) = &options.pragma {
pragma
} else {
create_element
}
));
pragmas.push(format!(
"@jsxFrag {}",
if let Some(pragma_frag) = &options.pragma_frag {
pragma_frag
} else {
fragment
}
));
}
if !pragmas.is_empty() {
program.comments.insert(
0,
swc_common::comments::Comment {
kind: swc_common::comments::CommentKind::Block,
text: pragmas.join(" ").into(),
span: swc_common::DUMMY_SP,
},
);
}
}
// Inject an import in the classic runtime for the pragma (and presumably,
// fragment).
if options.jsx_runtime == Some(JsxRuntime::Classic) {
let pragma = if let Some(pragma) = &options.pragma {
pragma
} else {
"React"
};
let sym = pragma.split('.').next().expect("first item always exists");
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::Import(swc_ecma_ast::ImportDecl {
specifiers: vec![swc_ecma_ast::ImportSpecifier::Named(
swc_ecma_ast::ImportNamedSpecifier {
local: swc_ecma_ast::Ident {
sym: sym.into(),
optional: false,
span: swc_common::DUMMY_SP,
},
imported: None,
span: swc_common::DUMMY_SP,
is_type_only: false,
},
)],
src: Box::new(swc_ecma_ast::Str {
value: (if let Some(source) = &options.pragma_import_source {
source.clone()
} else {
"react".into()
})
.into(),
span: swc_common::DUMMY_SP,
raw: None,
}),
type_only: false,
asserts: None,
span: swc_common::DUMMY_SP,
}),
));
}
// Find the `export default`, the JSX expression, and leave the rest as it
// is.
let mut input = program.module.body.split_off(0);
input.reverse();
let mut layout = false;
let mut layout_position = None;
let content = true;
while let Some(module_item) = input.pop() {
match module_item {
// ```js
// export default props => <>{props.children}</>
// ```
//
// Treat it as an inline layout declaration.
//
// In estree, the below two are the same node (`ExportDefault`).
swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportDefaultDecl(
decl,
)) => {
if layout {
return Err(create_double_layout_message(
bytepos_to_point(&decl.span.lo, location).as_ref(),
layout_position.as_ref(),
));
}
layout = true;
layout_position = span_to_position(&decl.span, location);
match decl.decl {
swc_ecma_ast::DefaultDecl::Class(cls) => {
replacements.push(create_layout_decl(swc_ecma_ast::Expr::Class(cls)))
}
swc_ecma_ast::DefaultDecl::Fn(func) => {
replacements.push(create_layout_decl(swc_ecma_ast::Expr::Fn(func)))
}
swc_ecma_ast::DefaultDecl::TsInterfaceDecl(_) => {
return Err(
prefix_error_with_point(
"Cannot use TypeScript interface declarations as default export in MDX files. The default export is reserved for a layout, which must be a component".into(),
bytepos_to_point(&decl.span.lo, location).as_ref()
)
);
}
}
}
swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportDefaultExpr(
expr,
)) => {
if layout {
return Err(create_double_layout_message(
bytepos_to_point(&expr.span.lo, location).as_ref(),
layout_position.as_ref(),
));
}
layout = true;
layout_position = span_to_position(&expr.span, location);
replacements.push(create_layout_decl(*expr.expr));
}
// ```js
// export {a, b as c} from 'd'
// export {a, b as c}
// ```
swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportNamed(
mut named_export,
)) => {
// SWC is currently crashing when generating code, w/o source
// map, if an actual location is set on this node.
named_export.span = swc_common::DUMMY_SP;
let mut index = 0;
let mut id = None;
while index < named_export.specifiers.len() {
let mut take = false;
// Note: the `swc_ecma_ast::ExportSpecifier::Default`
// branch of this looks interesting, but as far as I
// understand it *is not* valid ES.
// `export a from 'b'` is a syntax error, even in SWC.
if let swc_ecma_ast::ExportSpecifier::Named(named) =
&named_export.specifiers[index]
{
if let Some(swc_ecma_ast::ModuleExportName::Ident(ident)) = &named.exported
{
if ident.sym.as_ref() == "default" {
// For some reason the AST supports strings
// instead of identifiers.
// Looks like some TC39 proposal. Ignore for now
// and only do things if this is an ID.
if let swc_ecma_ast::ModuleExportName::Ident(ident) = &named.orig {
if layout {
return Err(create_double_layout_message(
bytepos_to_point(&ident.span.lo, location).as_ref(),
layout_position.as_ref(),
));
}
layout = true;
layout_position = span_to_position(&ident.span, location);
take = true;
id = Some(ident.clone());
}
}
}
}
if take {
named_export.specifiers.remove(index);
} else {
index += 1;
}
}
if let Some(id) = id {
let source = named_export.src.clone();
// If there was just a default export, we can drop the original node.
if !named_export.specifiers.is_empty() {
// Pass through.
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::ExportNamed(named_export),
));
}
// It’s an `export {x} from 'y'`, so generate an import.
if let Some(source) = source {
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::Import(swc_ecma_ast::ImportDecl {
specifiers: vec![swc_ecma_ast::ImportSpecifier::Named(
swc_ecma_ast::ImportNamedSpecifier {
local: swc_ecma_ast::Ident {
sym: "MDXLayout".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
imported: Some(swc_ecma_ast::ModuleExportName::Ident(id)),
span: swc_common::DUMMY_SP,
is_type_only: false,
},
)],
src: source,
type_only: false,
asserts: None,
span: swc_common::DUMMY_SP,
}),
))
}
// It’s an `export {x}`, so generate a variable declaration.
else {
replacements.push(create_layout_decl(swc_ecma_ast::Expr::Ident(id)));
}
} else {
// Pass through.
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::ExportNamed(named_export),
));
}
}
swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::Import(mut x)) => {
// SWC is currently crashing when generating code, w/o source
// map, if an actual location is set on this node.
x.span = swc_common::DUMMY_SP;
// Pass through.
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::Import(x),
));
}
swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportDecl(_))
| swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportAll(_))
| swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::TsImportEquals(_))
| swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::TsExportAssignment(
_,
))
| swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::TsNamespaceExport(
_,
)) => {
// Pass through.
replacements.push(module_item);
}
swc_ecma_ast::ModuleItem::Stmt(swc_ecma_ast::Stmt::Expr(expr_stmt)) => {
match *expr_stmt.expr {
swc_ecma_ast::Expr::JSXElement(elem) => {
replacements.append(&mut create_mdx_content(
Some(swc_ecma_ast::Expr::JSXElement(elem)),
layout,
));
}
swc_ecma_ast::Expr::JSXFragment(mut frag) => {
// Unwrap if possible.
if frag.children.len() == 1 {
let item = frag.children.pop().unwrap();
if let swc_ecma_ast::JSXElementChild::JSXElement(elem) = item {
replacements.append(&mut create_mdx_content(
Some(swc_ecma_ast::Expr::JSXElement(elem)),
layout,
));
continue;
}
frag.children.push(item)
}
replacements.append(&mut create_mdx_content(
Some(swc_ecma_ast::Expr::JSXFragment(frag)),
layout,
));
}
_ => {
// Pass through.
replacements.push(swc_ecma_ast::ModuleItem::Stmt(
swc_ecma_ast::Stmt::Expr(expr_stmt),
));
}
}
}
swc_ecma_ast::ModuleItem::Stmt(stmt) => {
replacements.push(swc_ecma_ast::ModuleItem::Stmt(stmt));
}
}
}
// Generate an empty component.
if !content {
replacements.append(&mut create_mdx_content(None, layout));
}
// ```jsx
// export default MDXContent
// ```
replacements.push(swc_ecma_ast::ModuleItem::ModuleDecl(
swc_ecma_ast::ModuleDecl::ExportDefaultExpr(swc_ecma_ast::ExportDefaultExpr {
expr: Box::new(swc_ecma_ast::Expr::Ident(swc_ecma_ast::Ident {
sym: "MDXContent".into(),
optional: false,
span: swc_common::DUMMY_SP,
})),
span: swc_common::DUMMY_SP,
}),
));
program.module.body = replacements;
Ok(())
}
/// Create a content component.
fn create_mdx_content(
expr: Option<swc_ecma_ast::Expr>,
has_internal_layout: bool,
) -> Vec<swc_ecma_ast::ModuleItem> {
// ```jsx
// <MDXLayout {...props}>xxx</MDXLayout>
// ```
let mut result = swc_ecma_ast::Expr::JSXElement(Box::new(swc_ecma_ast::JSXElement {
opening: swc_ecma_ast::JSXOpeningElement {
name: swc_ecma_ast::JSXElementName::Ident(swc_ecma_ast::Ident {
sym: "MDXLayout".into(),
optional: false,
span: swc_common::DUMMY_SP,
}),
attrs: vec![swc_ecma_ast::JSXAttrOrSpread::SpreadElement(
swc_ecma_ast::SpreadElement {
dot3_token: swc_common::DUMMY_SP,
expr: Box::new(swc_ecma_ast::Expr::Ident(swc_ecma_ast::Ident {
sym: "props".into(),
optional: false,
span: swc_common::DUMMY_SP,
})),
},
)],
self_closing: false,
type_args: None,
span: swc_common::DUMMY_SP,
},
closing: Some(swc_ecma_ast::JSXClosingElement {
name: swc_ecma_ast::JSXElementName::Ident(swc_ecma_ast::Ident {
sym: "MDXLayout".into(),
optional: false,
span: swc_common::DUMMY_SP,
}),
span: swc_common::DUMMY_SP,
}),
// ```jsx
// <_createMdxContent {...props} />
// ```
children: vec![swc_ecma_ast::JSXElementChild::JSXElement(Box::new(
swc_ecma_ast::JSXElement {
opening: swc_ecma_ast::JSXOpeningElement {
name: swc_ecma_ast::JSXElementName::Ident(swc_ecma_ast::Ident {
sym: "_createMdxContent".into(),
optional: false,
span: swc_common::DUMMY_SP,
}),
attrs: vec![swc_ecma_ast::JSXAttrOrSpread::SpreadElement(
swc_ecma_ast::SpreadElement {
dot3_token: swc_common::DUMMY_SP,
expr: Box::new(swc_ecma_ast::Expr::Ident(swc_ecma_ast::Ident {
sym: "props".into(),
optional: false,
span: swc_common::DUMMY_SP,
})),
},
)],
self_closing: true,
type_args: None,
span: swc_common::DUMMY_SP,
},
closing: None,
children: vec![],
span: swc_common::DUMMY_SP,
},
))],
span: swc_common::DUMMY_SP,
}));
if !has_internal_layout {
// ```jsx
// MDXLayout ? <MDXLayout>xxx</MDXLayout> : _createMdxContent(props)
// ```
result = swc_ecma_ast::Expr::Cond(swc_ecma_ast::CondExpr {
test: Box::new(swc_ecma_ast::Expr::Ident(swc_ecma_ast::Ident {
sym: "MDXLayout".into(),
optional: false,
span: swc_common::DUMMY_SP,
})),
cons: Box::new(result),
alt: Box::new(swc_ecma_ast::Expr::Call(swc_ecma_ast::CallExpr {
callee: swc_ecma_ast::Callee::Expr(Box::new(swc_ecma_ast::Expr::Ident(
swc_ecma_ast::Ident {
sym: "_createMdxContent".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
))),
args: vec![swc_ecma_ast::ExprOrSpread {
spread: None,
expr: Box::new(swc_ecma_ast::Expr::Ident(swc_ecma_ast::Ident {
sym: "props".into(),
optional: false,
span: swc_common::DUMMY_SP,
})),
}],
type_args: None,
span: swc_common::DUMMY_SP,
})),
span: swc_common::DUMMY_SP,
});
}
// ```jsx
// function _createMdxContent(props) {
// return xxx
// }
// ```
let create_mdx_content = swc_ecma_ast::ModuleItem::Stmt(swc_ecma_ast::Stmt::Decl(
swc_ecma_ast::Decl::Fn(swc_ecma_ast::FnDecl {
ident: swc_ecma_ast::Ident {
sym: "_createMdxContent".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
declare: false,
function: Box::new(swc_ecma_ast::Function {
params: vec![swc_ecma_ast::Param {
pat: swc_ecma_ast::Pat::Ident(swc_ecma_ast::BindingIdent {
id: swc_ecma_ast::Ident {
sym: "props".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
type_ann: None,
}),
decorators: vec![],
span: swc_common::DUMMY_SP,
}],
decorators: vec![],
body: Some(swc_ecma_ast::BlockStmt {
stmts: vec![swc_ecma_ast::Stmt::Return(swc_ecma_ast::ReturnStmt {
arg: Some(Box::new(expr.unwrap_or({
swc_ecma_ast::Expr::Lit(swc_ecma_ast::Lit::Null(swc_ecma_ast::Null {
span: swc_common::DUMMY_SP,
}))
}))),
span: swc_common::DUMMY_SP,
})],
span: swc_common::DUMMY_SP,
}),
is_generator: false,
is_async: false,
type_params: None,
return_type: None,
span: swc_common::DUMMY_SP,
}),
}),
));
// ```jsx
// function MDXContent(props = {}) {
// return <MDXLayout>xxx</MDXLayout>
// }
// ```
let mdx_content = swc_ecma_ast::ModuleItem::Stmt(swc_ecma_ast::Stmt::Decl(
swc_ecma_ast::Decl::Fn(swc_ecma_ast::FnDecl {
ident: swc_ecma_ast::Ident {
sym: "MDXContent".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
declare: false,
function: Box::new(swc_ecma_ast::Function {
params: vec![swc_ecma_ast::Param {
pat: swc_ecma_ast::Pat::Assign(swc_ecma_ast::AssignPat {
left: Box::new(swc_ecma_ast::Pat::Ident(swc_ecma_ast::BindingIdent {
id: swc_ecma_ast::Ident {
sym: "props".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
type_ann: None,
})),
right: Box::new(swc_ecma_ast::Expr::Object(swc_ecma_ast::ObjectLit {
props: vec![],
span: swc_common::DUMMY_SP,
})),
span: swc_common::DUMMY_SP,
type_ann: None,
}),
decorators: vec![],
span: swc_common::DUMMY_SP,
}],
decorators: vec![],
body: Some(swc_ecma_ast::BlockStmt {
stmts: vec![swc_ecma_ast::Stmt::Return(swc_ecma_ast::ReturnStmt {
arg: Some(Box::new(result)),
span: swc_common::DUMMY_SP,
})],
span: swc_common::DUMMY_SP,
}),
is_generator: false,
is_async: false,
type_params: None,
return_type: None,
span: swc_common::DUMMY_SP,
}),
}),
));
vec![create_mdx_content, mdx_content]
}
/// Create a layout, inside the document.
fn create_layout_decl(expr: swc_ecma_ast::Expr) -> swc_ecma_ast::ModuleItem {
// ```jsx
// const MDXLayout = xxx
// ```
swc_ecma_ast::ModuleItem::Stmt(swc_ecma_ast::Stmt::Decl(swc_ecma_ast::Decl::Var(Box::new(
swc_ecma_ast::VarDecl {
kind: swc_ecma_ast::VarDeclKind::Const,
declare: false,
decls: vec![swc_ecma_ast::VarDeclarator {
name: swc_ecma_ast::Pat::Ident(swc_ecma_ast::BindingIdent {
id: swc_ecma_ast::Ident {
sym: "MDXLayout".into(),
optional: false,
span: swc_common::DUMMY_SP,
},
type_ann: None,
}),
init: Some(Box::new(expr)),
span: swc_common::DUMMY_SP,
definite: false,
}],
span: swc_common::DUMMY_SP,
},
))))
}
/// Create an error message about multiple layouts.
fn create_double_layout_message(at: Option<&Point>, previous: Option<&Position>) -> String {
prefix_error_with_point(
format!(
"Cannot specify multiple layouts{}",
if let Some(previous) = previous {
format!(" (previous: {:?})", previous)
} else {
"".into()
}
),
at,
)
}
|