Skip to content

Remove usages of quote_expr, quote_stmt #5643

@hyf0

Description

@hyf0

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions