From 550574982fa6682c1406702c16504efe02112532 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 28 Aug 2024 08:42:37 +0000 Subject: [PATCH] feat(ast): add `accessibility` field to `AccessorProperty` (#5290) --- crates/oxc_ast/src/ast/js.rs | 15 ++++++++ .../oxc_ast/src/generated/assert_layouts.rs | 6 ++-- crates/oxc_ast/src/generated/ast_builder.rs | 9 +++++ .../oxc_ast/src/generated/derive_clone_in.rs | 1 + crates/oxc_codegen/src/gen.rs | 3 ++ crates/oxc_isolated_declarations/src/class.rs | 1 + crates/oxc_parser/src/js/class.rs | 16 +++++++-- crates/oxc_traverse/src/generated/ancestor.rs | 34 +++++++++++++++++++ tasks/transform_conformance/babel.snap.md | 2 ++ 9 files changed, 82 insertions(+), 5 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 4d5697bb09772..8e6e88fdfc7a1 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -2272,6 +2272,21 @@ pub struct AccessorProperty<'a> { /// /// Will only ever be [`Some`] for TypeScript files. pub type_annotation: Option>>, + /// 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, } #[ast(visit)] diff --git a/crates/oxc_ast/src/generated/assert_layouts.rs b/crates/oxc_ast/src/generated/assert_layouts.rs index a17d9b9e0e570..e3453b27ed693 100644 --- a/crates/oxc_ast/src/generated/assert_layouts.rs +++ b/crates/oxc_ast/src/generated/assert_layouts.rs @@ -681,7 +681,7 @@ const _: () = { assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); - assert!(size_of::() == 96usize); + assert!(size_of::() == 104usize); assert!(align_of::() == 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::() == 56usize); assert!(align_of::() == 8usize); @@ -2081,7 +2082,7 @@ const _: () = { assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); - assert!(size_of::() == 52usize); + assert!(size_of::() == 56usize); assert!(align_of::() == 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::() == 32usize); assert!(align_of::() == 4usize); diff --git a/crates/oxc_ast/src/generated/ast_builder.rs b/crates/oxc_ast/src/generated/ast_builder.rs index 807224ba7180c..066ac8b062b86 100644 --- a/crates/oxc_ast/src/generated/ast_builder.rs +++ b/crates/oxc_ast/src/generated/ast_builder.rs @@ -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( self, @@ -6381,6 +6382,7 @@ impl<'a> AstBuilder<'a> { r#static: bool, definite: bool, type_annotation: T1, + accessibility: Option, ) -> ClassElement<'a> where T1: IntoIn<'a, Option>>>, @@ -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( self, @@ -6959,6 +6963,7 @@ impl<'a> AstBuilder<'a> { r#static: bool, definite: bool, type_annotation: T1, + accessibility: Option, ) -> AccessorProperty<'a> where T1: IntoIn<'a, Option>>>, @@ -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( self, @@ -7002,6 +7009,7 @@ impl<'a> AstBuilder<'a> { r#static: bool, definite: bool, type_annotation: T1, + accessibility: Option, ) -> Box<'a, AccessorProperty<'a>> where T1: IntoIn<'a, Option>>>, @@ -7017,6 +7025,7 @@ impl<'a> AstBuilder<'a> { r#static, definite, type_annotation, + accessibility, ), self.allocator, ) diff --git a/crates/oxc_ast/src/generated/derive_clone_in.rs b/crates/oxc_ast/src/generated/derive_clone_in.rs index 2c21042b17166..8b330c63d7181 100644 --- a/crates/oxc_ast/src/generated/derive_clone_in.rs +++ b/crates/oxc_ast/src/generated/derive_clone_in.rs @@ -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), } } } diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index c7f8a28828564..d6c580e2eed2c 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -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 "); } diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index 4f60e524a6a55..9a64e643e0343 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -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); } diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index 3cbcae2888c61..872949fbda234 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -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` // ^ @@ -484,7 +492,7 @@ impl<'a> ParserImpl<'a> { } /// - #[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, ) -> Result> { 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, )) } } diff --git a/crates/oxc_traverse/src/generated/ancestor.rs b/crates/oxc_traverse/src/generated/ancestor.rs index d26b6ea9b08b2..448a5363ec5a9 100644 --- a/crates/oxc_traverse/src/generated/ancestor.rs +++ b/crates/oxc_traverse/src/generated/ancestor.rs @@ -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>>) } } + + #[inline] + pub fn accessibility(self) -> &'t Option { + unsafe { + &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY) + as *const Option) + } + } } #[repr(transparent)] @@ -7832,6 +7842,14 @@ impl<'a, 't> AccessorPropertyWithoutKey<'a, 't> { as *const Option>>) } } + + #[inline] + pub fn accessibility(self) -> &'t Option { + unsafe { + &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY) + as *const Option) + } + } } #[repr(transparent)] @@ -7892,6 +7910,14 @@ impl<'a, 't> AccessorPropertyWithoutValue<'a, 't> { as *const Option>>) } } + + #[inline] + pub fn accessibility(self) -> &'t Option { + unsafe { + &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY) + as *const Option) + } + } } #[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 { + unsafe { + &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_ACCESSIBILITY) + as *const Option) + } + } } pub(crate) const OFFSET_IMPORT_EXPRESSION_SPAN: usize = offset_of!(ImportExpression, span); diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 84883e790e6e8..4ce3ff99116a9 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -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]