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
10 changes: 10 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,9 @@ pub struct BindingRestElement<'a> {
)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
// https://github.com/estree/estree/blob/master/es5.md#patterns
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/cd61c555bfc93e985b313263a42ed78074570d08/types/estree/index.d.ts#L411
#[estree(add_ts_def = "type ParamPattern = FormalParameter | FormalParameterRest")]
pub struct Function<'a> {
pub span: Span,
pub r#type: FunctionType,
Expand Down Expand Up @@ -1590,6 +1593,7 @@ pub struct Function<'a> {
/// Function parameters.
///
/// Does not include `this` parameters used by some TypeScript functions.
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
/// The TypeScript return type annotation.
#[ts]
Expand Down Expand Up @@ -1651,10 +1655,15 @@ pub struct FormalParameters<'a> {
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
// Pluralize as `FormalParameterList` to avoid naming clash with `FormalParameters`.
#[plural(FormalParameterList)]
#[estree(no_type)]
pub struct FormalParameter<'a> {
pub span: Span,
#[ts]
pub decorators: Vec<'a, Decorator<'a>>,
#[estree(
flatten,
ts_type = "(BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern)"
)]
pub pattern: BindingPattern<'a>,
#[ts]
pub accessibility: Option<TSAccessibility>,
Expand Down Expand Up @@ -1704,6 +1713,7 @@ pub struct ArrowFunctionExpression<'a> {
pub r#async: bool,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
#[ts]
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ pub struct TSCallSignatureDeclaration<'a> {
pub span: Span,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub this_param: Option<TSThisParameter<'a>>,
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
}
Expand Down Expand Up @@ -1009,6 +1010,7 @@ pub struct TSMethodSignature<'a> {
pub kind: TSMethodSignatureKind,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
Expand All @@ -1022,6 +1024,7 @@ pub struct TSMethodSignature<'a> {
pub struct TSConstructSignatureDeclaration<'a> {
pub span: Span,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
Expand Down Expand Up @@ -1343,6 +1346,7 @@ pub struct TSFunctionType<'a> {
/// ```
pub this_param: Option<Box<'a, TSThisParameter<'a>>>,
/// Function parameters. Akin to [`Function::params`].
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
/// Return type of the function.
/// ```ts
Expand All @@ -1359,6 +1363,7 @@ pub struct TSConstructorType<'a> {
pub span: Span,
pub r#abstract: bool,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
#[estree(ts_type = "ParamPattern[]")]
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Box<'a, TSTypeAnnotation<'a>>,
}
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,10 +1355,9 @@ impl Serialize for FunctionType {
impl Serialize for FormalParameter<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "FormalParameter")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("decorators", &self.decorators)?;
map.serialize_entry("pattern", &self.pattern)?;
self.pattern.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("accessibility", &self.accessibility)?;
map.serialize_entry("readonly", &self.readonly)?;
map.serialize_entry("override", &self.r#override)?;
Expand Down
24 changes: 5 additions & 19 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use oxc_span::{Atom, Span};
use oxc_syntax::number::BigintBase;

use crate::ast::{
BigIntLiteral, BindingPatternKind, BooleanLiteral, Directive, Elision, FormalParameter,
FormalParameterKind, FormalParameters, JSXElementName, JSXIdentifier,
JSXMemberExpressionObject, NullLiteral, NumericLiteral, Program, RegExpFlags, RegExpLiteral,
RegExpPattern, Statement, StringLiteral, TSModuleBlock, TSTypeAnnotation,
BigIntLiteral, BindingPatternKind, BooleanLiteral, Directive, Elision, FormalParameters,
JSXElementName, JSXIdentifier, JSXMemberExpressionObject, NullLiteral, NumericLiteral, Program,
RegExpFlags, RegExpLiteral, RegExpPattern, Statement, StringLiteral, TSModuleBlock,
TSTypeAnnotation,
};

#[derive(Serialize)]
Expand Down Expand Up @@ -187,24 +187,10 @@ impl Serialize for FormalParameters<'_> {
type_annotation: &rest.argument.type_annotation,
optional: rest.argument.optional,
});
let converted = SerFormalParameters {
span: self.span,
kind: self.kind,
items: ElementsAndRest::new(&self.items, converted_rest.as_ref()),
};
converted.serialize(serializer)
ElementsAndRest::new(&self.items, converted_rest.as_ref()).serialize(serializer)
}
}

#[derive(Serialize)]
#[serde(tag = "type", rename = "FormalParameters")]
struct SerFormalParameters<'a, 'b> {
#[serde(flatten)]
span: Span,
kind: FormalParameterKind,
items: ElementsAndRest<'b, FormalParameter<'a>, SerFormalParameterRest<'a, 'b>>,
}

