Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/parser): Support auto accessors #6981

Merged
merged 40 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/swc_atoms/words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ abstract
accent-color
accept
accesskey
accessor
acos
acronym
action
Expand Down
75 changes: 73 additions & 2 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
Accessibility, TsExprWithTypeArgs, TsIndexSignature, TsTypeAnn, TsTypeParamDecl,
TsTypeParamInstantiation,
},
EmptyStmt,
BigInt, ComputedPropName, EmptyStmt, Id, Ident, Number,
};

#[ast_node]
Expand Down Expand Up @@ -82,9 +82,13 @@ pub enum ClassMember {
#[tag("EmptyStatement")]
Empty(EmptyStmt),

// Stage 3
/// Stage 3
#[tag("StaticBlock")]
StaticBlock(StaticBlock),

/// Stage 3
#[tag("AutoAccessor")]
AutoAccessor(AutoAccessor),
}

impl Take for ClassMember {
Expand Down Expand Up @@ -275,3 +279,70 @@ impl Take for StaticBlock {
}
}
}

/// Either a private name or a public name.
#[ast_node]
#[derive(Is, Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Key {
#[tag("PrivateName")]
Private(PrivateName),
#[tag("Identifier")]
#[tag("StringLiteral")]
#[tag("NumericLiteral")]
#[tag("Computed")]
#[tag("BigIntLiteral")]
Public(PropName),
}

bridge_from!(Key, PropName, Ident);
bridge_from!(Key, PropName, Id);
bridge_from!(Key, PropName, Number);
bridge_from!(Key, PropName, ComputedPropName);
bridge_from!(Key, PropName, BigInt);

impl Take for Key {
fn dummy() -> Self {
Key::Public(Take::dummy())
}
}

#[ast_node("AutoAccessor")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct AutoAccessor {
#[serde(default)]
pub span: Span,

pub key: Key,

#[serde(default)]
pub value: Option<Box<Expr>>,

#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<Box<TsTypeAnn>>,

#[serde(default)]
pub is_static: bool,

#[serde(default)]
pub decorators: Vec<Decorator>,

/// Typescript extension.
#[serde(default)]
pub accessibility: Option<Accessibility>,
}

impl Take for AutoAccessor {
fn dummy() -> AutoAccessor {
AutoAccessor {
span: Take::dummy(),
key: Take::dummy(),
value: Take::dummy(),
type_ann: None,
is_static: false,
decorators: Take::dummy(),
accessibility: None,
}
}
}
4 changes: 2 additions & 2 deletions crates/swc_ecma_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use swc_common::{ast_node, EqIgnoreSpan, Span};

pub use self::{
class::{
Class, ClassMember, ClassMethod, ClassProp, Constructor, Decorator, MethodKind,
PrivateMethod, PrivateProp, StaticBlock,
AutoAccessor, Class, ClassMember, ClassMethod, ClassProp, Constructor, Decorator, Key,
MethodKind, PrivateMethod, PrivateProp, StaticBlock,
},
decl::{ClassDecl, Decl, FnDecl, VarDecl, VarDeclKind, VarDeclarator},
expr::{
Expand Down
4 changes: 3 additions & 1 deletion crates/swc_ecma_ast/src/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
pat::Pat,
stmt::BlockStmt,
typescript::TsTypeAnn,
MemberProp,
Id, MemberProp,
};

#[ast_node]
Expand Down Expand Up @@ -109,6 +109,8 @@ pub enum PropName {
BigInt(BigInt),
}

bridge_from!(PropName, Ident, Id);

impl Take for PropName {
fn dummy() -> Self {
PropName::Ident(Take::dummy())
Expand Down
33 changes: 33 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,39 @@ where
ClassMember::TsIndexSignature(ref n) => emit!(n),
ClassMember::Empty(ref n) => emit!(n),
ClassMember::StaticBlock(ref n) => emit!(n),
ClassMember::AutoAccessor(ref n) => emit!(n),
}
}

#[emitter]
fn emit_auto_accessor(&mut self, n: &AutoAccessor) -> Result {
self.emit_list(n.span, Some(&n.decorators), ListFormat::Decorators)?;

if n.is_static {
keyword!("static");
space!();
}

keyword!("accessor");
space!();

emit!(n.key);

if let Some(init) = &n.value {
formatting_space!();
punct!("=");
formatting_space!();
emit!(init);
}

semi!();
}

#[emitter]
fn emit_key(&mut self, n: &Key) -> Result {
match n {
Key::Private(n) => emit!(n),
Key::Public(n) => emit!(n),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ impl<'a, I: Input> Lexer<'a, I> {
} else {
match s.as_bytes()[0] {
b'a' if s == "await" => Await.into(),
b'a' if s == "accessor" => Accessor.into(),
b'b' if s == "break" => Break.into(),
b'c' => match s {
"case" => Case.into(),
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ macro_rules! tok {
crate::token::Token::Word(crate::token::Word::Keyword(crate::token::Keyword::Yield))
};

("accessor") => {
crate::token::Token::Word(crate::token::Word::Keyword(crate::token::Keyword::Accessor))
};

// ----------
// JSX
// ----------
Expand Down
Loading