fix(es/transforms): Fix bugs (#1769)

swc_ecma_transforms_optimization:
 - `dce`: Mark references from decorator as usage. (denoland/deno#10789)

swc_ecma_transforms_proposal:
 - Allow using `super` in decorated class methods. (#846)
This commit is contained in:
강동윤 2021-05-30 19:52:51 +09:00 committed by GitHub
parent 5d219b8cd1
commit ad55711e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 872 additions and 161 deletions

View File

@ -0,0 +1,17 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Helper for transforms for the swc project"
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_classes/"
edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_classes"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"
[dependencies]
swc_atoms = {version = "0.2.6", path = "../../../atoms"}
swc_common = {version = "0.10.20", path = "../../../common"}
swc_ecma_ast = {version = "0.45.0", path = "../../ast"}
swc_ecma_transforms_base = {version = "0.15.5", path = "../base"}
swc_ecma_utils = {version = "0.36.0", path = "../../utils"}
swc_ecma_visit = {version = "0.31.0", path = "../../visit"}

View File

@ -0,0 +1,22 @@
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_utils::ExprFactory;
#[macro_use]
pub mod macros;
pub mod super_field;
/// Creates
///
/// ```js
/// Child.__proto__ || Object.getPrototypeOf(Child)
/// ```
pub fn get_prototype_of(obj: Expr) -> Expr {
Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(get_prototype_of, "getPrototypeOf"),
args: vec![obj.as_arg()],
type_args: Default::default(),
})
}

View File

@ -1,3 +1,5 @@
/// Not intended for public use.
#[macro_export]
macro_rules! fold_only_key { macro_rules! fold_only_key {
() => { () => {
fn fold_class_member(&mut self, m: ClassMember) -> ClassMember { fn fold_class_member(&mut self, m: ClassMember) -> ClassMember {

View File

@ -23,7 +23,7 @@ use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
/// ///
/// _get(Child.prototype.__proto__ || Object.getPrototypeOf(Child.prototype), /// _get(Child.prototype.__proto__ || Object.getPrototypeOf(Child.prototype),
/// 'foo', this).call(this, a); /// 'foo', this).call(this, a);
pub(crate) struct SuperFieldAccessFolder<'a> { pub struct SuperFieldAccessFolder<'a> {
pub class_name: &'a Ident, pub class_name: &'a Ident,
pub vars: &'a mut Vec<VarDeclarator>, pub vars: &'a mut Vec<VarDeclarator>,

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_compat" name = "swc_ecma_transforms_compat"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.17.7" version = "0.17.8"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
@ -22,6 +22,7 @@ swc_atoms = {version = "0.2.5", path = "../../../atoms"}
swc_common = {version = "0.10.16", path = "../../../common"} swc_common = {version = "0.10.16", path = "../../../common"}
swc_ecma_ast = {version = "0.45.0", path = "../../ast"} swc_ecma_ast = {version = "0.45.0", path = "../../ast"}
swc_ecma_transforms_base = {version = "0.15.0", path = "../base"} swc_ecma_transforms_base = {version = "0.15.0", path = "../base"}
swc_ecma_transforms_classes = {version = "0.1.0", path = "../classes"}
swc_ecma_transforms_macros = {version = "0.2.1", path = "../macros"} swc_ecma_transforms_macros = {version = "0.2.1", path = "../macros"}
swc_ecma_utils = {version = "0.36.0", path = "../../utils"} swc_ecma_utils = {version = "0.36.0", path = "../../utils"}
swc_ecma_visit = {version = "0.31.0", path = "../../visit"} swc_ecma_visit = {version = "0.31.0", path = "../../visit"}

View File

@ -1,9 +1,10 @@
use super::get_prototype_of;
use std::iter; use std::iter;
use swc_atoms::JsWord; use swc_atoms::JsWord;
use swc_common::{Mark, DUMMY_SP}; use swc_common::{Mark, DUMMY_SP};
use swc_ecma_ast::*; use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper; use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_classes::fold_only_key;
use swc_ecma_transforms_classes::get_prototype_of;
use swc_ecma_utils::quote_ident; use swc_ecma_utils::quote_ident;
use swc_ecma_utils::ExprFactory; use swc_ecma_utils::ExprFactory;
use swc_ecma_visit::noop_visit_type; use swc_ecma_visit::noop_visit_type;

View File

@ -1,4 +1,3 @@
pub(crate) use self::super_field::SuperFieldAccessFolder;
use self::{ use self::{
constructor::{ constructor::{
constructor_fn, make_possible_return_value, replace_this_in_constructor, ConstructorFolder, constructor_fn, make_possible_return_value, replace_this_in_constructor, ConstructorFolder,
@ -14,6 +13,7 @@ use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper; use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_base::native::is_native; use swc_ecma_transforms_base::native::is_native;
use swc_ecma_transforms_base::perf::Check; use swc_ecma_transforms_base::perf::Check;
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_transforms_macros::fast_path; use swc_ecma_transforms_macros::fast_path;
use swc_ecma_utils::quote_expr; use swc_ecma_utils::quote_expr;
use swc_ecma_utils::quote_str; use swc_ecma_utils::quote_str;
@ -25,11 +25,8 @@ use swc_ecma_utils::{private_ident, quote_ident};
use swc_ecma_visit::noop_visit_type; use swc_ecma_visit::noop_visit_type;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Node, Visit, VisitWith}; use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Node, Visit, VisitWith};
#[macro_use]
mod macros;
mod constructor; mod constructor;
mod prop_name; mod prop_name;
mod super_field;
pub fn classes<C>(comments: Option<C>) -> impl Fold pub fn classes<C>(comments: Option<C>) -> impl Fold
where where
@ -864,20 +861,6 @@ where
} }
} }
/// Creates
///
/// ```js
/// Child.__proto__ || Object.getPrototypeOf(Child)
/// ```
fn get_prototype_of(obj: Expr) -> Expr {
Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(get_prototype_of, "getPrototypeOf"),
args: vec![obj.as_arg()],
type_args: Default::default(),
})
}
fn inject_class_call_check(c: &mut Vec<Stmt>, name: Ident) { fn inject_class_call_check(c: &mut Vec<Stmt>, name: Ident) {
let class_call_check = CallExpr { let class_call_check = CallExpr {
span: DUMMY_SP, span: DUMMY_SP,

View File

@ -4,7 +4,6 @@ use self::{
this_in_static::ThisInStaticFolder, this_in_static::ThisInStaticFolder,
used_name::{UsedNameCollector, UsedNameRenamer}, used_name::{UsedNameCollector, UsedNameRenamer},
}; };
use crate::es2015::classes::SuperFieldAccessFolder;
use std::{collections::HashSet, mem::take}; use std::{collections::HashSet, mem::take};
use swc_atoms::JsWord; use swc_atoms::JsWord;
use swc_common::SyntaxContext; use swc_common::SyntaxContext;
@ -12,6 +11,7 @@ use swc_common::{util::move_map::MoveMap, Mark, Spanned, DUMMY_SP};
use swc_ecma_ast::*; use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper; use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_base::perf::Check; use swc_ecma_transforms_base::perf::Check;
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_transforms_macros::fast_path; use swc_ecma_transforms_macros::fast_path;
use swc_ecma_utils::private_ident; use swc_ecma_utils::private_ident;
use swc_ecma_utils::quote_ident; use swc_ecma_utils::quote_ident;

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_optimization" name = "swc_ecma_transforms_optimization"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.20.1" version = "0.20.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]

View File

@ -204,6 +204,24 @@ impl VisitMut for Dce<'_> {
preserve!(visit_mut_break_stmt, BreakStmt); preserve!(visit_mut_break_stmt, BreakStmt);
preserve!(visit_mut_continue_stmt, ContinueStmt); preserve!(visit_mut_continue_stmt, ContinueStmt);
fn visit_mut_bin_expr(&mut self, node: &mut BinExpr) {
if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.is_marked(node.left.span())
|| self.is_marked(node.right.span())
{
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.left);
self.mark(&mut node.right);
}
}
fn visit_mut_block_stmt(&mut self, node: &mut BlockStmt) { fn visit_mut_block_stmt(&mut self, node: &mut BlockStmt) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -216,6 +234,40 @@ impl VisitMut for Dce<'_> {
} }
} }
fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.is_marked(node.callee.span())
|| node.args.iter().any(|arg| self.is_marked(arg.expr.span()))
{
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.callee);
self.mark(&mut node.args);
}
}
fn visit_mut_class_decl(&mut self, node: &mut ClassDecl) {
if self.is_marked(node.span()) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.included.contains(&node.ident.to_id())
|| self.is_marked(node.ident.span())
|| self.is_marked(node.class.span())
{
self.mark(&mut node.ident);
self.mark(&mut node.class);
}
}
fn visit_mut_do_while_stmt(&mut self, node: &mut DoWhileStmt) { fn visit_mut_do_while_stmt(&mut self, node: &mut DoWhileStmt) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -500,6 +552,13 @@ impl VisitMut for Dce<'_> {
} }
} }
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
self.visit_mut_stmt_like(n)
}
normal!(visit_mut_throw_stmt, ThrowStmt, arg);
normal!(visit_mut_try_stmt, TryStmt, block, handler, finalizer);
fn visit_mut_named_export(&mut self, node: &mut NamedExport) { fn visit_mut_named_export(&mut self, node: &mut NamedExport) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -520,6 +579,26 @@ impl VisitMut for Dce<'_> {
} }
} }
fn visit_mut_new_expr(&mut self, node: &mut NewExpr) {
if self.is_marked(node.span) {
return;
}
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.callee);
self.mark(&mut node.args);
}
fn visit_mut_private_name(&mut self, _: &mut PrivateName) {}
fn visit_mut_prop_name(&mut self, n: &mut PropName) {
match n {
PropName::Computed(_) => n.visit_mut_children_with(self),
_ => {}
}
}
fn visit_mut_return_stmt(&mut self, node: &mut ReturnStmt) { fn visit_mut_return_stmt(&mut self, node: &mut ReturnStmt) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -529,6 +608,14 @@ impl VisitMut for Dce<'_> {
self.mark(&mut node.arg); self.mark(&mut node.arg);
} }
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
if !self.decl_dropping_phase {
n.visit_mut_children_with(self);
return;
}
self.visit_mut_stmt_like(n)
}
fn visit_mut_switch_case(&mut self, node: &mut SwitchCase) { fn visit_mut_switch_case(&mut self, node: &mut SwitchCase) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -566,8 +653,19 @@ impl VisitMut for Dce<'_> {
} }
} }
normal!(visit_mut_throw_stmt, ThrowStmt, arg); fn visit_mut_unary_expr(&mut self, node: &mut UnaryExpr) {
normal!(visit_mut_try_stmt, TryStmt, block, handler, finalizer); if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase || self.is_marked(node.arg.span()) {
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.arg);
}
}
fn visit_mut_update_expr(&mut self, node: &mut UpdateExpr) { fn visit_mut_update_expr(&mut self, node: &mut UpdateExpr) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
@ -578,18 +676,6 @@ impl VisitMut for Dce<'_> {
self.mark(&mut node.arg); self.mark(&mut node.arg);
} }
fn visit_mut_var_declarator(&mut self, d: &mut VarDeclarator) {
if self.is_marked(d.span) {
return;
}
d.visit_mut_children_with(self);
if self.is_marked(d.name.span()) || self.is_marked(d.init.span()) {
d.span = d.span.apply_mark(self.config.used_mark);
}
}
fn visit_mut_var_decl(&mut self, mut var: &mut VarDecl) { fn visit_mut_var_decl(&mut self, mut var: &mut VarDecl) {
if self.is_marked(var.span) { if self.is_marked(var.span) {
return; return;
@ -622,6 +708,18 @@ impl VisitMut for Dce<'_> {
var.span = var.span.apply_mark(self.config.used_mark); var.span = var.span.apply_mark(self.config.used_mark);
} }
fn visit_mut_var_declarator(&mut self, d: &mut VarDeclarator) {
if self.is_marked(d.span) {
return;
}
d.visit_mut_children_with(self);
if self.is_marked(d.name.span()) || self.is_marked(d.init.span()) {
d.span = d.span.apply_mark(self.config.used_mark);
}
}
fn visit_mut_while_stmt(&mut self, node: &mut WhileStmt) { fn visit_mut_while_stmt(&mut self, node: &mut WhileStmt) {
if self.is_marked(node.span) { if self.is_marked(node.span) {
return; return;
@ -637,104 +735,6 @@ impl VisitMut for Dce<'_> {
} }
} }
fn visit_mut_unary_expr(&mut self, node: &mut UnaryExpr) {
if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase || self.is_marked(node.arg.span()) {
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.arg);
}
}
fn visit_mut_bin_expr(&mut self, node: &mut BinExpr) {
if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.is_marked(node.left.span())
|| self.is_marked(node.right.span())
{
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.left);
self.mark(&mut node.right);
}
}
fn visit_mut_new_expr(&mut self, node: &mut NewExpr) {
if self.is_marked(node.span) {
return;
}
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.callee);
self.mark(&mut node.args);
}
fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
if self.is_marked(node.span) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.is_marked(node.callee.span())
|| node.args.iter().any(|arg| self.is_marked(arg.expr.span()))
{
node.span = node.span.apply_mark(self.config.used_mark);
self.mark(&mut node.callee);
self.mark(&mut node.args);
}
}
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
self.visit_mut_stmt_like(n)
}
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
if !self.decl_dropping_phase {
n.visit_mut_children_with(self);
return;
}
self.visit_mut_stmt_like(n)
}
fn visit_mut_private_name(&mut self, _: &mut PrivateName) {}
fn visit_mut_prop_name(&mut self, n: &mut PropName) {
match n {
PropName::Computed(_) => n.visit_mut_children_with(self),
_ => {}
}
}
fn visit_mut_class_decl(&mut self, node: &mut ClassDecl) {
if self.is_marked(node.span()) {
return;
}
node.visit_mut_children_with(self);
if self.marking_phase
|| self.included.contains(&node.ident.to_id())
|| self.is_marked(node.ident.span())
|| self.is_marked(node.class.span())
{
self.mark(&mut node.ident);
self.mark(&mut node.class);
}
}
// ---- ---- ----- ---- ----- // ---- ---- ----- ---- -----
// If Spanned::span is synthesized, we should some work // If Spanned::span is synthesized, we should some work
@ -919,9 +919,10 @@ impl Dce<'_> {
pub fn has_marked_elem<T>(&self, n: &[T]) -> bool pub fn has_marked_elem<T>(&self, n: &[T]) -> bool
where where
T: Spanned, T: Spanned + IsDecorator,
{ {
n.iter().any(|n| self.is_marked(n.span())) n.iter()
.any(|n| T::is_decorator() || self.is_marked(n.span()))
} }
pub fn should_preserve_export(&self, i: &JsWord) -> bool { pub fn should_preserve_export(&self, i: &JsWord) -> bool {
@ -980,3 +981,59 @@ impl Dce<'_> {
self.decl_dropping_phase = old; self.decl_dropping_phase = old;
} }
} }
trait IsDecorator {
fn is_decorator() -> bool;
}
impl<T> IsDecorator for Option<T>
where
T: IsDecorator,
{
fn is_decorator() -> bool {
T::is_decorator()
}
}
impl<T> IsDecorator for Box<T>
where
T: IsDecorator,
{
fn is_decorator() -> bool {
T::is_decorator()
}
}
impl IsDecorator for Decorator {
fn is_decorator() -> bool {
true
}
}
macro_rules! not_decorator {
($T:ty) => {
impl IsDecorator for $T {
fn is_decorator() -> bool {
false
}
}
};
($($T:ty,)*) => {
$(
not_decorator!($T);
)*
};
}
not_decorator!(
ExprOrSpread,
Pat,
ClassMember,
ParamOrTsParamProp,
PropOrSpread,
Prop,
SpreadElement,
ObjectPatProp,
Expr,
);

