Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ pub struct TSCallSignatureDeclaration<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[estree(skip)]
pub this_param: Option<TSThisParameter<'a>>,
#[estree(via = TSCallSignatureDeclarationFormalParameters)]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
}
Expand Down Expand Up @@ -1056,6 +1057,7 @@ pub struct TSMethodSignature<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[estree(skip)]
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
#[estree(via = TSMethodSignatureFormalParameters)]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
Expand Down Expand Up @@ -1366,6 +1368,7 @@ pub struct TSFunctionType<'a> {
#[estree(skip)]
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
/// Function parameters. Akin to [`Function::params`].
#[estree(via = TSFunctionTypeFormalParameters)]
pub params: Box<'a, FormalParameters<'a>>,
/// Return type of the function.
/// ```ts
Expand Down
9 changes: 6 additions & 3 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2930,7 +2930,10 @@ impl ESTree for TSCallSignatureDeclaration<'_> {
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("typeParameters", &self.type_parameters);
state.serialize_field("params", &self.params);
state.serialize_field(
"params",
&crate::serialize::TSCallSignatureDeclarationFormalParameters(self),
);
state.serialize_field("returnType", &self.return_type);
state.end();
}
Expand All @@ -2957,7 +2960,7 @@ impl ESTree for TSMethodSignature<'_> {
state.serialize_field("optional", &self.optional);
state.serialize_field("kind", &self.kind);
state.serialize_field("typeParameters", &self.type_parameters);
state.serialize_field("params", &self.params);
state.serialize_field("params", &crate::serialize::TSMethodSignatureFormalParameters(self));
state.serialize_field("returnType", &self.return_type);
state.serialize_field("accessibility", &crate::serialize::Null(self));
state.serialize_field("readonly", &crate::serialize::False(self));
Expand Down Expand Up @@ -3149,7 +3152,7 @@ impl ESTree for TSFunctionType<'_> {
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("typeParameters", &self.type_parameters);
state.serialize_field("params", &self.params);
state.serialize_field("params", &crate::serialize::TSFunctionTypeFormalParameters(self));
state.serialize_field("returnType", &self.return_type);
state.end();
}
Expand Down
218 changes: 156 additions & 62 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,45 +634,6 @@ impl ESTree for FormalParametersRest<'_, '_> {
}
}

/// Serializer for `params` field of `Function`.
///
/// In TS AST, this adds `this_param` to start of the array.
#[ast_meta]
#[estree(
ts_type = "ParamPattern[]",
raw_deser = "
const params = DESER[Box<FormalParameters>](POS_OFFSET.params);
/* IF_TS */
const thisParam = DESER[Option<Box<TSThisParameter>>](POS_OFFSET.this_param)
if (thisParam !== null) params.unshift(thisParam);
/* END_IF_TS */
params
"
)]
pub struct FunctionFormalParameters<'a, 'b>(pub &'b Function<'a>);

impl ESTree for FunctionFormalParameters<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut seq = serializer.serialize_sequence();

if S::INCLUDE_TS_FIELDS {
if let Some(this_param) = &self.0.this_param {
seq.serialize_element(this_param);
}
}

for item in &self.0.params.items {
seq.serialize_element(item);
}

if let Some(rest) = &self.0.params.rest {
seq.serialize_element(&FormalParametersRest(rest));
}

seq.end();
}
}

/// Serializer for `key` field of `MethodDefinition`.
///
/// In TS-ESTree `"constructor"` in `class C { "constructor"() {} }`
Expand Down Expand Up @@ -719,29 +680,6 @@ impl ESTree for MethodDefinitionKey<'_, '_> {
}
}

/// Serializer for `extends` field of `TSInterfaceDeclaration`.
///
/// Serialize `extends` as an empty array if it's `None`.
#[ast_meta]
#[estree(
ts_type = "Array<TSInterfaceHeritage>",
raw_deser = "
const extendsArr = DESER[Option<Vec<TSInterfaceHeritage>>](POS_OFFSET.extends);
extendsArr === null ? [] : extendsArr
"
)]
pub struct TSInterfaceDeclarationExtends<'a, 'b>(pub &'b TSInterfaceDeclaration<'a>);