#[derive(Serialize)]
#[serde(tag = "type", rename = "RestElement", rename_all = "camelCase")]
struct SerFormalParameterRest<'a, 'b> {
Expand Down
110 changes: 110 additions & 0 deletions napi/parser/test/__snapshots__/parse.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`parse > estree function params 1`] = `
{
"async": true,
"body": {
"directives": [],
"end": 48,
"start": 46,
"statements": [],
"type": "FunctionBody",
},
"declare": false,
"end": 48,
"generator": false,
"id": {
"end": 19,
"name": "test",
"start": 15,
"type": "Identifier",
},
"params": [
{
"accessibility": null,
"decorators": [],
"end": 21,
"name": "x",
"optional": false,
"override": false,
"readonly": false,
"start": 20,
"type": "Identifier",
"typeAnnotation": null,
},
{
"accessibility": null,
"decorators": [],
"end": 28,
"optional": false,
"override": false,
"properties": [
{
"computed": false,
"end": 26,
"key": {
"end": 26,
"name": "y",
"start": 25,
"type": "Identifier",
},
"shorthand": true,
"start": 25,
"type": "BindingProperty",
"value": {
"end": 26,
"name": "y",
"optional": false,
"start": 25,
"type": "Identifier",
"typeAnnotation": null,
},
},
],
"readonly": false,
"start": 23,
"type": "ObjectPattern",
"typeAnnotation": null,
},
{
"accessibility": null,
"decorators": [],
"elements": [
{
"end": 33,
"name": "z",
"optional": false,
"start": 32,
"type": "Identifier",
"typeAnnotation": null,
},
],
"end": 35,
"optional": false,
"override": false,
"readonly": false,
"start": 30,
"type": "ArrayPattern",
"typeAnnotation": null,
},
{
"argument": {
"end": 44,
"name": "rest",
"start": 40,
"type": "Identifier",
},
"end": 44,
"optional": false,
"start": 37,
"type": "RestElement",
"typeAnnotation": null,
},
],
"returnType": null,
"start": 0,
"thisParam": null,
"type": "FunctionDeclaration",
"typeParameters": null,
}
`;
8 changes: 8 additions & 0 deletions napi/parser/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('parse', () => {
});
expect(code.substring(comment.start, comment.end)).toBe('/*' + comment.value + '*/');
});

it('estree function params', async () => {
const ret = await parseAsync(
'test.js',
`async function test(x, { y }, [ z ], ...rest) {}`,
);
expect(ret.program.body[0]).matchSnapshot();
});
});

describe('error', () => {
Expand Down
33 changes: 18 additions & 15 deletions npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,13 @@ export interface Function extends Span {
declare: boolean;
typeParameters: TSTypeParameterDeclaration | null;
thisParam: TSThisParameter | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
body: FunctionBody | null;
}

export type ParamPattern = FormalParameter | FormalParameterRest;

export type FunctionType =
| 'FunctionDeclaration'
| 'FunctionExpression'
Expand All @@ -742,14 +744,15 @@ export interface FormalParameterRest extends Span {
optional: boolean;
}

export interface FormalParameter extends Span {
type: 'FormalParameter';
decorators: Array<Decorator>;
pattern: BindingPattern;
accessibility: TSAccessibility | null;
readonly: boolean;
override: boolean;
}
export type FormalParameter =
& ({
decorators: Array<Decorator>;
accessibility: TSAccessibility | null;
readonly: boolean;
override: boolean;
})
& Span
& (BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern);

export type FormalParameterKind = 'FormalParameter' | 'UniqueFormalParameters' | 'ArrowFormalParameters' | 'Signature';

Expand All @@ -764,7 +767,7 @@ export interface ArrowFunctionExpression extends Span {
expression: boolean;
async: boolean;
typeParameters: TSTypeParameterDeclaration | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
body: FunctionBody;
}
Expand Down Expand Up @@ -1532,7 +1535,7 @@ export interface TSCallSignatureDeclaration extends Span {
type: 'TSCallSignatureDeclaration';
typeParameters: TSTypeParameterDeclaration | null;
thisParam: TSThisParameter | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
}

Expand All @@ -1546,14 +1549,14 @@ export interface TSMethodSignature extends Span {
kind: TSMethodSignatureKind;
typeParameters: TSTypeParameterDeclaration | null;
thisParam: TSThisParameter | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
}

export interface TSConstructSignatureDeclaration extends Span {
type: 'TSConstructSignatureDeclaration';
typeParameters: TSTypeParameterDeclaration | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
}

Expand Down Expand Up @@ -1642,15 +1645,15 @@ export interface TSFunctionType extends Span {
type: 'TSFunctionType';
typeParameters: TSTypeParameterDeclaration | null;
thisParam: TSThisParameter | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation;
}

export interface TSConstructorType extends Span {
type: 'TSConstructorType';
abstract: boolean;
typeParameters: TSTypeParameterDeclaration | null;
params: FormalParameters;
params: ParamPattern[];
returnType: TSTypeAnnotation;
}

Expand Down