1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00
This commit is contained in:
Marvin Hagemeister 2024-12-18 15:01:46 +01:00
parent 5ea018aa58
commit 619cfb7932
2 changed files with 160 additions and 52 deletions

View file

@ -779,109 +779,82 @@ fn serialize_expr(
pos pos
} }
Expr::New(node) => { Expr::New(node) => {
let pos = ctx.header(AstNode::NewExpression, parent, &node.span, 3); let opts = ctx.alloc_new_expr(
let callee_pos = ctx.ref_field(AstProp::Callee); parent,
let type_args_pos = ctx.ref_field(AstProp::TypeArguments); &node.span,
let args_pos = ctx.ref_vec_field(
AstProp::Arguments,
node.args.as_ref().map_or(0, |v| v.len()), node.args.as_ref().map_or(0, |v| v.len()),
); );
let callee = serialize_expr(ctx, node.callee.as_ref(), pos); let callee = serialize_expr(ctx, node.callee.as_ref(), opts.0);
let args: Vec<NodeRef> = node.args.as_ref().map_or(vec![], |args| { let args: Vec<NodeRef> = node.args.as_ref().map_or(vec![], |args| {
args args
.iter() .iter()
.map(|arg| serialize_expr_or_spread(ctx, arg, pos)) .map(|arg| serialize_expr_or_spread(ctx, arg, opts.0))
.collect::<Vec<_>>() .collect::<Vec<_>>()
}); });
let type_args = node.type_args.clone().map(|param_node| { let type_args = node.type_args.clone().map(|param_node| {
serialize_ts_param_inst(ctx, param_node.as_ref(), pos) serialize_ts_param_inst(ctx, param_node.as_ref(), opts.0)
}); });
ctx.write_ref(callee_pos, callee); ctx.commit_new_expr(opts, callee, type_args, args)
ctx.write_maybe_ref(type_args_pos, type_args);
ctx.write_refs(args_pos, args);
pos
} }
Expr::Seq(node) => { Expr::Seq(node) => {
let pos = ctx.header(AstNode::SequenceExpression, parent, &node.span, 1); let opts = ctx.alloc_seq(parent, &node.span, node.exprs.len());
let exprs_pos = ctx.ref_vec_field(AstProp::Expressions, node.exprs.len());
let children = node let children = node
.exprs .exprs
.iter() .iter()
.map(|expr| serialize_expr(ctx, expr, pos)) .map(|expr| serialize_expr(ctx, expr, opts.0))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_refs(exprs_pos, children); ctx.commit_seq(opts, children)
pos
} }
Expr::Ident(node) => serialize_ident(ctx, node, parent), Expr::Ident(node) => serialize_ident(ctx, node, parent),
Expr::Lit(node) => serialize_lit(ctx, node, parent), Expr::Lit(node) => serialize_lit(ctx, node, parent),
Expr::Tpl(node) => { Expr::Tpl(node) => {
let pos = ctx.header(AstNode::TemplateLiteral, parent, &node.span, 2); let opts =
let quasis_pos = ctx.ref_vec_field(AstProp::Quasis, node.quasis.len()); ctx.alloc_tpl(parent, &node.span, node.quasis.len(), node.exprs.len());
let exprs_pos = ctx.ref_vec_field(AstProp::Expressions, node.exprs.len());
let quasis = node let quasis = node
.quasis .quasis
.iter() .iter()
.map(|quasi| { .map(|quasi| {
let tpl_pos = let opts = ctx.alloc_tpl_elem(parent, &node.span);
ctx.header(AstNode::TemplateElement, pos, &quasi.span, 3);
let tail_pos = ctx.bool_field(AstProp::Tail);
let raw_pos = ctx.str_field(AstProp::Raw);
let cooked_pos = ctx.str_field(AstProp::Cooked);
ctx.write_bool(tail_pos, quasi.tail); ctx.commit_tpl_elem(
ctx.write_str(raw_pos, &quasi.raw); opts,
ctx.write_str( quasi.tail,
cooked_pos, &quasi.raw,
&quasi &quasi
.cooked .cooked
.as_ref() .as_ref()
.map_or("".to_string(), |v| v.to_string()), .map_or("".to_string(), |v| v.to_string()),
); )
tpl_pos
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let exprs = node let exprs = node
.exprs .exprs
.iter() .iter()
.map(|expr| serialize_expr(ctx, expr, pos)) .map(|expr| serialize_expr(ctx, expr, opts.0))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_refs(quasis_pos, quasis); ctx.commit_tpl(opts, quasis, exprs)
ctx.write_refs(exprs_pos, exprs);
pos
} }
Expr::TaggedTpl(node) => { Expr::TaggedTpl(node) => {
let pos = let opts = ctx.alloc_tagged_tpl(parent, &node.span);
ctx.header(AstNode::TaggedTemplateExpression, parent, &node.span, 3);
let tag_pos = ctx.ref_field(AstProp::Tag);
let type_arg_pos = ctx.ref_field(AstProp::TypeArguments);
let quasi_pos = ctx.ref_field(AstProp::Quasi);
let tag = serialize_expr(ctx, &node.tag, pos); let tag = serialize_expr(ctx, &node.tag, opts.0);
let type_param_id = node let type_param_id = node
.type_params .type_params
.clone() .clone()
.map(|params| serialize_ts_param_inst(ctx, params.as_ref(), pos)); .map(|params| serialize_ts_param_inst(ctx, params.as_ref(), opts.0));
let quasi = serialize_expr(ctx, &Expr::Tpl(*node.tpl.clone()), pos); let quasi = serialize_expr(ctx, &Expr::Tpl(*node.tpl.clone()), opts.0);
ctx.write_ref(tag_pos, tag); ctx.commit_tagged_tpl(opts, tag, type_param_id, quasi)
ctx.write_maybe_ref(type_arg_pos, type_param_id);
ctx.write_ref(quasi_pos, quasi);
pos
} }
Expr::Arrow(node) => { Expr::Arrow(node) => {
let pos = let pos =

View file

@ -434,6 +434,141 @@ impl TsEsTreeBuilder {
ctx: SerializeCtx::new(kind_count, prop_count), ctx: SerializeCtx::new(kind_count, prop_count),
} }
} }
pub fn alloc_new_expr(
&mut self,
parent: NodeRef,
span: &Span,
args_len: usize,
) -> (NodeRef, usize, usize, usize) {
let pos = self.ctx.header(AstNode::NewExpression, parent, span, 3);
let callee_pos = self.ctx.ref_field(AstProp::Callee);
let type_args_pos = self.ctx.ref_field(AstProp::TypeArguments);
let args_pos = self.ctx.ref_vec_field(AstProp::Arguments, args_len);
(pos, callee_pos, type_args_pos, args_pos)
}
pub fn commit_new_expr(
&mut self,
opts: (NodeRef, usize, usize, usize),
callee: NodeRef,
type_args: Option<NodeRef>,
args: Vec<NodeRef>,
) -> NodeRef {
self.ctx.write_ref(opts.1, callee);
self.ctx.write_maybe_ref(opts.2, type_args);
self.ctx.write_refs(opts.3, args);
opts.0
}
pub fn alloc_seq(
&mut self,
parent: NodeRef,
span: &Span,
len: usize,
) -> (NodeRef, usize) {
let pos = self
.ctx
.header(AstNode::SequenceExpression, parent, &span, 1);
let exprs_pos = self.ctx.ref_vec_field(AstProp::Expressions, len);
(pos, exprs_pos)
}
pub fn commit_seq(
&mut self,
opts: (NodeRef, usize),
children: Vec<NodeRef>,
) -> NodeRef {
self.ctx.write_refs(opts.1, children);
opts.0
}
pub fn alloc_tpl(
&mut self,
parent: NodeRef,
span: &Span,
quasis_len: usize,
exprs_len: usize,
) -> (NodeRef, usize, usize) {
let pos = self.ctx.header(AstNode::TemplateLiteral, parent, &span, 2);
let quasis_pos = self.ctx.ref_vec_field(AstProp::Quasis, quasis_len);
let exprs_pos = self.ctx.ref_vec_field(AstProp::Expressions, exprs_len);
(pos, quasis_pos, exprs_pos)
}
pub fn commit_tpl(
&mut self,
opts: (NodeRef, usize, usize),
quasis: Vec<NodeRef>,
exprs: Vec<NodeRef>,
) -> NodeRef {
self.ctx.write_refs(opts.1, quasis);
self.ctx.write_refs(opts.2, exprs);
opts.0
}
pub fn alloc_tpl_elem(
&mut self,
parent: NodeRef,
span: &Span,
) -> (NodeRef, usize, usize, usize) {
let tpl_pos = self.ctx.header(AstNode::TemplateElement, parent, span, 3);
let tail_pos = self.ctx.bool_field(AstProp::Tail);
let raw_pos = self.ctx.str_field(AstProp::Raw);
let cooked_pos = self.ctx.str_field(AstProp::Cooked);
(tpl_pos, tail_pos, raw_pos, cooked_pos)
}
pub fn commit_tpl_elem(
&mut self,
opts: (NodeRef, usize, usize, usize),
tail: bool,
quasi: &str,
raw: &str,
) -> NodeRef {
self.ctx.write_bool(opts.1, tail);
self.ctx.write_str(opts.2, raw);
self.ctx.write_str(opts.3, quasi);
opts.0
}
pub fn alloc_tagged_tpl(
&mut self,
parent: NodeRef,
span: &Span,
) -> (NodeRef, usize, usize, usize) {
let pos =
self
.ctx
.header(AstNode::TaggedTemplateExpression, parent, &span, 3);
let tag_pos = self.ctx.ref_field(AstProp::Tag);
let type_arg_pos = self.ctx.ref_field(AstProp::TypeArguments);
let quasi_pos = self.ctx.ref_field(AstProp::Quasi);
(pos, tag_pos, type_arg_pos, quasi_pos)
}
pub fn commit_tagged_tpl(
&mut self,
opts: (NodeRef, usize, usize, usize),
tag: NodeRef,
type_param: Option<NodeRef>,
quasi: NodeRef,
) -> NodeRef {
self.ctx.write_ref(opts.1, tag);
self.ctx.write_maybe_ref(opts.2, type_param);
self.ctx.write_ref(opts.3, quasi);
opts.0
}
} }
impl AstBufSerializer<AstNode, AstProp> for TsEsTreeBuilder { impl AstBufSerializer<AstNode, AstProp> for TsEsTreeBuilder {