-
Notifications
You must be signed in to change notification settings - Fork 709
Closed
Description
rolldown/crates/rolldown_ecmascript_utils/src/quote.rs
Lines 9 to 39 in 71b1ccc
| pub fn quote_expr<'alloc>( | |
| alloc: &'alloc Allocator, | |
| input: &str, | |
| ) -> oxc::ast::ast::Expression<'alloc> { | |
| let input = alloc.alloc_str(input); | |
| Parser::new(alloc, input, SourceType::default()) | |
| .parse_expression() | |
| .unwrap_or_else(|e| panic!("Failed to parse {input:?} into expression. Got {e:#?}")) | |
| } | |
| pub fn quote_stmts<'alloc>( | |
| alloc: &'alloc Allocator, | |
| input: &str, | |
| ) -> allocator::Vec<'alloc, ast::Statement<'alloc>> { | |
| let input = alloc.alloc_str(input); | |
| let p = Parser::new(alloc, input, SourceType::default()).parse(); | |
| assert!( | |
| !p.panicked && p.errors.is_empty(), | |
| "Failed to parse {:?} into statements. Got {:#?}", | |
| input, | |
| p.errors | |
| ); | |
| p.program.body | |
| } | |
| pub fn quote_stmt<'alloc>(alloc: &'alloc Allocator, input: &str) -> ast::Statement<'alloc> { | |
| let mut stmts = quote_stmts(alloc, input); | |
| assert_eq!(stmts.len(), 1, "Expected exactly one statement, got {}", stmts.len()); | |
| stmts.pop().unwrap() | |
| } | |
These helpers provide a good experience of constructing in runtime, but the performant way should be constructing during compile time.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels