diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-10-04 10:29:30 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-10-04 10:29:30 +0200 |
commit | 9d90e35d51555d9f853f2eccfa771f47d71a6bc1 (patch) | |
tree | afaa271b06215981a137868f652431ef9ab293f7 /tests/test_utils/swc.rs | |
parent | 3a1fd1918f65c0a751a872d66e2d5bb01c64830c (diff) | |
download | markdown-rs-9d90e35d51555d9f853f2eccfa771f47d71a6bc1.tar.gz markdown-rs-9d90e35d51555d9f853f2eccfa771f47d71a6bc1.tar.bz2 markdown-rs-9d90e35d51555d9f853f2eccfa771f47d71a6bc1.zip |
Refactor to share a test utility
Diffstat (limited to 'tests/test_utils/swc.rs')
-rw-r--r-- | tests/test_utils/swc.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/tests/test_utils/swc.rs b/tests/test_utils/swc.rs index c0fffa0..80376b8 100644 --- a/tests/test_utils/swc.rs +++ b/tests/test_utils/swc.rs @@ -3,8 +3,11 @@ extern crate swc_common; extern crate swc_ecma_ast; extern crate swc_ecma_parser; use micromark::{MdxExpressionKind, MdxSignal}; -use swc_common::{source_map::Pos, BytePos, FileName, SourceFile, Spanned}; +use swc_common::{ + source_map::Pos, sync::Lrc, BytePos, FileName, FilePathMapping, SourceFile, SourceMap, Spanned, +}; use swc_ecma_ast::{EsVersion, Expr, Module}; +use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_parser::{ error::Error as SwcError, parse_file_as_expr, parse_file_as_module, EsConfig, Syntax, }; @@ -164,6 +167,30 @@ pub fn parse_expression_to_tree( } } +/// Serialize an SWC module. +/// To do: support comments. +#[allow(dead_code)] +pub fn serialize(module: &Module) -> String { + let mut buf = vec![]; + let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); + // let comm = &program.comments as &dyn swc_common::comments::Comments; + { + let mut emitter = Emitter { + cfg: swc_ecma_codegen::Config { + ..Default::default() + }, + cm: cm.clone(), + // To do: figure out how to pass them. + comments: None, + wr: JsWriter::new(cm, "\n", &mut buf, None), + }; + + emitter.emit_module(module).unwrap(); + } + + String::from_utf8_lossy(&buf).to_string() +} + /// Check that the resulting AST of ESM is OK. /// /// This checks that only module declarations (import/exports) are used, not |