Skip to content

Commit 218fc25

Browse files
committed
tests for export consts
1 parent 0b85118 commit 218fc25

File tree

1 file changed

+77
-0
lines changed
  • rust/import-export-transpiler-swc-plugin/src

1 file changed

+77
-0
lines changed

rust/import-export-transpiler-swc-plugin/src/lib.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,83 @@ mod tests {
551551
);
552552
}
553553

554+
#[test]
555+
fn test_export_const_expression() {
556+
let globals = Globals::new();
557+
let cm: Lrc<SourceMap> = Default::default();
558+
let diagnostics = Arc::new(Mutex::new(Vec::new()));
559+
let emitter = Box::new(TestEmitter {
560+
diagnostics: diagnostics.clone(),
561+
});
562+
let handler = Handler::with_emitter_and_flags(
563+
emitter,
564+
HandlerFlags {
565+
can_emit_warnings: true,
566+
..Default::default()
567+
},
568+
);
569+
570+
let js_code = r#"
571+
export const sql = (input) => intput + 5;
572+
export const a1 = 5, a2 = ()=>111, a3 = (inputA3)=>inputA3+"Done";
573+
"#;
574+
575+
let mut transformed_program: Option<Program> = None;
576+
577+
swc_core::common::GLOBALS.set(&globals, || {
578+
HANDLER.set(&handler, || {
579+
let fm = cm.new_source_file(
580+
Arc::new(FileName::Custom("input.js".into())),
581+
js_code.into(),
582+
);
583+
let lexer = Lexer::new(
584+
Syntax::Es(Default::default()),
585+
EsVersion::Es2020,
586+
StringInput::from(&*fm),
587+
None,
588+
);
589+
let mut parser = Parser::new_from(lexer);
590+
let mut program: Program =
591+
parser.parse_program().expect("Failed to parse the JS code");
592+
593+
let mut visitor = TransformVisitor { source_map: None };
594+
program.visit_mut_with(&mut visitor);
595+
transformed_program = Some(program);
596+
});
597+
});
598+
599+
let transformed_program = transformed_program.expect("Transformation failed");
600+
let output_code = generate_code(&transformed_program, &cm);
601+
602+
assert!(
603+
output_code.contains("const sql = (input)=>intput + 5;"),
604+
"Output code should contain original single const definition, got:\n{}",
605+
output_code
606+
);
607+
assert!(
608+
output_code.contains("addExport({\n sql: sql\n})"),
609+
"Output code should contain addExport call, got:\n{}",
610+
output_code
611+
);
612+
assert!(
613+
output_code.contains("const a1 = 5, a2 = ()=>111, a3 = (inputA3)=>inputA3 + \"Done\""),
614+
"Output code should contain original multiple const definitions, got:\n{}",
615+
output_code
616+
);
617+
assert!(
618+
output_code.contains("addExport({\n a1: a1,\n a2: a2,\n a3: a3\n})"),
619+
"Output code should contain addExport call, got:\n{}",
620+
output_code
621+
);
622+
623+
let diags = diagnostics.lock().unwrap();
624+
assert!(
625+
diags.is_empty(),
626+
"Should not emit errors, got: {:?}",
627+
*diags
628+
);
629+
}
630+
554631
#[test]
555632
fn test_import_named_default() {
556633
let globals = Globals::new();

0 commit comments

Comments
 (0)