Skip to content

Commit

Permalink
feat(ast): add accessibility field to AccessorProperty (#5290)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Aug 28, 2024
1 parent 76e86f8 commit 5505749
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 5 deletions.
15 changes: 15 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,21 @@ pub struct AccessorProperty<'a> {
///
/// Will only ever be [`Some`] for TypeScript files.
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// Accessibility modifier.
///
/// Only ever [`Some`] for TypeScript files.
///
/// ## Example
///
/// ```ts
/// class Foo {
/// public accessor w: number // Some(TSAccessibility::Public)
/// private accessor x: string // Some(TSAccessibility::Private)
/// protected accessor y: boolean // Some(TSAccessibility::Protected)
/// accessor z // None
/// }
/// ```
pub accessibility: Option<TSAccessibility>,
}

#[ast(visit)]
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ const _: () = {
assert!(size_of::<AccessorPropertyType>() == 1usize);
assert!(align_of::<AccessorPropertyType>() == 1usize);

assert!(size_of::<AccessorProperty>() == 96usize);
assert!(size_of::<AccessorProperty>() == 104usize);
assert!(align_of::<AccessorProperty>() == 8usize);
assert!(offset_of!(AccessorProperty, r#type) == 0usize);
assert!(offset_of!(AccessorProperty, span) == 4usize);
Expand All @@ -692,6 +692,7 @@ const _: () = {
assert!(offset_of!(AccessorProperty, r#static) == 81usize);
assert!(offset_of!(AccessorProperty, definite) == 82usize);
assert!(offset_of!(AccessorProperty, type_annotation) == 88usize);
assert!(offset_of!(AccessorProperty, accessibility) == 96usize);

assert!(size_of::<ImportExpression>() == 56usize);
assert!(align_of::<ImportExpression>() == 8usize);
Expand Down Expand Up @@ -2081,7 +2082,7 @@ const _: () = {
assert!(size_of::<AccessorPropertyType>() == 1usize);
assert!(align_of::<AccessorPropertyType>() == 1usize);

assert!(size_of::<AccessorProperty>() == 52usize);
assert!(size_of::<AccessorProperty>() == 56usize);
assert!(align_of::<AccessorProperty>() == 4usize);
assert!(offset_of!(AccessorProperty, r#type) == 0usize);
assert!(offset_of!(AccessorProperty, span) == 4usize);
Expand All @@ -2092,6 +2093,7 @@ const _: () = {
assert!(offset_of!(AccessorProperty, r#static) == 45usize);
assert!(offset_of!(AccessorProperty, definite) == 46usize);
assert!(offset_of!(AccessorProperty, type_annotation) == 48usize);
assert!(offset_of!(AccessorProperty, accessibility) == 52usize);

assert!(size_of::<ImportExpression>() == 32usize);
assert!(align_of::<ImportExpression>() == 4usize);
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 @@ -6369,6 +6369,7 @@ impl<'a> AstBuilder<'a> {
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
/// - accessibility: Accessibility modifier.
#[inline]
pub fn class_element_accessor_property<T1>(
self,
Expand All @@ -6381,6 +6382,7 @@ impl<'a> AstBuilder<'a> {
r#static: bool,
definite: bool,
type_annotation: T1,
accessibility: Option<TSAccessibility>,
) -> ClassElement<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
Expand All @@ -6395,6 +6397,7 @@ impl<'a> AstBuilder<'a> {
r#static,
definite,
type_annotation,
accessibility,
)))
}

Expand Down Expand Up @@ -6947,6 +6950,7 @@ impl<'a> AstBuilder<'a> {
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
/// - accessibility: Accessibility modifier.
#[inline]
pub fn accessor_property<T1>(
self,
Expand All @@ -6959,6 +6963,7 @@ impl<'a> AstBuilder<'a> {
r#static: bool,
definite: bool,
type_annotation: T1,
accessibility: Option<TSAccessibility>,
) -> AccessorProperty<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
Expand All @@ -6973,6 +6978,7 @@ impl<'a> AstBuilder<'a> {
r#static,
definite,
type_annotation: type_annotation.into_in(self.allocator),
accessibility,
}
}

Expand All @@ -6990,6 +6996,7 @@ impl<'a> AstBuilder<'a> {
/// - r#static: Property was declared with a `static` modifier
/// - definite: Property has a `!` after its key.
/// - type_annotation: Type annotation on the property.
/// - accessibility: Accessibility modifier.
#[inline]
pub fn alloc_accessor_property<T1>(
self,
Expand All @@ -7002,6 +7009,7 @@ impl<'a> AstBuilder<'a> {
r#static: bool,
definite: bool,
type_annotation: T1,
accessibility: Option<TSAccessibility>,
) -> Box<'a, AccessorProperty<'a>>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
Expand All @@ -7017,6 +7025,7 @@ impl<'a> AstBuilder<'a> {
r#static,
definite,
type_annotation,
accessibility,
),
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 @@ -1939,6 +1939,7 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for AccessorProperty<'old_alloc
r#static: self.r#static.clone_in(allocator),
definite: self.definite.clone_in(allocator),
type_annotation: self.type_annotation.clone_in(allocator),
accessibility: self.accessibility.clone_in(allocator),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,9 @@ impl<'a> Gen for AccessorProperty<'a> {
if self.r#type.is_abstract() {
p.print_str("abstract ");
}
if let Some(accessibility) = &self.accessibility {
accessibility.gen(p, ctx);
}
if self.r#static {
p.print_str("static ");
}
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 @@ -472,6 +472,7 @@ impl<'a> IsolatedDeclarations<'a> {
property.definite,
// SAFETY: `ast.copy` is unsound! We need to fix.
unsafe { self.ast.copy(&property.type_annotation) },
property.accessibility,
);
elements.push(new_element);
}
Expand Down
16 changes: 13 additions & 3 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,16 @@ 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, definite)
.map(Some)
self.parse_class_accessor_property(
span,
key,
computed,
r#static,
r#abstract,
definite,
accessibility,
)
.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 @@ -484,7 +492,7 @@ impl<'a> ParserImpl<'a> {
}

/// <https://github.com/tc39/proposal-decorators>
#[allow(clippy::fn_params_excessive_bools)]
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
fn parse_class_accessor_property(
&mut self,
span: Span,
Expand All @@ -493,6 +501,7 @@ impl<'a> ParserImpl<'a> {
r#static: bool,
r#abstract: bool,
definite: bool,
accessibility: Option<TSAccessibility>,
) -> Result<ClassElement<'a>> {
let type_annotation =
if self.ts_enabled() { self.parse_ts_type_annotation()? } else { None };
Expand All @@ -515,6 +524,7 @@ impl<'a> ParserImpl<'a> {
r#static,
definite,
type_annotation,
accessibility,
))
}
}
34 changes: 34 additions & 0 deletions crates/oxc_traverse/src/generated/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7712,6 +7712,8 @@ pub(crate) const OFFSET_ACCESSOR_PROPERTY_STATIC: usize = offset_of!(AccessorPro
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);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY: usize =
offset_of!(AccessorProperty, accessibility);

#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -7771,6 +7773,14 @@ impl<'a, 't> AccessorPropertyWithoutDecorators<'a, 't> {
as *const Option<Box<'a, TSTypeAnnotation<'a>>>)
}
}

#[inline]
pub fn accessibility(self) -> &'t Option<TSAccessibility> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY)
as *const Option<TSAccessibility>)
}
}
}

#[repr(transparent)]
Expand Down Expand Up @@ -7832,6 +7842,14 @@ impl<'a, 't> AccessorPropertyWithoutKey<'a, 't> {
as *const Option<Box<'a, TSTypeAnnotation<'a>>>)
}
}