impl ESTree for TSInterfaceDeclarationExtends<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
if let Some(extends) = &self.0.extends {
extends.serialize(serializer);
} else {
[(); 0].serialize(serializer);
}
}
}

/// Serializer for `specifiers` field of `ImportDeclaration`.
///
/// Serialize `specifiers` as an empty array if it's `None`.
Expand Down Expand Up @@ -1127,6 +1065,162 @@ impl ESTree for TSMappedTypeModifierOperatorConverter<'_> {
}
}

/// Serializer for `params` field of `Function`.
///
/// In TS AST, this adds `this_param` to start of the `params` array.
#[ast_meta]
#[estree(
ts_type = "ParamPattern[]",
raw_deser = "
const params = DESER[Box<FormalParameters>](POS_OFFSET.params);
/* IF_TS */
const thisParam = DESER[Option<Box<TSThisParameter>>](POS_OFFSET.this_param)
if (thisParam !== null) params.unshift(thisParam);
/* END_IF_TS */
params
"
)]
pub struct FunctionFormalParameters<'a, 'b>(pub &'b Function<'a>);

impl ESTree for FunctionFormalParameters<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut seq = serializer.serialize_sequence();

if S::INCLUDE_TS_FIELDS {
if let Some(this_param) = &self.0.this_param {
seq.serialize_element(this_param);
}
}

for item in &self.0.params.items {
seq.serialize_element(item);
}

if let Some(rest) = &self.0.params.rest {
seq.serialize_element(&FormalParametersRest(rest));
}

seq.end();
}
}

/// Serializer for `params` field of `TSCallSignatureDeclaration`.
///
/// These add `this_param` to start of the `params` array.
#[ast_meta]
#[estree(
ts_type = "ParamPattern[]",
raw_deser = "
const params = DESER[Box<FormalParameters>](POS_OFFSET.params);
const thisParam = DESER[Option<Box<TSThisParameter>>](POS_OFFSET.this_param)
if (thisParam !== null) params.unshift(thisParam);
params
"
)]
pub struct TSCallSignatureDeclarationFormalParameters<'a, 'b>(
pub &'b TSCallSignatureDeclaration<'a>,
);

impl ESTree for TSCallSignatureDeclarationFormalParameters<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let v = self.0;
serialize_formal_params_with_this_param(v.this_param.as_ref(), &v.params, serializer);
}
}

/// Serializer for `params` field of `TSMethodSignature`.
///
/// These add `this_param` to start of the `params` array.
#[ast_meta]
#[estree(
ts_type = "ParamPattern[]",
raw_deser = "
const params = DESER[Box<FormalParameters>](POS_OFFSET.params);
const thisParam = DESER[Option<Box<TSThisParameter>>](POS_OFFSET.this_param)
if (thisParam !== null) params.unshift(thisParam);
params
"
)]
pub struct TSMethodSignatureFormalParameters<'a, 'b>(pub &'b TSMethodSignature<'a>);

impl ESTree for TSMethodSignatureFormalParameters<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let v = self.0;
serialize_formal_params_with_this_param(v.this_param.as_deref(), &v.params, serializer);
}
}

/// Serializer for `params` field of `TSFunctionType`.
///
/// These add `this_param` to start of the `params` array.
#[ast_meta]
#[estree(
ts_type = "ParamPattern[]",
raw_deser = "
const params = DESER[Box<FormalParameters>](POS_OFFSET.params);
const thisParam = DESER[Option<Box<TSThisParameter>>](POS_OFFSET.this_param)
if (thisParam !== null) params.unshift(thisParam);
params
"
)]
pub struct TSFunctionTypeFormalParameters<'a, 'b>(pub &'b TSFunctionType<'a>);

impl ESTree for TSFunctionTypeFormalParameters<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let v = self.0;
serialize_formal_params_with_this_param(v.this_param.as_deref(), &v.params, serializer);
}
}

/// Shared serialization logic used by:
/// - `TSCallSignatureDeclarationFormalParameters`
/// - `TSMethodSignatureFormalParameters`
/// - `TSFunctionTypeFormalParameters`
fn serialize_formal_params_with_this_param<'a, S: Serializer>(
this_param: Option<&TSThisParameter<'a>>,
params: &FormalParameters<'a>,
serializer: S,
) {
let mut seq = serializer.serialize_sequence();

if let Some(this_param) = this_param {
seq.serialize_element(this_param);
}

for item in &params.items {
seq.serialize_element(item);
}

if let Some(rest) = &params.rest {
seq.serialize_element(&FormalParametersRest(rest));
}

seq.end();
}

