mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(ast): add accessibility field to AccessorProperty (#5290)
This commit is contained in:
parent
76e86f80df
commit
550574982f
9 changed files with 82 additions and 5 deletions
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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>>>>,
|
||||
|
|
@ -6395,6 +6397,7 @@ impl<'a> AstBuilder<'a> {
|
|||
r#static,
|
||||
definite,
|
||||
type_annotation,
|
||||
accessibility,
|
||||
)))
|
||||
}
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
@ -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>>>>,
|
||||
|
|
@ -6973,6 +6978,7 @@ impl<'a> AstBuilder<'a> {
|
|||
r#static,
|
||||
definite,
|
||||
type_annotation: type_annotation.into_in(self.allocator),
|
||||
accessibility,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
@ -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>>>>,
|
||||
|
|
@ -7017,6 +7025,7 @@ impl<'a> AstBuilder<'a> {
|
|||
r#static,
|
||||
definite,
|
||||
type_annotation,
|
||||
accessibility,
|
||||
),
|
||||
self.allocator,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>`
|
||||
// ^
|
||||
|
|
@ -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,
|
||||
|
|
@ -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 };
|
||||
|
|
@ -515,6 +524,7 @@ impl<'a> ParserImpl<'a> {
|
|||
r#static,
|
||||
definite,
|
||||
type_annotation,
|
||||
accessibility,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
@ -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)]
|
||||
|
|
@ -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)]
|
||||
|
|
@ -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)]
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Reference in a new issue