#[inline]
pub fn accessibility(self) -> &'t Option<TSAccessibility> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY)
as *const Option<TSAccessibility>)
}
}
}

#[repr(transparent)]
Expand Down Expand Up @@ -7892,6 +7910,14 @@ impl<'a, 't> AccessorPropertyWithoutValue<'a, 't> {
as *const Option<Box<'a, TSTypeAnnotation<'a>>>)
}
}

#[inline]
pub fn accessibility(self) -> &'t Option<TSAccessibility> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY)
as *const Option<TSAccessibility>)
}
}
}

#[repr(transparent)]
Expand Down Expand Up @@ -7952,6 +7978,14 @@ impl<'a, 't> AccessorPropertyWithoutTypeAnnotation<'a, 't> {
pub fn definite(self) -> &'t bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_DEFINITE) as *const bool) }
}

#[inline]
pub fn accessibility(self) -> &'t Option<TSAccessibility> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY)
as *const Option<TSAccessibility>)
}
}
}

pub(crate) const OFFSET_IMPORT_EXPRESSION_SPAN: usize = offset_of!(ImportExpression, span);
Expand Down
2 changes: 2 additions & 0 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,7 @@ failed to resolve query: failed to parse the rest of input: ...''


* class/accessor-allowDeclareFields-false/input.ts
x Output mismatch
x TS(18010): An accessibility modifier cannot be used with a private
| identifier.
,-[tasks/coverage/babel/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/input.ts:8:3]
Expand All @@ -1990,6 +1991,7 @@ failed to resolve query: failed to parse the rest of input: ...''


* class/accessor-allowDeclareFields-true/input.ts
x Output mismatch
x TS(18010): An accessibility modifier cannot be used with a private
| identifier.
,-[tasks/coverage/babel/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/input.ts:8:3]
Expand Down

0 comments on commit 5505749

Please sign in to comment.