View File

@ -0,0 +1,6 @@
@decorator
class Class { }
function decorator(cls) {
console.log(cls.name);
}

View File

@ -0,0 +1,6 @@
@decorator
class Class {
}
function decorator(cls) {
console.log(cls.name);
}

View File

@ -0,0 +1,6 @@
@decorator
class Class {
}
function decorator(cls) {
console.log(cls.name);
}

View File

@ -12,6 +12,7 @@ fn dce_single_pass(input: PathBuf) {
test_fixture( test_fixture(
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
decorators: true,
dynamic_import: true, dynamic_import: true,
..Default::default() ..Default::default()
}), }),
@ -27,6 +28,7 @@ fn dce_repeated(input: PathBuf) {
test_fixture( test_fixture(
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
decorators: true,
dynamic_import: true, dynamic_import: true,
..Default::default() ..Default::default()
}), }),

View File

@ -25,6 +25,7 @@ swc_ecma_ast = {version = "0.45.0", path = "../../ast"}
swc_ecma_loader = {version = "0.6.0", path = "../../loader", optional = true} swc_ecma_loader = {version = "0.6.0", path = "../../loader", optional = true}
swc_ecma_parser = {version = "0.57.0", path = "../../parser"} swc_ecma_parser = {version = "0.57.0", path = "../../parser"}
swc_ecma_transforms_base = {version = "0.15.0", path = "../base"} swc_ecma_transforms_base = {version = "0.15.0", path = "../base"}
swc_ecma_transforms_classes = {version = "0.1.0", path = "../classes"}
swc_ecma_utils = {version = "0.36.0", path = "../../utils"} swc_ecma_utils = {version = "0.36.0", path = "../../utils"}
swc_ecma_visit = {version = "0.31.0", path = "../../visit"} swc_ecma_visit = {version = "0.31.0", path = "../../visit"}

