mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(ast): add type to AccessorProperty to support TSAbractAccessorProperty (#3256)
This commit is contained in:
parent
7193d75e94
commit
eefb66f750
6 changed files with 57 additions and 7 deletions
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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>,
|
||||
|
|
|
|||
|
|
@ -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>>,
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>`
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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) }
|
||||
|
|
@ -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) }
|
||||
|
|
@ -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) }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Passed: 306/362
|
||||
Passed: 308/362
|
||||
|
||||
# All Passed:
|
||||
* babel-preset-react
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue