Node to String #7424
-
Hi, I'm pretty new to parsing in general and to swc. I'm wondering if there's a function that allows me to get the string representation of an ast node, without having to rely on a use swc_common::BytePos;
use swc_ecma_ast::{EsVersion, FnDecl, Stmt};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
fn main() {
let src = r#"
const value = "";
function myFunction(param1: number, param2: string, param3: any): number {}
"#;
let mut parser = Parser::new_from(Lexer::new(
Syntax::Typescript(TsConfig {
tsx: false,
decorators: true,
dts: false,
no_early_errors: true,
disallow_ambiguous_jsx_like: false,
}),
EsVersion::Es2021,
StringInput::new(src, BytePos(0), BytePos(0)),
None,
));
let stmts: Vec<Stmt> = parser.parse_script().unwrap().body;
let func_decl: FnDecl = stmts
.into_iter()
.find_map(|stmt| stmt.decl()?.fn_decl())
.unwrap();
assert_eq!(
// >>> Does this functionality exist.
node_to_string(func_decl),
"function myFunction(param1: number, param2: string, param3: any): number {}"
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
The quickest way I can think of is |
Beta Was this translation helpful? Give feedback.
-
I would be interested in a good solution for this problem, too. I did use The |
Beta Was this translation helpful? Give feedback.
The quickest way I can think of is
compiler.print
.