Skip to content

Commit

Permalink
fix(ast): estree compat Function (#8972)
Browse files Browse the repository at this point in the history
Part of #2854

diff of estree_test262.snap

```diff
-Positive Passed: 6233/44293 (14.07%)
+Positive Passed: 13042/44293 (29.44%)
```
  • Loading branch information
hi-ogawa committed Feb 9, 2025
1 parent f2d28f3 commit a2883b1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6,817 deletions.
8 changes: 7 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,11 @@ pub struct BindingRestElement<'a> {
#[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")]
#[estree(
add_ts_def = "type ParamPattern = FormalParameter | FormalParameterRest",
add_fields(expression = false),
add_ts = "expression: false"
)]
pub struct Function<'a> {
pub span: Span,
pub r#type: FunctionType,
Expand Down Expand Up @@ -1695,9 +1699,11 @@ pub enum FormalParameterKind {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(rename = "BlockStatement")]
pub struct FunctionBody<'a> {
pub span: Span,
pub directives: Vec<'a, Directive<'a>>,
#[estree(rename = "body")]
pub statements: Vec<'a, Statement<'a>>,
}

Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ impl Serialize for Function<'_> {
map.serialize_entry("params", &self.params)?;
map.serialize_entry("returnType", &self.return_type)?;
map.serialize_entry("body", &self.body)?;
map.serialize_entry("expression", &false)?;
map.end()
}
}
Expand Down Expand Up @@ -1399,11 +1400,11 @@ impl Serialize for FormalParameterKind {
impl Serialize for FunctionBody<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "FunctionBody")?;
map.serialize_entry("type", "BlockStatement")?;
map.serialize_entry("start", &self.span.start)?;
map.serialize_entry("end", &self.span.end)?;
map.serialize_entry("directives", &self.directives)?;
map.serialize_entry("statements", &self.statements)?;
map.serialize_entry("body", &self.statements)?;
map.end()
}
}
Expand Down
5 changes: 3 additions & 2 deletions npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ export interface Function extends Span {
params: ParamPattern[];
returnType: TSTypeAnnotation | null;
body: FunctionBody | null;
expression: false;
}

export type ParamPattern = FormalParameter | FormalParameterRest;
Expand Down Expand Up @@ -576,9 +577,9 @@ export type FormalParameter =
export type FormalParameterKind = 'FormalParameter' | 'UniqueFormalParameters' | 'ArrowFormalParameters' | 'Signature';

export interface FunctionBody extends Span {
type: 'FunctionBody';
type: 'BlockStatement';
directives: Array<Directive>;
statements: Array<Statement>;
body: Array<Statement>;
}

export interface ArrowFunctionExpression extends Span {
Expand Down
Loading

0 comments on commit a2883b1

Please sign in to comment.