Skip to content

Commit

Permalink
feat(ast)!: add pure field to Function, CallExpression, and `Ne…
Browse files Browse the repository at this point in the history
…wExpression`
  • Loading branch information
overlookmotel authored and Boshen committed Feb 25, 2025
1 parent 5ee2cab commit a435f86
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 12 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ pub struct CallExpression<'a> {
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
pub arguments: Vec<'a, Argument<'a>>,
pub optional: bool, // for optional chaining
/// `true` if the call expression is marked with a `/* @__PURE__ */` comment
#[builder(default)]
#[estree(skip)]
pub pure: bool,
}

/// `new C()` in `class C {}; new C();`
Expand All @@ -599,6 +603,10 @@ pub struct NewExpression<'a> {
pub arguments: Vec<'a, Argument<'a>>,
#[ts]
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
/// `true` if the new expression is marked with a `/* @__PURE__ */` comment
#[builder(default)]
#[estree(skip)]
pub pure: bool,
}

/// `import.meta` in `console.log(import.meta);`
Expand Down Expand Up @@ -1702,6 +1710,10 @@ pub struct Function<'a> {
/// ```
pub body: Option<Box<'a, FunctionBody<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
/// `true` if the function is marked with a `/*#__NO_SIDE_EFFECTS__*/` comment
#[builder(default)]
#[estree(skip)]
pub pure: bool,
}

#[ast]
Expand Down
47 changes: 45 additions & 2 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{borrow::Cow, mem};

use oxc_allocator::{Allocator, Box, FromIn, Vec};
use oxc_allocator::{Allocator, Box, FromIn, IntoIn, Vec};
use oxc_span::{Atom, SPAN, Span};
use oxc_syntax::{number::NumberBase, operator::UnaryOperator, scope::ScopeId};

Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'a> AstBuilder<'a> {
body: FunctionBody<'a>,
scope_id: ScopeId,
) -> Box<'a, Function<'a>> {
self.alloc_function_with_scope_id(
self.alloc_function_with_scope_id_and_pure(
span,
r#type,
id,
Expand All @@ -275,6 +275,49 @@ impl<'a> AstBuilder<'a> {
NONE,
Some(body),
scope_id,
false,
)
}

/// Build a [`Function`] with `scope_id`.
#[expect(clippy::too_many_arguments)]
#[inline]
pub fn alloc_function_with_scope_id<T1, T2, T3, T4, T5>(
self,
span: Span,
r#type: FunctionType,
id: Option<BindingIdentifier<'a>>,
generator: bool,
r#async: bool,
declare: bool,
type_parameters: T1,
this_param: T2,
params: T3,
return_type: T4,
body: T5,
scope_id: ScopeId,
) -> Box<'a, Function<'a>>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
T2: IntoIn<'a, Option<Box<'a, TSThisParameter<'a>>>>,
T3: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
T5: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
{
self.alloc_function_with_scope_id_and_pure(
span,
r#type,
id,
generator,
r#async,
declare,
type_parameters,
this_param,
params,
return_type,
body,
scope_id,
false,
)
}

Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ const _: () = {
assert!(offset_of!(CallExpression, type_parameters) == 24);
assert!(offset_of!(CallExpression, arguments) == 32);
assert!(offset_of!(CallExpression, optional) == 64);
assert!(offset_of!(CallExpression, pure) == 65);

assert!(size_of::<NewExpression>() == 64);
assert!(size_of::<NewExpression>() == 72);
assert!(align_of::<NewExpression>() == 8);
assert!(offset_of!(NewExpression, span) == 0);
assert!(offset_of!(NewExpression, callee) == 8);
assert!(offset_of!(NewExpression, arguments) == 24);
assert!(offset_of!(NewExpression, type_parameters) == 56);
assert!(offset_of!(NewExpression, pure) == 64);

assert!(size_of::<MetaProperty>() == 56);
assert!(align_of::<MetaProperty>() == 8);
Expand Down Expand Up @@ -516,6 +518,7 @@ const _: () = {
assert!(offset_of!(Function, return_type) == 80);
assert!(offset_of!(Function, body) == 88);
assert!(offset_of!(Function, scope_id) == 96);
assert!(offset_of!(Function, pure) == 100);

assert!(size_of::<FunctionType>() == 1);
assert!(align_of::<FunctionType>() == 1);
Expand Down Expand Up @@ -1722,13 +1725,15 @@ const _: () = {
assert!(offset_of!(CallExpression, type_parameters) == 16);
assert!(offset_of!(CallExpression, arguments) == 20);
assert!(offset_of!(CallExpression, optional) == 36);
assert!(offset_of!(CallExpression, pure) == 37);

assert!(size_of::<NewExpression>() == 36);
assert!(size_of::<NewExpression>() == 40);
assert!(align_of::<NewExpression>() == 4);
assert!(offset_of!(NewExpression, span) == 0);
assert!(offset_of!(NewExpression, callee) == 8);
assert!(offset_of!(NewExpression, arguments) == 16);
assert!(offset_of!(NewExpression, type_parameters) == 32);
assert!(offset_of!(NewExpression, pure) == 36);

assert!(size_of::<MetaProperty>() == 40);
assert!(align_of::<MetaProperty>() == 4);
Expand Down Expand Up @@ -2079,7 +2084,7 @@ const _: () = {
assert!(offset_of!(BindingRestElement, span) == 0);
assert!(offset_of!(BindingRestElement, argument) == 8);

assert!(size_of::<Function>() == 60);
assert!(size_of::<Function>() == 64);
assert!(align_of::<Function>() == 4);
assert!(offset_of!(Function, span) == 0);
assert!(offset_of!(Function, r#type) == 8);
Expand All @@ -2093,6 +2098,7 @@ const _: () = {
assert!(offset_of!(Function, return_type) == 48);
assert!(offset_of!(Function, body) == 52);
assert!(offset_of!(Function, scope_id) == 56);
assert!(offset_of!(Function, pure) == 60);

assert!(size_of::<FunctionType>() == 1);
assert!(align_of::<FunctionType>() == 1);
Expand Down
Loading

0 comments on commit a435f86

Please sign in to comment.