Skip to content

Commit

Permalink
feat(ast): add type to AccessorProperty to support TSAbractAccessorPr…
Browse files Browse the repository at this point in the history
…operty (#3256)
  • Loading branch information
Dunqing committed May 13, 2024
1 parent 7193d75 commit eefb66f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
18 changes: 16 additions & 2 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,8 @@ impl<'a> ClassElement<'a> {
Self::PropertyDefinition(property) => {
property.r#type == PropertyDefinitionType::TSAbstractPropertyDefinition
}
_ => false,
Self::AccessorProperty(property) => property.r#type.is_abstract(),
Self::StaticBlock(_) => false,
}
}

Expand Down Expand Up @@ -2701,11 +2702,24 @@ impl<'a> ModuleDeclaration<'a> {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AccessorPropertyType {
AccessorProperty,
TSAbstractAccessorProperty,
}

impl AccessorPropertyType {
pub fn is_abstract(&self) -> bool {
matches!(self, Self::TSAbstractAccessorProperty)
}
}

#[visited_node]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
pub struct AccessorProperty<'a> {
pub r#type: AccessorPropertyType,
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub key: PropertyKey<'a>,
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ impl<'a> AstBuilder<'a> {

pub fn accessor_property(
&self,
r#type: AccessorPropertyType,
span: Span,
key: PropertyKey<'a>,
value: Option<Expression<'a>>,
Expand All @@ -1020,6 +1021,7 @@ impl<'a> AstBuilder<'a> {
decorators: Vec<'a, Decorator<'a>>,
) -> ClassElement<'a> {
ClassElement::AccessorProperty(self.alloc(AccessorProperty {
r#type,
span,
key,
value,
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 @@ -2397,6 +2397,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for PropertyDefinition<'a> {
impl<'a, const MINIFY: bool> Gen<MINIFY> for AccessorProperty<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.add_source_mapping(self.span.start);
if p.options.enable_typescript && self.r#type.is_abstract() {
p.print_str(b"abstract ");
}
if self.r#static {
p.print_str(b"static ");
}
Expand Down
10 changes: 9 additions & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'a> ParserImpl<'a> {
if accessor {
self.parse_ts_type_annotation()?;

return self.parse_class_accessor_property(span, key, computed, r#static);
return self.parse_class_accessor_property(span, key, computed, r#static, r#abstract);
}

// LAngle for start of type parameters `foo<T>`
Expand Down Expand Up @@ -468,10 +468,18 @@ impl<'a> ParserImpl<'a> {
key: PropertyKey<'a>,
computed: bool,
r#static: bool,
r#abstract: bool,
) -> Result<ClassElement<'a>> {
let value =
self.eat(Kind::Eq).then(|| self.parse_assignment_expression_base()).transpose()?;
let r#type = if r#abstract {
AccessorPropertyType::TSAbstractAccessorProperty
} else {
AccessorPropertyType::AccessorProperty
};

Ok(self.ast.accessor_property(
r#type,
self.end_span(span),
key,
value,
Expand Down
25 changes: 25 additions & 0 deletions crates/oxc_traverse/src/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6963,6 +6963,7 @@ impl<'a> StaticBlockWithoutBody<'a> {
}
}

pub(crate) const OFFSET_ACCESSOR_PROPERTY_TYPE: usize = offset_of!(AccessorProperty, r#type);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_SPAN: usize = offset_of!(AccessorProperty, span);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_KEY: usize = offset_of!(AccessorProperty, key);
pub(crate) const OFFSET_ACCESSOR_PROPERTY_VALUE: usize = offset_of!(AccessorProperty, value);
Expand All @@ -6976,6 +6977,14 @@ pub(crate) const OFFSET_ACCESSOR_PROPERTY_DECORATORS: usize =
pub struct AccessorPropertyWithoutKey<'a>(pub(crate) *const AccessorProperty<'a>);

impl<'a> AccessorPropertyWithoutKey<'a> {
#[inline]
pub fn r#type(&self) -> &AccessorPropertyType {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_TYPE)
as *const AccessorPropertyType)
}
}

#[inline]
pub fn span(&self) -> &Span {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_SPAN) as *const Span) }
Expand Down Expand Up @@ -7013,6 +7022,14 @@ impl<'a> AccessorPropertyWithoutKey<'a> {
pub struct AccessorPropertyWithoutValue<'a>(pub(crate) *const AccessorProperty<'a>);

impl<'a> AccessorPropertyWithoutValue<'a> {
#[inline]
pub fn r#type(&self) -> &AccessorPropertyType {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_TYPE)
as *const AccessorPropertyType)
}
}

#[inline]
pub fn span(&self) -> &Span {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_SPAN) as *const Span) }
Expand Down Expand Up @@ -7049,6 +7066,14 @@ impl<'a> AccessorPropertyWithoutValue<'a> {
pub struct AccessorPropertyWithoutDecorators<'a>(pub(crate) *const AccessorProperty<'a>);

impl<'a> AccessorPropertyWithoutDecorators<'a> {
#[inline]
pub fn r#type(&self) -> &AccessorPropertyType {
unsafe {
&*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_TYPE)
as *const AccessorPropertyType)
}
}

#[inline]
pub fn span(&self) -> &Span {
unsafe { &*((self.0 as *const u8).add(OFFSET_ACCESSOR_PROPERTY_SPAN) as *const Span) }
Expand Down
6 changes: 2 additions & 4 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 306/362
Passed: 308/362

# All Passed:
* babel-preset-react
Expand All @@ -24,9 +24,7 @@ Passed: 306/362
* opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/input.ts

# babel-plugin-transform-typescript (121/156)
* class/accessor-allowDeclareFields-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts
# babel-plugin-transform-typescript (123/156)
* enum/mix-references/input.ts
* enum/scoped/input.ts
* enum/ts5.0-const-foldable/input.ts
Expand Down

0 comments on commit eefb66f

Please sign in to comment.