mapping parsed program

I've adapted this example code for use in a program that then uses v8 to execute the resultant javascript. I'd like to be able to map errors from v8 back to the original text source (line and character) if possible. Is there an example that shows how to extract that information from a parsed module?
GitHub
swc/crates/swc_ecma_transforms_typescript/examples/ts_to_js.rs at m...
Rust-based platform for the Web. Contribute to swc-project/swc development by creating an account on GitHub.
1 Reply
xnor
xnorOP4w ago
tickling this thread again.. any insights about how to map lines between a parsed module output and input code string? AI ended up helping me
// Create an emitter for both code and source map
let mut buf = std::io::Cursor::new(Vec::new());
let mut srcmap = Vec::new();
let cfg = swc_ecma_codegen::Config::default().with_minify(false);
//let comments = SingleThreadedComments::default();

let mut emitter = Emitter {
cfg,
cm: cm.clone(),
comments: Some(&comments), // If you're preserving comments
wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, Some(&mut srcmap))),
};

// Emit the code and source map
let res = emitter.emit_program(&program);
// Create an emitter for both code and source map
let mut buf = std::io::Cursor::new(Vec::new());
let mut srcmap = Vec::new();
let cfg = swc_ecma_codegen::Config::default().with_minify(false);
//let comments = SingleThreadedComments::default();

let mut emitter = Emitter {
cfg,
cm: cm.clone(),
comments: Some(&comments), // If you're preserving comments
wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, Some(&mut srcmap))),
};

// Emit the code and source map
let res = emitter.emit_program(&program);

Did you find this page helpful?