View File

@ -1,9 +1,12 @@
use either::Either; use either::Either;
use serde::Deserialize; use serde::Deserialize;
use std::iter; use std::iter;
use std::mem::take;
use swc_common::{Spanned, DUMMY_SP}; use swc_common::{Spanned, DUMMY_SP};
use swc_ecma_ast::*; use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper; use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
use swc_ecma_utils::prepend;
use swc_ecma_utils::private_ident; use swc_ecma_utils::private_ident;
use swc_ecma_utils::quote_ident; use swc_ecma_utils::quote_ident;
use swc_ecma_utils::quote_str; use swc_ecma_utils::quote_str;
@ -64,6 +67,7 @@ pub fn decorators(c: Config) -> impl Fold {
} }
Either::Right(Decorators { Either::Right(Decorators {
is_in_strict: false, is_in_strict: false,
vars: Default::default(),
}) })
} }
} }
@ -79,6 +83,8 @@ pub struct Config {
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct Decorators { struct Decorators {
is_in_strict: bool, is_in_strict: bool,
vars: Vec<VarDeclarator>,
} }
impl Fold for Decorators { impl Fold for Decorators {
@ -243,12 +249,25 @@ impl Fold for Decorators {
}); });
self.is_in_strict = old_strict; self.is_in_strict = old_strict;
if !self.vars.is_empty() {
prepend(
&mut buf,
ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: take(&mut self.vars),
}))),
)
}
buf buf
} }
} }
impl Decorators { impl Decorators {
fn fold_class_inner(&self, ident: Ident, mut class: Class) -> Expr { fn fold_class_inner(&mut self, ident: Ident, mut class: Class) -> Expr {
let initialize = private_ident!("_initialize"); let initialize = private_ident!("_initialize");
let super_class_ident = match class.super_class { let super_class_ident = match class.super_class {
Some(ref expr) => Some(alias_ident_for(expr, "_super")), Some(ref expr) => Some(alias_ident_for(expr, "_super")),
@ -324,9 +343,26 @@ impl Decorators {
} }
}; };
let mut vars = vec![];
macro_rules! fold_method { macro_rules! fold_method {
($method:expr, $fn_name:expr, $key_prop_value:expr) => {{ ($method:expr, $fn_name:expr, $key_prop_value:expr) => {{
let fn_name = $fn_name; let fn_name = $fn_name;
let method = $method;
let mut folder = SuperFieldAccessFolder {
class_name: &ident,
vars: &mut vars,
constructor_this_mark: None,
is_static: method.is_static,
folding_constructor: true,
in_nested_scope: false,
in_injected_define_property_call: false,
this_alias_mark: None,
};
let method = method.fold_with(&mut folder);
// kind: "method", // kind: "method",
// key: getKeyJ(), // key: getKeyJ(),
// value: function () { // value: function () {
@ -339,7 +375,7 @@ impl Decorators {
KeyValueProp { KeyValueProp {
key: PropName::Ident(quote_ident!("kind")), key: PropName::Ident(quote_ident!("kind")),
value: Box::new(Expr::Lit(Lit::Str(quote_str!( value: Box::new(Expr::Lit(Lit::Str(quote_str!(
match $method.kind { match method.kind {
MethodKind::Method => "method", MethodKind::Method => "method",
MethodKind::Getter => "get", MethodKind::Getter => "get",
MethodKind::Setter => "set", MethodKind::Setter => "set",
@ -347,7 +383,7 @@ impl Decorators {
)))), )))),
}, },
)))) ))))
.chain(if $method.is_static { .chain(if method.is_static {
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("static")), key: PropName::Ident(quote_ident!("static")),
value: Box::new(Expr::Lit(Lit::Bool(Bool { value: Box::new(Expr::Lit(Lit::Bool(Bool {
@ -360,14 +396,14 @@ impl Decorators {
}) })
.chain({ .chain({
// //
if $method.function.decorators.is_empty() { if method.function.decorators.is_empty() {
None None
} else { } else {
Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { Some(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("decorators")), key: PropName::Ident(quote_ident!("decorators")),
value: Box::new(Expr::Array(ArrayLit { value: Box::new(Expr::Array(ArrayLit {
span: DUMMY_SP, span: DUMMY_SP,
elems: $method elems: method
.function .function
.decorators .decorators
.into_iter() .into_iter()
@ -392,7 +428,7 @@ impl Decorators {
ident: fn_name.map(IdentExt::private), ident: fn_name.map(IdentExt::private),
function: Function { function: Function {
decorators: vec![], decorators: vec![],
..$method.function ..method.function
}, },
} }
.into(), .into(),
@ -420,7 +456,8 @@ impl Decorators {
PropName::Str(ref s) => Some(Ident::new(s.value.clone(), s.span)), PropName::Str(ref s) => Some(Ident::new(s.value.clone(), s.span)),
_ => None, _ => None,
}; };
let key_prop_value = Box::new(prop_name_to_expr_value(method.key)); let key_prop_value = Box::new(prop_name_to_expr_value(method.key.clone()));
fold_method!(method, fn_name, key_prop_value) fold_method!(method, fn_name, key_prop_value)
} }
ClassMember::PrivateMethod(method) => { ClassMember::PrivateMethod(method) => {
@ -430,7 +467,7 @@ impl Decorators {
); );
let key_prop_value = Box::new(Expr::Lit(Lit::Str(Str { let key_prop_value = Box::new(Expr::Lit(Lit::Str(Str {
span: method.key.id.span, span: method.key.id.span,
value: method.key.id.sym, value: method.key.id.sym.clone(),
has_escape: false, has_escape: false,
kind: StrKind::Normal { kind: StrKind::Normal {
contains_quote: false, contains_quote: false,
@ -537,6 +574,8 @@ impl Decorators {
.map(Some) .map(Some)
.collect(); .collect();
self.vars.extend(vars);
Expr::Call(make_decorate_call( Expr::Call(make_decorate_call(
class.decorators, class.decorators,
iter::once({ iter::once({

View File

@ -5736,3 +5736,71 @@ test!(
}) || _class) || _class) || _class) || _class; }) || _class) || _class) || _class) || _class;
"# "#
); );
test!(
ts(),
|_| decorators(decorators::Config {
..Default::default()
}),
issue_846_1,
"
class SomeClass {
@dec
someMethod() {}
}
class OtherClass extends SomeClass {
@dec
anotherMethod() {
super.someMethod()
}
}
",
"
let SomeClass = _decorate([], function(_initialize) {
class SomeClass {
constructor(){
_initialize(this);
}
}
return {
F: SomeClass,
d: [
{
kind: 'method',
decorators: [
dec
],
key: 'someMethod',
value: function someMethod() {
}
}
]
};
});
let OtherClass = _decorate([], function(_initialize, _SomeClass) {
class OtherClass extends _SomeClass {
constructor(...args){
super(...args);
_initialize(this);
}
}
return {
F: OtherClass,
d: [
{
kind: 'method',
decorators: [
dec
],
key: 'anotherMethod',
value: function anotherMethod() {
_get(_getPrototypeOf(OtherClass.prototype), 'someMethod', \
this).call(this);
}
}
]
};
}, SomeClass);
"
);

View File

@ -1,6 +1,6 @@
{ {
"name": "@swc/core", "name": "@swc/core",
"version": "1.2.59", "version": "1.2.60",
"description": "Super-fast alternative for babel", "description": "Super-fast alternative for babel",
"homepage": "https://swc.rs", "homepage": "https://swc.rs",
"main": "./index.js", "main": "./index.js",

View File

@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"decorators": true
}
}
}

View File

@ -0,0 +1,11 @@
class SomeClass {
@dec
someMethod() { }
}
class OtherClass extends SomeClass {
@dec
anotherMethod() {
super.someMethod()
}
}

View File

@ -0,0 +1,481 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _decorate(decorators, factory, superClass) {
var r = factory(function initialize(O) {
_initializeInstanceElements(O, decorated.elements);
}, superClass);
var decorated = _decorateClass(_coalesceClassElements(r.d.map(_createElementDescriptor)), decorators);
_initializeClassElements(r.F, decorated.elements);
return _runClassFinishers(r.F, decorated.finishers);
}
function _createElementDescriptor(def) {
var key = _toPropertyKey(def.key);
var descriptor;
if (def.kind === "method") {
descriptor = {
value: def.value,
writable: true,
configurable: true,
enumerable: false
};
Object.defineProperty(def.value, "name", {
value: _typeof(key) === "symbol" ? "" : key,
configurable: true
});
} else if (def.kind === "get") {
descriptor = {
get: def.value,
configurable: true,
enumerable: false
};
} else if (def.kind === "set") {
descriptor = {
set: def.value,
configurable: true,
enumerable: false
};
} else if (def.kind === "field") {
descriptor = {
configurable: true,
writable: true,
enumerable: true
};
}
var element = {
kind: def.kind === "field" ? "field" : "method",
key: key,
placement: def.static ? "static" : def.kind === "field" ? "own" : "prototype",
descriptor: descriptor
};
if (def.decorators) element.decorators = def.decorators;
if (def.kind === "field") element.initializer = def.value;
return element;
}
function _coalesceGetterSetter(element, other) {
if (element.descriptor.get !== undefined) {
other.descriptor.get = element.descriptor.get;
} else {
other.descriptor.set = element.descriptor.set;
}
}
function _coalesceClassElements(elements) {
var newElements = [];
var isSameElement = function isSameElement(other) {
return other.kind === "method" && other.key === element.key && other.placement === element.placement;
};
for(var i = 0; i < elements.length; i++){
var element = elements[i];
var other;
if (element.kind === "method" && (other = newElements.find(isSameElement))) {
if (_isDataDescriptor(element.descriptor) || _isDataDescriptor(other.descriptor)) {
if (_hasDecorators(element) || _hasDecorators(other)) {
throw new ReferenceError("Duplicated methods (" + element.key + ") can't be decorated.");
}
other.descriptor = element.descriptor;
} else {
if (_hasDecorators(element)) {
if (_hasDecorators(other)) {
throw new ReferenceError("Decorators can't be placed on different accessors with for " + "the same property (" + element.key + ").");
}
other.decorators = element.decorators;
}
_coalesceGetterSetter(element, other);
}
} else {
newElements.push(element);
}
}
return newElements;
}
function _hasDecorators(element) {
return element.decorators && element.decorators.length;
}
function _isDataDescriptor(desc) {
return desc !== undefined && !(desc.value === undefined && desc.writable === undefined);
}
function _initializeClassElements(F, elements) {
var proto = F.prototype;
[
"method",
"field"
].forEach(function(kind) {
elements.forEach(function(element) {
var placement = element.placement;
if (element.kind === kind && (placement === "static" || placement === "prototype")) {
var receiver = placement === "static" ? F : proto;
_defineClassElement(receiver, element);
}
});
});
}
function _initializeInstanceElements(O, elements) {
[
"method",
"field"
].forEach(function(kind) {
elements.forEach(function(element) {
if (element.kind === kind && element.placement === "own") {
_defineClassElement(O, element);
}
});
});
}
function _defineClassElement(receiver, element) {
var descriptor = element.descriptor;
if (element.kind === "field") {
var initializer = element.initializer;
descriptor = {
enumerable: descriptor.enumerable,
writable: descriptor.writable,
configurable: descriptor.configurable,
value: initializer === void 0 ? void 0 : initializer.call(receiver)
};
}
Object.defineProperty(receiver, element.key, descriptor);
}
function _decorateClass(elements, decorators) {
var newElements = [];
var finishers = [];
var placements = {
static: [],
prototype: [],
own: []
};
elements.forEach(function(element) {
_addElementPlacement(element, placements);
});
elements.forEach(function(element) {
if (!_hasDecorators(element)) return newElements.push(element);
var elementFinishersExtras = _decorateElement(element, placements);
newElements.push(elementFinishersExtras.element);
newElements.push.apply(newElements, elementFinishersExtras.extras);
finishers.push.apply(finishers, elementFinishersExtras.finishers);
});
if (!decorators) {
return {
elements: newElements,
finishers: finishers
};
}
var result = _decorateConstructor(newElements, decorators);
finishers.push.apply(finishers, result.finishers);
result.finishers = finishers;
return result;
}
function _addElementPlacement(element, placements, silent) {
var keys = placements[element.placement];
if (!silent && keys.indexOf(element.key) !== -1) {
throw new TypeError("Duplicated element (" + element.key + ")");
}
keys.push(element.key);
}
function _decorateElement(element, placements) {
var extras = [];
var finishers = [];
for(var decorators = element.decorators, i = decorators.length - 1; i >= 0; i--){
var keys = placements[element.placement];
keys.splice(keys.indexOf(element.key), 1);
var elementObject = _fromElementDescriptor(element);
var elementFinisherExtras = _toElementFinisherExtras((0, decorators[i])(elementObject) || elementObject);
element = elementFinisherExtras.element;
_addElementPlacement(element, placements);
if (elementFinisherExtras.finisher) {
finishers.push(elementFinisherExtras.finisher);
}
var newExtras = elementFinisherExtras.extras;
if (newExtras) {
for(var j = 0; j < newExtras.length; j++){
_addElementPlacement(newExtras[j], placements);
}
extras.push.apply(extras, newExtras);
}
}
return {
element: element,
finishers: finishers,
extras: extras
};
}
function _decorateConstructor(elements, decorators) {
var finishers = [];
for(var i = decorators.length - 1; i >= 0; i--){
var obj = _fromClassDescriptor(elements);
var elementsAndFinisher = _toClassDescriptor((0, decorators[i])(obj) || obj);
if (elementsAndFinisher.finisher !== undefined) {
finishers.push(elementsAndFinisher.finisher);
}
if (elementsAndFinisher.elements !== undefined) {
elements = elementsAndFinisher.elements;
for(var j = 0; j < elements.length - 1; j++){
for(var k = j + 1; k < elements.length; k++){
if (elements[j].key === elements[k].key && elements[j].placement === elements[k].placement) {
throw new TypeError("Duplicated element (" + elements[j].key + ")");
}
}
}
}
}
return {
elements: elements,
finishers: finishers
};
}
function _fromElementDescriptor(element) {
var obj = {
kind: element.kind,
key: element.key,
placement: element.placement,
descriptor: element.descriptor
};
var desc = {
value: "Descriptor",
configurable: true
};
Object.defineProperty(obj, Symbol.toStringTag, desc);
if (element.kind === "field") obj.initializer = element.initializer;
return obj;
}
function _toElementDescriptors(elementObjects) {
if (elementObjects === undefined) return;
return _toArray(elementObjects).map(function(elementObject) {
var element = _toElementDescriptor(elementObject);
_disallowProperty(elementObject, "finisher", "An element descriptor");
_disallowProperty(elementObject, "extras", "An element descriptor");
return element;
});
}
function _toElementDescriptor(elementObject) {
var kind = String(elementObject.kind);
if (kind !== "method" && kind !== "field") {
throw new TypeError("An element descriptor's .kind property must be either \"method\" or" + " \"field\", but a decorator created an element descriptor with" + " .kind \"" + kind + "\"");
}
var key = _toPropertyKey(elementObject.key);
var placement = String(elementObject.placement);
if (placement !== "static" && placement !== "prototype" && placement !== "own") {
throw new TypeError("An element descriptor's .placement property must be one of \"static\"," + " \"prototype\" or \"own\", but a decorator created an element descriptor" + " with .placement \"" + placement + "\"");
}
var descriptor = elementObject.descriptor;
_disallowProperty(elementObject, "elements", "An element descriptor");
var element = {
kind: kind,
key: key,
placement: placement,
descriptor: Object.assign({
}, descriptor)
};
if (kind !== "field") {
_disallowProperty(elementObject, "initializer", "A method descriptor");
} else {
_disallowProperty(descriptor, "get", "The property descriptor of a field descriptor");
_disallowProperty(descriptor, "set", "The property descriptor of a field descriptor");
_disallowProperty(descriptor, "value", "The property descriptor of a field descriptor");
element.initializer = elementObject.initializer;
}
return element;
}
function _toElementFinisherExtras(elementObject) {
var element = _toElementDescriptor(elementObject);
var finisher = _optionalCallableProperty(elementObject, "finisher");
var extras = _toElementDescriptors(elementObject.extras);
return {
element: element,
finisher: finisher,
extras: extras
};
}
function _fromClassDescriptor(elements) {
var obj = {
kind: "class",
elements: elements.map(_fromElementDescriptor)
};
var desc = {
value: "Descriptor",
configurable: true
};
Object.defineProperty(obj, Symbol.toStringTag, desc);
return obj;
}
function _toClassDescriptor(obj) {
var kind = String(obj.kind);
if (kind !== "class") {
throw new TypeError("A class descriptor's .kind property must be \"class\", but a decorator" + " created a class descriptor with .kind \"" + kind + "\"");
}
_disallowProperty(obj, "key", "A class descriptor");
_disallowProperty(obj, "placement", "A class descriptor");
_disallowProperty(obj, "descriptor", "A class descriptor");
_disallowProperty(obj, "initializer", "A class descriptor");
_disallowProperty(obj, "extras", "A class descriptor");
var finisher = _optionalCallableProperty(obj, "finisher");
var elements = _toElementDescriptors(obj.elements);
return {
elements: elements,
finisher: finisher
};
}
function _disallowProperty(obj, name, objectType) {
if (obj[name] !== undefined) {
throw new TypeError(objectType + " can't have a ." + name + " property.");
}
}
function _optionalCallableProperty(obj, name) {
var value = obj[name];
if (value !== undefined && typeof value !== "function") {
throw new TypeError("Expected '" + name + "' to be a function");
}
return value;
}
function _runClassFinishers(constructor, finishers) {
for(var i = 0; i < finishers.length; i++){
var newConstructor = (0, finishers[i])(constructor);
if (newConstructor !== undefined) {
if (typeof newConstructor !== "function") {
throw new TypeError("Finishers must return a constructor.");
}
constructor = newConstructor;
}
}
return constructor;
}
function _get(target, property, receiver) {
if (typeof Reflect !== "undefined" && Reflect.get) {
_get = Reflect.get;
} else {
_get = function _get(target, property, receiver) {
var base = _superPropBase(target, property);
if (!base) return;
var desc = Object.getOwnPropertyDescriptor(base, property);
if (desc.get) {
return desc.get.call(receiver);
}
return desc.value;
};
}
return _get(target, property, receiver || target);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _superPropBase(object, property) {
while(!Object.prototype.hasOwnProperty.call(object, property)){
object = _getPrototypeOf(object);
if (object === null) break;
}
return object;
}
function _toArray(arr) {
return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
}
function _toPrimitive(input, hint) {
if (_typeof(input) !== "object" || input === null) return input;
var prim = input[Symbol.toPrimitive];
if (prim !== undefined) {
var res = prim.call(input, hint || "default");
if (_typeof(res) !== "object") return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, "string");
return _typeof(key) === "symbol" ? key : String(key);
}
var _typeof = function(obj) {
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
var SomeClass = _decorate([], function(_initialize) {
var SomeClass1 = function SomeClass1() {
"use strict";
_classCallCheck(this, SomeClass1);
_initialize(this);
};
return {
F: SomeClass1,
d: [
{
kind: "method",
decorators: [
dec
],
key: "someMethod",
value: function someMethod() {
}
}
]
};
});
var OtherClass = _decorate([], function(_initialize, _SomeClass) {
var OtherClass1 = /*#__PURE__*/ function(_SomeClass1) {
"use strict";
_inherits(OtherClass1, _SomeClass1);
function OtherClass1() {
_classCallCheck(this, OtherClass1);
var _this;
_this = _possibleConstructorReturn(this, _getPrototypeOf(OtherClass1).apply(this, arguments));
_initialize(_assertThisInitialized(_this));
return _this;
}
return OtherClass1;
}(_SomeClass);
return {
F: OtherClass1,
d: [
{
kind: "method",
decorators: [
dec
],
key: "anotherMethod",
value: function anotherMethod() {
_get(_getPrototypeOf(OtherClass1.prototype), "someMethod", this).call(this);
}
}
]
};
}, SomeClass);

View File

@ -6,7 +6,7 @@ license = "Apache-2.0/MIT"
name = "wasm" name = "wasm"
publish = false publish = false
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "1.2.59" version = "1.2.60"
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]