/// Serializer for `extends` field of `TSInterfaceDeclaration`.
///
/// Serialize `extends` as an empty array if it's `None`.
#[ast_meta]
#[estree(
ts_type = "Array<TSInterfaceHeritage>",
raw_deser = "
const extendsArr = DESER[Option<Vec<TSInterfaceHeritage>>](POS_OFFSET.extends);
extendsArr === null ? [] : extendsArr
"
)]
pub struct TSInterfaceDeclarationExtends<'a, 'b>(pub &'b TSInterfaceDeclaration<'a>);

impl ESTree for TSInterfaceDeclarationExtends<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
if let Some(extends) = &self.0.extends {
extends.serialize(serializer);
} else {
EmptyArray(()).serialize(serializer);
}
}
}

// --------------------
// Comments
// --------------------
Expand Down
15 changes: 12 additions & 3 deletions napi/parser/deserialize-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,17 +1685,23 @@ function deserializeTSIndexSignature(pos) {
}

function deserializeTSCallSignatureDeclaration(pos) {
const params = deserializeBoxFormalParameters(pos + 48);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 16);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSCallSignatureDeclaration',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 8),
params: deserializeBoxFormalParameters(pos + 48),
params,
returnType: deserializeOptionBoxTSTypeAnnotation(pos + 56),
};
}

function deserializeTSMethodSignature(pos) {
const params = deserializeBoxFormalParameters(pos + 48);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 40);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSMethodSignature',
start: deserializeU32(pos),
Expand All @@ -1705,7 +1711,7 @@ function deserializeTSMethodSignature(pos) {
optional: deserializeBool(pos + 25),
kind: deserializeTSMethodSignatureKind(pos + 26),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 32),
params: deserializeBoxFormalParameters(pos + 48),
params,
returnType: deserializeOptionBoxTSTypeAnnotation(pos + 56),
accessibility: null,
readonly: false,
Expand Down Expand Up @@ -1823,12 +1829,15 @@ function deserializeTSImportType(pos) {
}

function deserializeTSFunctionType(pos) {
const params = deserializeBoxFormalParameters(pos + 24);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 16);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSFunctionType',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 8),
params: deserializeBoxFormalParameters(pos + 24),
params,
returnType: deserializeBoxTSTypeAnnotation(pos + 32),
};
}
Expand Down
15 changes: 12 additions & 3 deletions napi/parser/deserialize-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1804,17 +1804,23 @@ function deserializeTSIndexSignature(pos) {
}

function deserializeTSCallSignatureDeclaration(pos) {
const params = deserializeBoxFormalParameters(pos + 48);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 16);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSCallSignatureDeclaration',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 8),
params: deserializeBoxFormalParameters(pos + 48),
params,
returnType: deserializeOptionBoxTSTypeAnnotation(pos + 56),
};
}

function deserializeTSMethodSignature(pos) {
const params = deserializeBoxFormalParameters(pos + 48);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 40);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSMethodSignature',
start: deserializeU32(pos),
Expand All @@ -1824,7 +1830,7 @@ function deserializeTSMethodSignature(pos) {
optional: deserializeBool(pos + 25),
kind: deserializeTSMethodSignatureKind(pos + 26),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 32),
params: deserializeBoxFormalParameters(pos + 48),
params,
returnType: deserializeOptionBoxTSTypeAnnotation(pos + 56),
accessibility: null,
readonly: false,
Expand Down Expand Up @@ -1942,12 +1948,15 @@ function deserializeTSImportType(pos) {
}

function deserializeTSFunctionType(pos) {
const params = deserializeBoxFormalParameters(pos + 24);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 16);
if (thisParam !== null) params.unshift(thisParam);
return {
type: 'TSFunctionType',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 8),
params: deserializeBoxFormalParameters(pos + 24),
params,
returnType: deserializeBoxTSTypeAnnotation(pos + 32),
};
}
Expand Down
Loading
Loading