Skip to content

Commit

Permalink
fix(es/transforms): Fix bugs (#1769)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
kdy1 authored May 30, 2021
1 parent 5d219b8 commit ad55711
Show file tree
Hide file tree
Showing 22 changed files with 868 additions and 157 deletions.
17 changes: 17 additions & 0 deletions ecmascript/transforms/classes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"}
22 changes: 22 additions & 0 deletions ecmascript/transforms/classes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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(),
})
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// Not intended for public use.
#[macro_export]
macro_rules! fold_only_key {
() => {
fn fold_class_member(&mut self, m: ClassMember) -> ClassMember {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
///
/// _get(Child.prototype.__proto__ || Object.getPrototypeOf(Child.prototype),
/// 'foo', this).call(this, a);
pub(crate) struct SuperFieldAccessFolder<'a> {
pub struct SuperFieldAccessFolder<'a> {
pub class_name: &'a Ident,

pub vars: &'a mut Vec<VarDeclarator>,
Expand Down
3 changes: 2 additions & 1 deletion ecmascript/transforms/compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_compat"
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

[dependencies]
Expand All @@ -22,6 +22,7 @@ swc_atoms = {version = "0.2.5", path = "../../../atoms"}
swc_common = {version = "0.10.16", path = "../../../common"}
swc_ecma_ast = {version = "0.45.0", path = "../../ast"}
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_utils = {version = "0.36.0", path = "../../utils"}
swc_ecma_visit = {version = "0.31.0", path = "../../visit"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::get_prototype_of;
use std::iter;
use swc_atoms::JsWord;
use swc_common::{Mark, DUMMY_SP};
use swc_ecma_ast::*;
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::ExprFactory;
use swc_ecma_visit::noop_visit_type;
Expand Down
19 changes: 1 addition & 18 deletions ecmascript/transforms/compat/src/es2015/classes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub(crate) use self::super_field::SuperFieldAccessFolder;
use self::{
constructor::{
constructor_fn, make_possible_return_value, replace_this_in_constructor, ConstructorFolder,
Expand All @@ -14,6 +13,7 @@ use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_base::native::is_native;
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_utils::quote_expr;
use swc_ecma_utils::quote_str;
Expand All @@ -25,11 +25,8 @@ use swc_ecma_utils::{private_ident, quote_ident};
use swc_ecma_visit::noop_visit_type;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Node, Visit, VisitWith};

#[macro_use]
mod macros;
mod constructor;
mod prop_name;
mod super_field;

pub fn classes<C>(comments: Option<C>) -> impl Fold
where
Expand Down Expand Up @@ -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) {
let class_call_check = CallExpr {
span: DUMMY_SP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use self::{
this_in_static::ThisInStaticFolder,
used_name::{UsedNameCollector, UsedNameRenamer},
};
use crate::es2015::classes::SuperFieldAccessFolder;
use std::{collections::HashSet, mem::take};
use swc_atoms::JsWord;
use swc_common::SyntaxContext;
use swc_common::{util::move_map::MoveMap, Mark, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
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_utils::private_ident;
use swc_ecma_utils::quote_ident;
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/optimization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_optimization"
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

[dependencies]
Expand Down
Loading

0 comments on commit ad55711

Please sign in to comment.