1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00
This commit is contained in:
Marvin Hagemeister 2025-01-04 02:32:19 +01:00
parent 6c0c91a2fc
commit d3ae935d45
4 changed files with 1174 additions and 361 deletions

View file

@ -50,8 +50,8 @@ const PropFlags = {
Undefined: 6, Undefined: 6,
/** An object */ /** An object */
Obj: 7, Obj: 7,
/** A regex obj */
Regex: 8, Regex: 8,
BigInt: 8,
}; };
/** @typedef {import("./40_lint_types.d.ts").AstContext} AstContext */ /** @typedef {import("./40_lint_types.d.ts").AstContext} AstContext */

View file

@ -20,6 +20,7 @@ pub enum PropFlags {
Undefined, Undefined,
Obj, Obj,
Regex, Regex,
BigInt,
} }
impl From<PropFlags> for u8 { impl From<PropFlags> for u8 {
@ -476,6 +477,14 @@ impl SerializeCtx {
self.field(prop, PropFlags::Regex) self.field(prop, PropFlags::Regex)
} }
// Allocate a bigint field. Bigints are internally represented as strings
pub fn bigint_field<P>(&mut self, prop: P) -> usize
where
P: Into<u8> + Display + Clone,
{
self.field(prop, PropFlags::BigInt)
}
/// Allocate a bool field /// Allocate a bool field
pub fn bool_field<P>(&mut self, prop: P) -> usize pub fn bool_field<P>(&mut self, prop: P) -> usize
where where

View file

@ -87,6 +87,7 @@ use super::buffer::StrPos;
use super::ts_estree::AstNode; use super::ts_estree::AstNode;
use super::ts_estree::AstProp; use super::ts_estree::AstProp;
use super::ts_estree::TsEsTreeBuilder; use super::ts_estree::TsEsTreeBuilder;
use super::ts_estree::TsKeywordKind;
pub fn serialize_swc_to_buffer(parsed_source: &ParsedSource) -> Vec<u8> { pub fn serialize_swc_to_buffer(parsed_source: &ParsedSource) -> Vec<u8> {
let mut ctx = TsEsTreeBuilder::new(); let mut ctx = TsEsTreeBuilder::new();
@ -918,41 +919,25 @@ fn serialize_expr(
pos pos
} }
Expr::TsConstAssertion(node) => { Expr::TsConstAssertion(node) => {
let raw = ctx.header(AstNode::TSAsExpression, parent, &node.span); let pos = ctx.alloc_ts_as_expr(parent, &node.span);
let expr_pos = ctx.ref_field(AstProp::Expression);
ctx.null_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let expr = serialize_expr(ctx, node.expr.as_ref(), pos); let expr = serialize_expr(ctx, node.expr.as_ref(), pos);
ctx.write_ref(expr_pos, expr); // TODO(@marvinhagemeister): type_ann
ctx.write_ts_as_expr(pos, expr, NodeRef(0))
pos
} }
Expr::TsNonNull(node) => { Expr::TsNonNull(node) => {
let raw = ctx.header(AstNode::TSNonNullExpression, parent, &node.span); let pos = ctx.alloc_ts_non_null(parent, &node.span);
let expr_pos = ctx.ref_field(AstProp::Expression); let expr = serialize_expr(ctx, node.expr.as_ref(), pos);
let pos = ctx.commit_schema(raw); ctx.write_ts_non_null(pos, expr)
let expr_id = serialize_expr(ctx, node.expr.as_ref(), pos);
ctx.write_ref(expr_pos, expr_id);
pos
} }
Expr::TsAs(node) => { Expr::TsAs(node) => {
let raw = ctx.header(AstNode::TSAsExpression, parent, &node.span); let pos = ctx.alloc_ts_as_expr(parent, &node.span);
let expr_pos = ctx.ref_field(AstProp::Expression);
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let expr = serialize_expr(ctx, node.expr.as_ref(), pos); let expr = serialize_expr(ctx, node.expr.as_ref(), pos);
let type_ann = serialize_ts_type(ctx, node.type_ann.as_ref(), pos); let type_ann = serialize_ts_type(ctx, node.type_ann.as_ref(), pos);
ctx.write_ref(expr_pos, expr); ctx.write_ts_as_expr(pos, expr, type_ann)
ctx.write_ref(type_ann_pos, type_ann);
pos
} }
Expr::TsInstantiation(_) => { Expr::TsInstantiation(_) => {
// let raw = ctx.header(AstNode::TsInstantiation, parent, &node.span); // let raw = ctx.header(AstNode::TsInstantiation, parent, &node.span);
@ -971,39 +956,25 @@ fn serialize_expr(
todo!() todo!()
} }
Expr::TsSatisfies(node) => { Expr::TsSatisfies(node) => {
let raw = ctx.header(AstNode::TSSatisfiesExpression, parent, &node.span); let pos = ctx.alloc_ts_satisfies_expr(parent, &node.span);
let expr_pos = ctx.ref_field(AstProp::Expression);
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let epxr = serialize_expr(ctx, node.expr.as_ref(), pos); let expr = serialize_expr(ctx, node.expr.as_ref(), pos);
let type_ann = serialize_ts_type(ctx, node.type_ann.as_ref(), pos); let type_ann = serialize_ts_type(ctx, node.type_ann.as_ref(), pos);
ctx.write_ref(expr_pos, epxr); ctx.write_ts_satisfies_expr(pos, expr, type_ann)
ctx.write_ref(type_ann_pos, type_ann);
pos
} }
Expr::PrivateName(node) => serialize_private_name(ctx, node, parent), Expr::PrivateName(node) => serialize_private_name(ctx, node, parent),
Expr::OptChain(node) => { Expr::OptChain(node) => {
let raw = ctx.header(AstNode::ChainExpression, parent, &node.span); let pos = ctx.alloc_chain_expr(parent, &node.span);
let arg_pos = ctx.ref_field(AstProp::Argument);
let pos = ctx.commit_schema(raw);
let arg = match node.base.as_ref() { let expr = match node.base.as_ref() {
OptChainBase::Member(member_expr) => { OptChainBase::Member(member_expr) => {
serialize_member_expr(ctx, member_expr, pos, true) serialize_member_expr(ctx, member_expr, pos, true)
} }
OptChainBase::Call(opt_call) => { OptChainBase::Call(opt_call) => {
let raw = ctx.header(AstNode::CallExpression, pos, &opt_call.span); let call_pos = ctx.alloc_call_expr(pos, &opt_call.span);
let opt_pos = ctx.bool_field(AstProp::Optional);
let callee_pos = ctx.ref_field(AstProp::Callee);
let type_args_pos = ctx.ref_field(AstProp::TypeArguments);
let args_pos =
ctx.ref_vec_field(AstProp::Arguments, opt_call.args.len());
let call_pos = ctx.commit_schema(raw);
let callee = serialize_expr(ctx, &opt_call.callee, pos); let callee = serialize_expr(ctx, &opt_call.callee, call_pos);
let type_param_id = opt_call.type_args.clone().map(|params| { let type_param_id = opt_call.type_args.clone().map(|params| {
serialize_ts_param_inst(ctx, params.as_ref(), call_pos) serialize_ts_param_inst(ctx, params.as_ref(), call_pos)
@ -1012,21 +983,14 @@ fn serialize_expr(
let args = opt_call let args = opt_call
.args .args
.iter() .iter()
.map(|arg| serialize_expr_or_spread(ctx, arg, pos)) .map(|arg| serialize_expr_or_spread(ctx, arg, call_pos))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_bool(opt_pos, true); ctx.write_call_expr(call_pos, true, callee, type_param_id, args)
ctx.write_ref(callee_pos, callee);
ctx.write_maybe_ref(type_args_pos, type_param_id);
ctx.write_refs(args_pos, args);
call_pos
} }
}; };
ctx.write_ref(arg_pos, arg); ctx.write_chain_expr(pos, expr)
pos
} }
Expr::Invalid(_) => { Expr::Invalid(_) => {
unreachable!() unreachable!()
@ -1047,15 +1011,7 @@ fn serialize_prop_or_spread(
parent, parent,
), ),
PropOrSpread::Prop(prop) => { PropOrSpread::Prop(prop) => {
let raw = ctx.header(AstNode::Property, parent, &prop.span()); let pos = ctx.alloc_property(parent, &prop.span());
let shorthand_pos = ctx.bool_field(AstProp::Shorthand);
let computed_pos = ctx.bool_field(AstProp::Computed);
let method_pos = ctx.bool_field(AstProp::Method);
let kind_pos = ctx.str_field(AstProp::Kind);
let key_pos = ctx.ref_field(AstProp::Key);
let value_pos = ctx.ref_field(AstProp::Value);
let pos = ctx.commit_schema(raw);
let mut shorthand = false; let mut shorthand = false;
let mut computed = false; let mut computed = false;
@ -1063,7 +1019,7 @@ fn serialize_prop_or_spread(
let mut kind = "init"; let mut kind = "init";
// FIXME: optional // FIXME: optional
let (key_id, value_id) = match prop.as_ref() { let (key, value) = match prop.as_ref() {
Prop::Shorthand(ident) => { Prop::Shorthand(ident) => {
shorthand = true; shorthand = true;
@ -1081,18 +1037,13 @@ fn serialize_prop_or_spread(
(key, value) (key, value)
} }
Prop::Assign(assign_prop) => { Prop::Assign(assign_prop) => {
let raw = let child_pos = ctx.alloc_assign_pat(pos, &assign_prop.span);
ctx.header(AstNode::AssignmentPattern, pos, &assign_prop.span);
let left_pos = ctx.ref_field(AstProp::Left);
let right_pos = ctx.ref_field(AstProp::Right);
let child_pos = ctx.commit_schema(raw);
let left = serialize_ident(ctx, &assign_prop.key, child_pos); let left = serialize_ident(ctx, &assign_prop.key, child_pos);
let right = let right =
serialize_expr(ctx, assign_prop.value.as_ref(), child_pos); serialize_expr(ctx, assign_prop.value.as_ref(), child_pos);
ctx.write_ref(left_pos, left); ctx.write_assign_pat(child_pos, left, right);
ctx.write_ref(right_pos, right);
(left, child_pos) (left, child_pos)
} }
@ -1168,14 +1119,7 @@ fn serialize_prop_or_spread(
} }
}; };
ctx.write_bool(shorthand_pos, shorthand); ctx.write_property(pos, shorthand, computed, method, kind, key, value)
ctx.write_bool(computed_pos, computed);
ctx.write_bool(method_pos, method);
ctx.write_str(kind_pos, kind);
ctx.write_ref(key_pos, key_id);
ctx.write_ref(value_pos, value_id);
pos
} }
} }
} }
@ -1495,18 +1439,9 @@ fn serialize_decl(
todo!(); todo!();
} }
Decl::TsInterface(node) => { Decl::TsInterface(node) => {
let raw = ctx.header(AstNode::TSInterface, parent, &node.span); let pos = ctx.alloc_ts_interface(parent, &node.span);
let declare_pos = ctx.bool_field(AstProp::Declare);
let id_pos = ctx.ref_field(AstProp::Id);
let extends_pos = ctx.ref_vec_field(AstProp::Extends, node.extends.len());
let type_param_pos = ctx.ref_field(AstProp::TypeParameters);
let body_pos = ctx.ref_field(AstProp::Body);
let pos = ctx.commit_schema(raw);
let body_raw = ctx.header(AstNode::TSInterfaceBody, pos, &node.body.span); let body_pos = ctx.alloc_ts_interface_body(pos, &node.body.span);
let body_body_pos =
ctx.ref_vec_field(AstProp::Body, node.body.body.len());
let body_id = ctx.commit_schema(body_raw);
let ident_id = serialize_ident(ctx, &node.id, pos); let ident_id = serialize_ident(ctx, &node.id, pos);
let type_param = let type_param =
@ -1516,20 +1451,14 @@ fn serialize_decl(
.extends .extends
.iter() .iter()
.map(|item| { .map(|item| {
let raw = ctx.header(AstNode::TSInterfaceHeritage, pos, &item.span); let child_pos = ctx.alloc_ts_interface_heritage(pos, &item.span);
let type_args_pos = ctx.ref_field(AstProp::TypeArguments);
let expr_pos = ctx.ref_field(AstProp::Expression);
let child_pos = ctx.commit_schema(raw);
let expr = serialize_expr(ctx, &item.expr, child_pos); let expr = serialize_expr(ctx, &item.expr, child_pos);
let type_args = item.type_args.clone().map(|params| { let type_args = item.type_args.clone().map(|params| {
serialize_ts_param_inst(ctx, params.as_ref(), child_pos) serialize_ts_param_inst(ctx, params.as_ref(), child_pos)
}); });
ctx.write_ref(expr_pos, expr); ctx.write_ts_interface_heritage(child_pos, expr, type_args)
ctx.write_maybe_ref(type_args_pos, type_args);
child_pos
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -1679,16 +1608,15 @@ fn serialize_decl(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_bool(declare_pos, node.declare); ctx.write_ts_interface_body(body_pos, body_elem_ids);
ctx.write_ref(id_pos, ident_id); ctx.write_ts_interface(
ctx.write_maybe_ref(type_param_pos, type_param); pos,
ctx.write_refs(extends_pos, extend_ids); node.declare,
ctx.write_ref(body_pos, body_id); ident_id,
type_param,
// Body extend_ids,
ctx.write_refs(body_body_pos, body_elem_ids); body_pos,
)
pos
} }
Decl::TsTypeAlias(node) => { Decl::TsTypeAlias(node) => {
let pos = ctx.alloc_ts_type_alias(parent, &node.span); let pos = ctx.alloc_ts_type_alias(parent, &node.span);
@ -2013,11 +1941,7 @@ fn serialize_pat(
match pat { match pat {
Pat::Ident(node) => serialize_ident(ctx, &node.id, parent), Pat::Ident(node) => serialize_ident(ctx, &node.id, parent),
Pat::Array(node) => { Pat::Array(node) => {
let raw = ctx.header(AstNode::ArrayPattern, parent, &node.span); let pos = ctx.alloc_arr_pat(parent, &node.span);
let opt_pos = ctx.bool_field(AstProp::Optional);
let type_pos = ctx.ref_field(AstProp::TypeAnnotation);
let elems_pos = ctx.ref_vec_field(AstProp::Elements, node.elems.len());
let pos = ctx.commit_schema(raw);
let type_ann = maybe_serialize_ts_type_ann(ctx, &node.type_ann, pos); let type_ann = maybe_serialize_ts_type_ann(ctx, &node.type_ann, pos);
@ -2031,11 +1955,7 @@ fn serialize_pat(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_bool(opt_pos, node.optional); ctx.write_arr_pat(pos, node.optional, type_ann, children)
ctx.write_maybe_ref(type_pos, type_ann);
ctx.write_refs(elems_pos, children);
pos
} }
Pat::Rest(node) => { Pat::Rest(node) => {
let pos = ctx.alloc_rest_elem(parent, &node.span); let pos = ctx.alloc_rest_elem(parent, &node.span);
@ -2046,11 +1966,7 @@ fn serialize_pat(
ctx.write_rest_elem(pos, type_ann, arg) ctx.write_rest_elem(pos, type_ann, arg)
} }
Pat::Object(node) => { Pat::Object(node) => {
let raw = ctx.header(AstNode::ObjectPattern, parent, &node.span); let pos = ctx.alloc_obj_pat(parent, &node.span);
let opt_pos = ctx.bool_field(AstProp::Optional);
let props_pos = ctx.ref_vec_field(AstProp::Properties, node.props.len());
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let type_ann = maybe_serialize_ts_type_ann(ctx, &node.type_ann, pos); let type_ann = maybe_serialize_ts_type_ann(ctx, &node.type_ann, pos);
@ -2059,12 +1975,7 @@ fn serialize_pat(
.iter() .iter()
.map(|prop| match prop { .map(|prop| match prop {
ObjectPatProp::KeyValue(key_value_prop) => { ObjectPatProp::KeyValue(key_value_prop) => {
let raw = let child_pos = ctx.alloc_property(pos, &key_value_prop.span());
ctx.header(AstNode::Property, pos, &key_value_prop.span());
let computed_pos = ctx.bool_field(AstProp::Computed);
let key_pos = ctx.ref_field(AstProp::Key);
let value_pos = ctx.ref_field(AstProp::Value);
let child_pos = ctx.commit_schema(raw);
let computed = matches!(key_value_prop.key, PropName::Computed(_)); let computed = matches!(key_value_prop.key, PropName::Computed(_));
@ -2072,31 +1983,25 @@ fn serialize_pat(
let value = let value =
serialize_pat(ctx, key_value_prop.value.as_ref(), child_pos); serialize_pat(ctx, key_value_prop.value.as_ref(), child_pos);
ctx.write_bool(computed_pos, computed); ctx.write_property(
ctx.write_ref(key_pos, key); child_pos, false, computed, false, "init", key, value,
ctx.write_ref(value_pos, value); )
child_pos
} }
ObjectPatProp::Assign(assign_pat_prop) => { ObjectPatProp::Assign(assign_pat_prop) => {
let raw = ctx.header(AstNode::Property, pos, &assign_pat_prop.span); let child_pos = ctx.alloc_property(pos, &assign_pat_prop.span);
// TOOD: Doesn't seem to be present in SWC ast
let _computed_pos = ctx.bool_field(AstProp::Computed);
let key_pos = ctx.ref_field(AstProp::Key);
let value_pos = ctx.ref_field(AstProp::Value);
let child_pos = ctx.commit_schema(raw);
let ident = serialize_ident(ctx, &assign_pat_prop.key.id, parent); let ident = serialize_ident(ctx, &assign_pat_prop.key.id, parent);
let value = assign_pat_prop // TODO(@marvinhagemeister): This seems wrong
.value
.as_ref()
.map(|value| serialize_expr(ctx, value, child_pos));
ctx.write_ref(key_pos, ident); let value =
ctx.write_maybe_ref(value_pos, value); assign_pat_prop.value.as_ref().map_or(NodeRef(0), |value| {
serialize_expr(ctx, value, child_pos)
});
child_pos ctx.write_property(
child_pos, false, false, false, "init", ident, value,
)
} }
ObjectPatProp::Rest(rest_pat) => { ObjectPatProp::Rest(rest_pat) => {
serialize_pat(ctx, &Pat::Rest(rest_pat.clone()), parent) serialize_pat(ctx, &Pat::Rest(rest_pat.clone()), parent)
@ -2104,11 +2009,7 @@ fn serialize_pat(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_bool(opt_pos, node.optional); ctx.write_obj_pat(pos, node.optional, type_ann, children)
ctx.write_maybe_ref(type_ann_pos, type_ann);
ctx.write_refs(props_pos, children);
pos
} }
Pat::Assign(node) => { Pat::Assign(node) => {
let pos = ctx.alloc_assign_pat(parent, &node.span); let pos = ctx.alloc_assign_pat(parent, &node.span);
@ -2188,10 +2089,7 @@ fn serialize_lit(
) -> NodeRef { ) -> NodeRef {
match lit { match lit {
Lit::Str(node) => { Lit::Str(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_str_lit(parent, &node.span);
let value_pos = ctx.str_field(AstProp::Value);
let raw_pos = ctx.str_field(AstProp::Raw);
let pos = ctx.commit_schema(raw);
let raw_value = if let Some(v) = &node.raw { let raw_value = if let Some(v) = &node.raw {
v.to_string() v.to_string()
@ -2199,37 +2097,18 @@ fn serialize_lit(
format!("{}", node.value).to_string() format!("{}", node.value).to_string()
}; };
ctx.write_str(value_pos, &node.value); ctx.write_str_lit(pos, &node.value, &raw_value)
ctx.write_str(raw_pos, &raw_value);
pos
} }
Lit::Bool(node) => { Lit::Bool(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_bool_lit(parent, &node.span);
let value_pos = ctx.bool_field(AstProp::Value); ctx.write_bool_lit(pos, node.value, &format!("{}", node.value))
let raw_pos = ctx.str_field(AstProp::Raw);
let pos = ctx.commit_schema(raw);
ctx.write_bool(value_pos, node.value);
ctx.write_str(raw_pos, &format!("{}", node.value));
pos
} }
Lit::Null(node) => { Lit::Null(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_null_lit(parent, &node.span);
ctx.null_field(AstProp::Value); ctx.write_null_lit(pos)
let raw_pos = ctx.str_field(AstProp::Raw);
let pos = ctx.commit_schema(raw);
ctx.write_str(raw_pos, "null");
pos
} }
Lit::Num(node) => { Lit::Num(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_num_lit(parent, &node.span);
let value_pos = ctx.num_field(AstProp::Value);
let raw_pos = ctx.str_field(AstProp::Raw);
let pos = ctx.commit_schema(raw);
let raw_value = if let Some(v) = &node.raw { let raw_value = if let Some(v) = &node.raw {
v.to_string() v.to_string()
@ -2238,17 +2117,10 @@ fn serialize_lit(
}; };
let value = node.raw.as_ref().unwrap(); let value = node.raw.as_ref().unwrap();
ctx.write_num(value_pos, value); ctx.write_num_lit(pos, value, &raw_value)
ctx.write_str(raw_pos, &raw_value);
pos
} }
Lit::BigInt(node) => { Lit::BigInt(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_bigint_lit(parent, &node.span);
let value_pos = ctx.str_field(AstProp::Value);
let raw_pos = ctx.str_field(AstProp::Raw);
let bigint_pos = ctx.str_field(AstProp::BigInt);
let pos = ctx.commit_schema(raw);
let raw_bigint_value = if let Some(v) = &node.raw { let raw_bigint_value = if let Some(v) = &node.raw {
let mut s = v.to_string(); let mut s = v.to_string();
@ -2264,35 +2136,29 @@ fn serialize_lit(
format!("{}", node.value).to_string() format!("{}", node.value).to_string()
}; };
// FIXME ctx.write_bigint_lit(
pos,
ctx.write_str(value_pos, &node.value.to_string()); &node.value.to_string(),
ctx.write_str(bigint_pos, &raw_bigint_value); &raw_value,
ctx.write_str(raw_pos, &raw_value); &raw_bigint_value,
)
pos
} }
Lit::Regex(node) => { Lit::Regex(node) => {
let raw = ctx.header(AstNode::Literal, parent, &node.span); let pos = ctx.alloc_regex_lit(parent, &node.span);
let _obj_pos = ctx.obj_field(AstProp::Regex, 2);
let flags_pos = ctx.str_field(AstProp::Flags);
let pattern_pos = ctx.str_field(AstProp::Pattern);
let raw_pos = ctx.str_field(AstProp::Raw);
let value_pos = ctx.regex_field(AstProp::Value);
let pos = ctx.commit_schema(raw);
let raw = format!("/{}/{}", node.exp.as_str(), node.flags.as_str()); let raw = format!("/{}/{}", node.exp.as_str(), node.flags.as_str());
ctx.write_str(pattern_pos, node.exp.as_str()); ctx.write_regex_lit(
ctx.write_str(flags_pos, node.flags.as_str()); pos,
ctx.write_str(raw_pos, &raw); node.exp.as_str(),
ctx.write_regex(value_pos, &raw); node.flags.as_str(),
&raw,
pos &raw,
)
} }
Lit::JSXText(jsxtext) => { Lit::JSXText(jsxtext) => {
let raw = ctx.header(AstNode::JSXText, parent, &jsxtext.span); let pos = ctx.alloc_jsx_text(parent, &jsxtext.span);
ctx.commit_schema(raw) ctx.write_jsx_text(pos, &jsxtext.raw, &jsxtext.value)
} }
} }
} }
@ -2326,28 +2192,24 @@ fn serialize_ts_type(
match node { match node {
TsType::TsKeywordType(node) => { TsType::TsKeywordType(node) => {
let kind = match node.kind { let kind = match node.kind {
TsKeywordTypeKind::TsAnyKeyword => AstNode::TSAnyKeyword, TsKeywordTypeKind::TsAnyKeyword => TsKeywordKind::Any,
TsKeywordTypeKind::TsUnknownKeyword => AstNode::TSUnknownKeyword, TsKeywordTypeKind::TsUnknownKeyword => TsKeywordKind::Unknown,
TsKeywordTypeKind::TsNumberKeyword => AstNode::TSNumberKeyword, TsKeywordTypeKind::TsNumberKeyword => TsKeywordKind::Number,
TsKeywordTypeKind::TsObjectKeyword => AstNode::TSObjectKeyword, TsKeywordTypeKind::TsObjectKeyword => TsKeywordKind::Object,
TsKeywordTypeKind::TsBooleanKeyword => AstNode::TSBooleanKeyword, TsKeywordTypeKind::TsBooleanKeyword => TsKeywordKind::Boolean,
TsKeywordTypeKind::TsBigIntKeyword => AstNode::TSBigIntKeyword, TsKeywordTypeKind::TsBigIntKeyword => TsKeywordKind::BigInt,
TsKeywordTypeKind::TsStringKeyword => AstNode::TSStringKeyword, TsKeywordTypeKind::TsStringKeyword => TsKeywordKind::String,
TsKeywordTypeKind::TsSymbolKeyword => AstNode::TSSymbolKeyword, TsKeywordTypeKind::TsSymbolKeyword => TsKeywordKind::Symbol,
TsKeywordTypeKind::TsVoidKeyword => AstNode::TSVoidKeyword, TsKeywordTypeKind::TsVoidKeyword => TsKeywordKind::Void,
TsKeywordTypeKind::TsUndefinedKeyword => AstNode::TSUndefinedKeyword, TsKeywordTypeKind::TsUndefinedKeyword => TsKeywordKind::Undefined,
TsKeywordTypeKind::TsNullKeyword => AstNode::TSNullKeyword, TsKeywordTypeKind::TsNullKeyword => TsKeywordKind::Null,
TsKeywordTypeKind::TsNeverKeyword => AstNode::TSNeverKeyword, TsKeywordTypeKind::TsNeverKeyword => TsKeywordKind::Never,
TsKeywordTypeKind::TsIntrinsicKeyword => AstNode::TSIntrinsicKeyword, TsKeywordTypeKind::TsIntrinsicKeyword => TsKeywordKind::Intrinsic,
}; };
let raw = ctx.header(kind, parent, &node.span); ctx.alloc_ts_keyword(kind, parent, &node.span)
ctx.commit_schema(raw)
}
TsType::TsThisType(node) => {
let raw = ctx.header(AstNode::TSThisType, parent, &node.span);
ctx.commit_schema(raw)
} }
TsType::TsThisType(node) => ctx.alloc_ts_this_type(parent, &node.span),
TsType::TsFnOrConstructorType(node) => match node { TsType::TsFnOrConstructorType(node) => match node {
TsFnOrConstructorType::TsFnType(node) => { TsFnOrConstructorType::TsFnType(node) => {
let raw = ctx.header(AstNode::TSFunctionType, parent, &node.span); let raw = ctx.header(AstNode::TSFunctionType, parent, &node.span);
@ -2369,10 +2231,7 @@ fn serialize_ts_type(
} }
}, },
TsType::TsTypeRef(node) => { TsType::TsTypeRef(node) => {
let raw = ctx.header(AstNode::TSTypeReference, parent, &node.span); let pos = ctx.alloc_ts_type_ref(parent, &node.span);
let name_pos = ctx.ref_field(AstProp::TypeName);
let type_args_pos = ctx.ref_field(AstProp::TypeArguments);
let pos = ctx.commit_schema(raw);
let name = serialize_ts_entity_name(ctx, &node.type_name, pos); let name = serialize_ts_entity_name(ctx, &node.type_name, pos);
@ -2381,16 +2240,10 @@ fn serialize_ts_type(
.clone() .clone()
.map(|param| serialize_ts_param_inst(ctx, &param, pos)); .map(|param| serialize_ts_param_inst(ctx, &param, pos));
ctx.write_ref(name_pos, name); ctx.write_ts_type_ref(pos, name, type_args)
ctx.write_maybe_ref(type_args_pos, type_args);
pos
} }
TsType::TsTypeQuery(node) => { TsType::TsTypeQuery(node) => {
let raw = ctx.header(AstNode::TSTypeQuery, parent, &node.span); let pos = ctx.alloc_ts_type_query(parent, &node.span);
let name_pos = ctx.ref_field(AstProp::ExprName);
let type_args_pos = ctx.ref_field(AstProp::TypeArguments);
let pos = ctx.commit_schema(raw);
let expr_name = match &node.expr_name { let expr_name = match &node.expr_name {
TsTypeQueryExpr::TsEntityName(entity) => { TsTypeQueryExpr::TsEntityName(entity) => {
@ -2406,76 +2259,48 @@ fn serialize_ts_type(
.clone() .clone()
.map(|param| serialize_ts_param_inst(ctx, &param, pos)); .map(|param| serialize_ts_param_inst(ctx, &param, pos));
ctx.write_ref(name_pos, expr_name); ctx.write_ts_type_query(pos, expr_name, type_args)
ctx.write_maybe_ref(type_args_pos, type_args);
pos
} }
TsType::TsTypeLit(_) => { TsType::TsTypeLit(_) => {
// TODO: Not sure what this is // TODO: Not sure what this is
todo!() todo!()
} }
TsType::TsArrayType(node) => { TsType::TsArrayType(node) => {
let raw = ctx.header(AstNode::TSArrayType, parent, &node.span); let pos = ctx.alloc_ts_array_type(parent, &node.span);
let elem_pos = ctx.ref_field(AstProp::ElementType);
let pos = ctx.commit_schema(raw);
let elem = serialize_ts_type(ctx, &node.elem_type, pos); let elem = serialize_ts_type(ctx, &node.elem_type, pos);
ctx.write_ts_array_type(pos, elem)
ctx.write_ref(elem_pos, elem);
pos
} }
TsType::TsTupleType(node) => { TsType::TsTupleType(node) => {
let raw = ctx.header(AstNode::TSTupleType, parent, &node.span); let pos = ctx.alloc_ts_tuple_type(parent, &node.span);
let children_pos =
ctx.ref_vec_field(AstProp::ElementTypes, node.elem_types.len());
let pos = ctx.commit_schema(raw);
let children = node let children = node
.elem_types .elem_types
.iter() .iter()
.map(|elem| { .map(|elem| {
if let Some(label) = &elem.label { if let Some(label) = &elem.label {
let raw = ctx.header(AstNode::TSNamedTupleMember, pos, &elem.span); let child_pos = ctx.alloc_ts_named_tuple_member(pos, &elem.span);
let label_pos = ctx.ref_field(AstProp::Label);
let type_pos = ctx.ref_field(AstProp::ElementType);
let child_pos = ctx.commit_schema(raw);
let label_id = serialize_pat(ctx, label, child_pos); let label = serialize_pat(ctx, label, child_pos);
let type_id = serialize_ts_type(ctx, elem.ty.as_ref(), child_pos); let type_id = serialize_ts_type(ctx, elem.ty.as_ref(), child_pos);
ctx.write_ref(label_pos, label_id); ctx.write_ts_named_tuple_member(pos, label, type_id)
ctx.write_ref(type_pos, type_id);
child_pos
} else { } else {
serialize_ts_type(ctx, elem.ty.as_ref(), pos) serialize_ts_type(ctx, elem.ty.as_ref(), pos)
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_refs(children_pos, children); ctx.write_ts_tuple_type(pos, children)
pos
} }
TsType::TsOptionalType(_) => todo!(), TsType::TsOptionalType(_) => todo!(),
TsType::TsRestType(node) => { TsType::TsRestType(node) => {
let raw = ctx.header(AstNode::TSRestType, parent, &node.span); let pos = ctx.alloc_ts_rest_type(parent, &node.span);
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let type_ann = serialize_ts_type(ctx, &node.type_ann, pos); let type_ann = serialize_ts_type(ctx, &node.type_ann, pos);
ctx.write_ts_rest_type(pos, type_ann)
ctx.write_ref(type_ann_pos, type_ann);
pos
} }
TsType::TsUnionOrIntersectionType(node) => match node { TsType::TsUnionOrIntersectionType(node) => match node {
TsUnionOrIntersectionType::TsUnionType(node) => { TsUnionOrIntersectionType::TsUnionType(node) => {
let raw = ctx.header(AstNode::TSUnionType, parent, &node.span); let pos = ctx.alloc_ts_union_type(parent, &node.span);
let types_pos = ctx.ref_vec_field(AstProp::Types, node.types.len());
let pos = ctx.commit_schema(raw);
let children = node let children = node
.types .types
@ -2483,14 +2308,10 @@ fn serialize_ts_type(
.map(|item| serialize_ts_type(ctx, item, pos)) .map(|item| serialize_ts_type(ctx, item, pos))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_refs(types_pos, children); ctx.write_ts_union_type(pos, children)
pos
} }
TsUnionOrIntersectionType::TsIntersectionType(node) => { TsUnionOrIntersectionType::TsIntersectionType(node) => {
let raw = ctx.header(AstNode::TSIntersectionType, parent, &node.span); let pos = ctx.alloc_ts_intersection_type(parent, &node.span);
let types_pos = ctx.ref_vec_field(AstProp::Types, node.types.len());
let pos = ctx.commit_schema(raw);
let children = node let children = node
.types .types
@ -2498,100 +2319,63 @@ fn serialize_ts_type(
.map(|item| serialize_ts_type(ctx, item, pos)) .map(|item| serialize_ts_type(ctx, item, pos))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ctx.write_refs(types_pos, children); ctx.write_ts_intersection_type(pos, children)
pos
} }
}, },
TsType::TsConditionalType(node) => { TsType::TsConditionalType(node) => {
let raw = ctx.header(AstNode::TSConditionalType, parent, &node.span); let pos = ctx.alloc_ts_conditional_type(parent, &node.span);
let check_pos = ctx.ref_field(AstProp::CheckType);
let extends_pos = ctx.ref_field(AstProp::ExtendsType);
let true_pos = ctx.ref_field(AstProp::TrueType);
let false_pos = ctx.ref_field(AstProp::FalseType);
let pos = ctx.commit_schema(raw);
let check = serialize_ts_type(ctx, &node.check_type, pos); let check = serialize_ts_type(ctx, &node.check_type, pos);
let extends = serialize_ts_type(ctx, &node.extends_type, pos); let extends = serialize_ts_type(ctx, &node.extends_type, pos);
let v_true = serialize_ts_type(ctx, &node.true_type, pos); let v_true = serialize_ts_type(ctx, &node.true_type, pos);
let v_false = serialize_ts_type(ctx, &node.false_type, pos); let v_false = serialize_ts_type(ctx, &node.false_type, pos);
ctx.write_ref(check_pos, check); ctx.write_ts_conditional_type(pos, check, extends, v_true, v_false)
ctx.write_ref(extends_pos, extends);
ctx.write_ref(true_pos, v_true);
ctx.write_ref(false_pos, v_false);
pos
} }
TsType::TsInferType(node) => { TsType::TsInferType(node) => {
let raw = ctx.header(AstNode::TSInferType, parent, &node.span); let pos = ctx.alloc_ts_infer_type(parent, &node.span);
let param_pos = ctx.ref_field(AstProp::TypeParameter);
let pos = ctx.commit_schema(raw);
let param = serialize_ts_type_param(ctx, &node.type_param, parent); let param = serialize_ts_type_param(ctx, &node.type_param, parent);
ctx.write_ts_infer_type(pos, param)
ctx.write_ref(param_pos, param);
pos
} }
TsType::TsParenthesizedType(_) => todo!(), TsType::TsParenthesizedType(_) => todo!(),
TsType::TsTypeOperator(node) => { TsType::TsTypeOperator(node) => {
let raw = ctx.header(AstNode::TSTypeOperator, parent, &node.span); let pos = ctx.alloc_ts_type_op(parent, &node.span);
let operator_pos = ctx.str_field(AstProp::Operator);
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let type_ann = serialize_ts_type(ctx, &node.type_ann, pos); let type_ann = serialize_ts_type(ctx, &node.type_ann, pos);
ctx.write_str( let op = match node.op {
operator_pos, TsTypeOperatorOp::KeyOf => "keyof",
match node.op { TsTypeOperatorOp::Unique => "unique",
TsTypeOperatorOp::KeyOf => "keyof", TsTypeOperatorOp::ReadOnly => "readonly",
TsTypeOperatorOp::Unique => "unique", };
TsTypeOperatorOp::ReadOnly => "readonly",
},
);
ctx.write_ref(type_ann_pos, type_ann);
pos ctx.write_ts_type_op(pos, op, type_ann)
} }
TsType::TsIndexedAccessType(node) => { TsType::TsIndexedAccessType(node) => {
let raw = ctx.header(AstNode::TSIndexedAccessType, parent, &node.span); let pos = ctx.alloc_ts_indexed_access_type(parent, &node.span);
let index_type_pos = ctx.ref_field(AstProp::IndexType);
let obj_type_pos = ctx.ref_field(AstProp::ObjectType);
let pos = ctx.commit_schema(raw);
let index = serialize_ts_type(ctx, &node.index_type, pos); let index = serialize_ts_type(ctx, &node.index_type, pos);
let obj = serialize_ts_type(ctx, &node.obj_type, pos); let obj = serialize_ts_type(ctx, &node.obj_type, pos);
ctx.write_ref(index_type_pos, index); ctx.write_ts_indexed_access_type(pos, index, obj)
ctx.write_ref(obj_type_pos, obj);
pos
} }
TsType::TsMappedType(node) => { TsType::TsMappedType(node) => {
let raw = ctx.header(AstNode::TSMappedType, parent, &node.span); let pos = ctx.alloc_ts_mapped_type(parent, &node.span);
let name_pos = ctx.ref_field(AstProp::NameType);
let type_ann_pos = ctx.ref_field(AstProp::TypeAnnotation);
let type_param_pos = ctx.ref_field(AstProp::TypeParameter);
let pos = ctx.commit_schema(raw);
let opt_pos = let opt_pos =
create_true_plus_minus_field(ctx, AstProp::Optional, node.optional); create_true_plus_minus_field(ctx, AstProp::Optional, node.optional);
let readonly_pos = let readonly_pos =
create_true_plus_minus_field(ctx, AstProp::Readonly, node.readonly); create_true_plus_minus_field(ctx, AstProp::Readonly, node.readonly);
let name_id = maybe_serialize_ts_type(ctx, &node.name_type, pos); let name = maybe_serialize_ts_type(ctx, &node.name_type, pos);
let type_ann = maybe_serialize_ts_type(ctx, &node.type_ann, pos); let type_ann = maybe_serialize_ts_type(ctx, &node.type_ann, pos);
let type_param = serialize_ts_type_param(ctx, &node.type_param, pos); let type_param = serialize_ts_type_param(ctx, &node.type_param, pos);
// FIXME: true plus minus
write_true_plus_minus(ctx, opt_pos, node.optional); write_true_plus_minus(ctx, opt_pos, node.optional);
write_true_plus_minus(ctx, readonly_pos, node.readonly); write_true_plus_minus(ctx, readonly_pos, node.readonly);
ctx.write_maybe_ref(name_pos, name_id);
ctx.write_maybe_ref(type_ann_pos, type_ann);
ctx.write_ref(type_param_pos, type_param);
pos ctx.write_ts_mapped_type(pos, name, type_ann, type_param)
} }
TsType::TsLitType(node) => serialize_ts_lit_type(ctx, node, parent), TsType::TsLitType(node) => serialize_ts_lit_type(ctx, node, parent),
TsType::TsTypePredicate(node) => { TsType::TsTypePredicate(node) => {
@ -2603,8 +2387,7 @@ fn serialize_ts_type(
let param_name = match &node.param_name { let param_name = match &node.param_name {
TsThisTypeOrIdent::TsThisType(ts_this_type) => { TsThisTypeOrIdent::TsThisType(ts_this_type) => {
let raw = ctx.header(AstNode::TSThisType, pos, &ts_this_type.span); ctx.alloc_ts_this_type(pos, &ts_this_type.span)
ctx.commit_schema(raw)
} }
TsThisTypeOrIdent::Ident(ident) => serialize_ident(ctx, ident, pos), TsThisTypeOrIdent::Ident(ident) => serialize_ident(ctx, ident, pos),
}; };
@ -2655,9 +2438,7 @@ fn serialize_ts_lit_type(
node: &TsLitType, node: &TsLitType,
parent: NodeRef, parent: NodeRef,
) -> NodeRef { ) -> NodeRef {
let raw = ctx.header(AstNode::TSLiteralType, parent, &node.span); let pos = ctx.alloc_ts_lit_type(parent, &node.span);
let lit_pos = ctx.ref_field(AstProp::Literal);
let pos = ctx.commit_schema(raw);
let lit = match &node.lit { let lit = match &node.lit {
TsLit::Number(lit) => serialize_lit(ctx, &Lit::Num(lit.clone()), pos), TsLit::Number(lit) => serialize_lit(ctx, &Lit::Num(lit.clone()), pos),
@ -2675,9 +2456,7 @@ fn serialize_ts_lit_type(
), ),
}; };
ctx.write_ref(lit_pos, lit); ctx.write_ts_lit_type(pos, lit)
pos
} }
fn create_true_plus_minus_field( fn create_true_plus_minus_field(
@ -2760,15 +2539,9 @@ fn serialize_ts_type_ann(
node: &TsTypeAnn, node: &TsTypeAnn,
parent: NodeRef, parent: NodeRef,
) -> NodeRef { ) -> NodeRef {
let raw = ctx.header(AstNode::TSTypeAnnotation, parent, &node.span); let pos = ctx.alloc_ts_type_ann(parent, &node.span);
let type_pos = ctx.ref_field(AstProp::TypeAnnotation);
let pos = ctx.commit_schema(raw);
let v_type = serialize_ts_type(ctx, &node.type_ann, pos); let v_type = serialize_ts_type(ctx, &node.type_ann, pos);
ctx.write_ts_type_ann(pos, v_type)
ctx.write_ref(type_pos, v_type);
pos
} }
fn maybe_serialize_ts_type( fn maybe_serialize_ts_type(

File diff suppressed because it is too large Load diff