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
4 changes: 3 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ pub struct BindingRestElement<'a> {
add_fields(expression = False),
field_order(
r#type, span, id, expression, generator, r#async, params, body,
declare, type_parameters, this_param, return_type,
declare, type_parameters, return_type,
),
)]
pub struct Function<'a> {
Expand Down Expand Up @@ -1695,10 +1695,12 @@ pub struct Function<'a> {
/// });
/// ```
#[ts]
#[estree(skip)]
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
/// Function parameters.
///
/// Does not include `this` parameters used by some TypeScript functions.
#[estree(via = FunctionFormalParameters)]
pub params: Box<'a, FormalParameters<'a>>,
/// The TypeScript return type annotation.
#[ts]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ use super::{inherit_variants, js::*, literal::*};
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(
rename = "Identifier",
add_fields(decorators = TsEmptyArray, optional = TsFalse, name = This)
)]
pub struct TSThisParameter<'a> {
pub span: Span,
#[estree(skip)]
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,11 +1345,10 @@ impl ESTree for Function<'_> {
state.serialize_field("expression", &crate::serialize::False(self));
state.serialize_field("generator", &self.generator);
state.serialize_field("async", &self.r#async);
state.serialize_field("params", &self.params);
state.serialize_field("params", &crate::serialize::FunctionFormalParameters(self));
state.serialize_field("body", &self.body);
state.serialize_ts_field("declare", &self.declare);
state.serialize_ts_field("typeParameters", &self.type_parameters);
state.serialize_ts_field("thisParam", &self.this_param);
state.serialize_ts_field("returnType", &self.return_type);
state.end();
}
Expand Down Expand Up @@ -2278,10 +2277,13 @@ impl ESTree for JSXText<'_> {
impl ESTree for TSThisParameter<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut state = serializer.serialize_struct();
state.serialize_field("type", &JsonSafeString("TSThisParameter"));
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("typeAnnotation", &self.type_annotation);
state.serialize_field("name", &crate::serialize::This(self));
state.serialize_ts_field("decorators", &crate::serialize::TsEmptyArray(self));
state.serialize_ts_field("optional", &crate::serialize::TsFalse(self));
state.end();
}
}
Expand Down
47 changes: 47 additions & 0 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ impl<T> ESTree for Init<'_, T> {
}
}

/// Serialized as `"this"`.
#[ast_meta]
#[estree(ts_type = "'this'", raw_deser = "'this'")]
pub struct This<'b, T>(#[expect(dead_code)] pub &'b T);

impl<T> ESTree for This<'_, T> {
fn serialize<S: Serializer>(&self, serializer: S) {
JsonSafeString("this").serialize(serializer);
}
}

#[ast_meta]
#[estree(ts_type = "[]", raw_deser = "[]")]
#[ts]
Expand Down Expand Up @@ -642,6 +653,42 @@ impl ESTree for ExpressionStatementDirective<'_, '_> {
}
}

#[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 let Some(this_param) = &self.0.this_param {
seq.serialize_ts_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();
}
}

// --------------------
// Comments
// --------------------
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_estree/src/serialize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{Config, ESTree, ESTreeSerializer, Formatter, Serializer, SerializerP
pub trait SequenceSerializer {
/// Serialize sequence entry.
fn serialize_element<T: ESTree + ?Sized>(&mut self, value: &T);
fn serialize_ts_element<T: ESTree + ?Sized>(&mut self, value: &T);

/// Finish serializing sequence.
fn end(self);
Expand Down Expand Up @@ -43,6 +44,12 @@ impl<C: Config, F: Formatter> SequenceSerializer for ESTreeSequenceSerializer<'_
value.serialize(&mut *self.serializer);
}

fn serialize_ts_element<T: ESTree + ?Sized>(&mut self, value: &T) {
if C::INCLUDE_TS_FIELDS {
self.serialize_element(value);
}
}

/// Finish serializing sequence.
fn end(mut self) {
let (buffer, formatter) = self.serializer.buffer_and_formatter_mut();
Expand Down
6 changes: 4 additions & 2 deletions napi/parser/deserialize-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ function deserializeBindingRestElement(pos) {
}

function deserializeFunction(pos) {
const params = deserializeBoxFormalParameters(pos + 72);
return {
type: deserializeFunctionType(pos + 8),
start: deserializeU32(pos),
Expand All @@ -757,7 +758,7 @@ function deserializeFunction(pos) {
expression: false,
generator: deserializeBool(pos + 48),
async: deserializeBool(pos + 49),
params: deserializeBoxFormalParameters(pos + 72),
params,
body: deserializeOptionBoxFunctionBody(pos + 88),
};
}
Expand Down Expand Up @@ -1262,10 +1263,11 @@ function deserializeJSXText(pos) {

function deserializeTSThisParameter(pos) {
return {
type: 'TSThisParameter',
type: 'Identifier',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeAnnotation: deserializeOptionBoxTSTypeAnnotation(pos + 16),
name: 'this',
};
}

Expand Down
13 changes: 10 additions & 3 deletions napi/parser/deserialize-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,11 @@ function deserializeBindingRestElement(pos) {
}

function deserializeFunction(pos) {
const params = deserializeBoxFormalParameters(pos + 72);
const thisParam = deserializeOptionBoxTSThisParameter(pos + 64);
if (thisParam !== null) {
params.unshift(thisParam);
}
return {
type: deserializeFunctionType(pos + 8),
start: deserializeU32(pos),
Expand All @@ -776,11 +781,10 @@ function deserializeFunction(pos) {
expression: false,
generator: deserializeBool(pos + 48),
async: deserializeBool(pos + 49),
params: deserializeBoxFormalParameters(pos + 72),
params,
body: deserializeOptionBoxFunctionBody(pos + 88),
declare: deserializeBool(pos + 50),
typeParameters: deserializeOptionBoxTSTypeParameterDeclaration(pos + 56),
thisParam: deserializeOptionBoxTSThisParameter(pos + 64),
returnType: deserializeOptionBoxTSTypeAnnotation(pos + 80),
};
}
Expand Down Expand Up @@ -1326,10 +1330,13 @@ function deserializeJSXText(pos) {

function deserializeTSThisParameter(pos) {
return {
type: 'TSThisParameter',
type: 'Identifier',
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
typeAnnotation: deserializeOptionBoxTSTypeAnnotation(pos + 16),
name: 'this',
decorators: [],
optional: false,
};
}

Expand Down
6 changes: 4 additions & 2 deletions npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ export interface Function extends Span {
body: FunctionBody | null;
declare?: boolean;
typeParameters?: TSTypeParameterDeclaration | null;
thisParam?: TSThisParameter | null;
returnType?: TSTypeAnnotation | null;
}

Expand Down Expand Up @@ -918,8 +917,11 @@ export interface JSXText extends Span {
}

export interface TSThisParameter extends Span {
type: 'TSThisParameter';
type: 'Identifier';
typeAnnotation: TSTypeAnnotation | null;
name: 'this';
decorators?: [];
optional?: false;
}

export interface TSEnumDeclaration extends Span {
Expand Down
Loading