Skip to content

Commit

Permalink
feat(ast,parser): add definite flag to AccessorProperty node
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent 486772b commit bc0a5e5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,8 @@ pub struct AccessorProperty<'a> {
pub computed: bool,
/// Property was declared with a `static` modifier
pub r#static: bool,
/// Property has a `!` after its key.
pub definite: bool,
/// Type annotation on the property.
///
/// Will only ever be [`Some`] for TypeScript files.
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ const _: () = {
assert!(offset_of!(AccessorProperty, value) == 64usize);
assert!(offset_of!(AccessorProperty, computed) == 80usize);
assert!(offset_of!(AccessorProperty, r#static) == 81usize);
assert!(offset_of!(AccessorProperty, definite) == 82usize);
assert!(offset_of!(AccessorProperty, type_annotation) == 88usize);

assert!(size_of::<ImportExpression>() == 56usize);
Expand Down Expand Up @@ -2101,6 +2102,7 @@ const _: () = {
assert!(offset_of!(AccessorProperty, value) == 36usize);
assert!(offset_of!(AccessorProperty, computed) == 44usize);
assert!(offset_of!(AccessorProperty, r#static) == 45usize);
assert!(offset_of!(AccessorProperty, definite) == 46usize);
assert!(offset_of!(AccessorProperty, type_annotation) == 48usize);

assert!(size_of::<ImportExpression>() == 32usize);
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6496,6 +6496,7 @@ impl<'a> AstBuilder<'a> {
/// - value: Initialized value in the declaration, if present.
/// - computed: Property was declared with a computed key
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
#[inline]
pub fn class_element_accessor_property<T1>(
Expand All @@ -6507,6 +6508,7 @@ impl<'a> AstBuilder<'a> {
value: Option<Expression<'a>>,
computed: bool,
r#static: bool,
definite: bool,
type_annotation: T1,
) -> ClassElement<'a>
where
Expand All @@ -6520,6 +6522,7 @@ impl<'a> AstBuilder<'a> {
value,
computed,
r#static,
definite,
type_annotation,
)))
}
Expand Down Expand Up @@ -7071,6 +7074,7 @@ impl<'a> AstBuilder<'a> {
/// - value: Initialized value in the declaration, if present.
/// - computed: Property was declared with a computed key
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
#[inline]
pub fn accessor_property<T1>(
Expand All @@ -7082,6 +7086,7 @@ impl<'a> AstBuilder<'a> {
value: Option<Expression<'a>>,
computed: bool,
r#static: bool,
definite: bool,
type_annotation: T1,
) -> AccessorProperty<'a>
where
Expand All @@ -7095,6 +7100,7 @@ impl<'a> AstBuilder<'a> {
value,
computed,
r#static,
definite,
type_annotation: type_annotation.into_in(self.allocator),
}
}
Expand All @@ -7111,6 +7117,7 @@ impl<'a> AstBuilder<'a> {
/// - value: Initialized value in the declaration, if present.
/// - computed: Property was declared with a computed key
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
#[inline]
pub fn alloc_accessor_property<T1>(
Expand All @@ -7122,6 +7129,7 @@ impl<'a> AstBuilder<'a> {
value: Option<Expression<'a>>,
computed: bool,
r#static: bool,
definite: bool,
type_annotation: T1,
) -> Box<'a, AccessorProperty<'a>>
where
Expand All @@ -7136,6 +7144,7 @@ impl<'a> AstBuilder<'a> {
value,
computed,
r#static,
definite,
type_annotation,
),
self.allocator,
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,7 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for AccessorProperty<'old_alloc
value: self.value.clone_in(allocator),
computed: self.computed.clone_in(allocator),
r#static: self.r#static.clone_in(allocator),
definite: self.definite.clone_in(allocator),
type_annotation: self.type_annotation.clone_in(allocator),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ impl<'a> IsolatedDeclarations<'a> {
None,
property.computed,
property.r#static,
property.definite,
// SAFETY: `ast.copy` is unsound! We need to fix.
unsafe { self.ast.copy(&property.type_annotation) },
);
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ impl<'a> ParserImpl<'a> {
if optional {
self.error(diagnostics::optional_accessor_property(optional_span));
}
self.parse_class_accessor_property(span, key, computed, r#static, r#abstract).map(Some)
self.parse_class_accessor_property(span, key, computed, r#static, r#abstract, definite)
.map(Some)
} else if self.at(Kind::LParen) || self.at(Kind::LAngle) || r#async || generator {
// LAngle for start of type parameters `foo<T>`
// ^
Expand Down Expand Up @@ -483,13 +484,15 @@ impl<'a> ParserImpl<'a> {
}

/// <https://github.com/tc39/proposal-decorators>
#[allow(clippy::fn_params_excessive_bools)]
fn parse_class_accessor_property(
&mut self,
span: Span,
key: PropertyKey<'a>,
computed: bool,
r#static: bool,
r#abstract: bool,
definite: bool,
) -> Result<ClassElement<'a>> {
let type_annotation =
if self.ts_enabled() { self.parse_ts_type_annotation()? } else { None };
Expand All @@ -510,6 +513,7 @@ impl<'a> ParserImpl<'a> {
value,
computed,
r#static,
definite,
type_annotation,
))
}
Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_traverse/src/generated/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7319,6 +7319,7 @@ pub(crate) const OFFSET_ACCESSOR_PROPERTY_KEY: usize = offset_of!(AccessorProper
pub(crate) const OFFSET_ACCESSOR_PROPERTY_VALUE: usize = offset_of!(AccessorProperty, value);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_COMPUTED: usize = offset_of!(AccessorProperty, computed);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_STATIC: usize = offset_of!(AccessorProperty, r#static);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_DEFINITE: usize = offset_of!(AccessorProperty, definite);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_TYPE_ANNOTATION: usize =
offset_of!(AccessorProperty, type_annotation);

Expand Down Expand Up @@ -7365,6 +7366,11 @@ impl<'a> AccessorPropertyWithoutDecorators<'a> {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_STATIC) as *const bool) }
}

#[inline]
pub fn definite(&self) -> &bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_DEFINITE) as *const bool) }
}

#[inline]
pub fn type_annotation(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand Down Expand Up @@ -7418,6 +7424,11 @@ impl<'a> AccessorPropertyWithoutKey<'a> {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_STATIC) as *const bool) }
}

#[inline]
pub fn definite(&self) -> &bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_DEFINITE) as *const bool) }
}

#[inline]
pub fn type_annotation(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand Down Expand Up @@ -7470,6 +7481,11 @@ impl<'a> AccessorPropertyWithoutValue<'a> {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_STATIC) as *const bool) }
}

#[inline]
pub fn definite(&self) -> &bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_DEFINITE) as *const bool) }
}

#[inline]
pub fn type_annotation(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand Down Expand Up @@ -7529,6 +7545,11 @@ impl<'a> AccessorPropertyWithoutTypeAnnotation<'a> {
pub fn r#static(&self) -> &bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_STATIC) as *const bool) }
}

#[inline]
pub fn definite(&self) -> &bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_DEFINITE) as *const bool) }
}
}

pub(crate) const OFFSET_IMPORT_EXPRESSION_SPAN: usize = offset_of!(ImportExpression, span);
Expand Down

0 comments on commit bc0a5e5

Please sign in to comment.