Skip to content

Commit

Permalink
fix(swc): Fix bugs (swc-project#1453)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_typescript:
  - Add option to use define propert for `strip` pass. (swc-project#1472)

node-swc:
  - Fix `keepClassNames`. (swc-project/swc-node#335)
  • Loading branch information
kdy1 committed Mar 17, 2021
1 parent 9d53a70 commit dcdac2d
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 35 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ jobs:
run: |
export PATH="$PATH:$HOME/npm/bin"
cargo test --color always -p ${{ matrix.crate }}
#
deploy-docs:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
needs:
- test
steps:
- uses: actions/checkout@v2
- name: Create rustdoc
run: cargo doc
# We use travis for rustdocs
# deploy-docs:
# runs-on: ubuntu-latest
# if: github.ref == 'refs/heads/master'
# needs:
# - test
# steps:
# - uses: actions/checkout@v2
# - name: Create rustdoc
# run: cargo doc
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc"
repository = "https://github.com/swc-project/swc.git"
version = "0.9.0"
version = "0.10.0"

[lib]
name = "swc"
Expand All @@ -33,8 +33,8 @@ swc_ecma_ast = {version = "0.40.0", path = "./ecmascript/ast"}
swc_ecma_codegen = {version = "0.48.0", path = "./ecmascript/codegen"}
swc_ecma_ext_transforms = {version = "0.7.0", path = "./ecmascript/ext-transforms"}
swc_ecma_parser = {version = "0.50.0", path = "./ecmascript/parser"}
swc_ecma_preset_env = {version = "0.9.0", path = "./ecmascript/preset_env"}
swc_ecma_transforms = {version = "0.39.0", path = "./ecmascript/transforms", features = [
swc_ecma_preset_env = {version = "0.10.0", path = "./ecmascript/preset_env"}
swc_ecma_transforms = {version = "0.40.0", path = "./ecmascript/transforms", features = [
"compat",
"module",
"optimization",
Expand Down
6 changes: 3 additions & 3 deletions bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include = ["Cargo.toml", "build.rs", "src/**/*.rs", "src/**/*.js"]
license = "Apache-2.0/MIT"
name = "swc_bundler"
repository = "https://github.com/swc-project/swc.git"
version = "0.26.0"
version = "0.27.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
Expand All @@ -35,7 +35,7 @@ swc_common = {version = "0.10.10", path = "../common"}
swc_ecma_ast = {version = "0.40.0", path = "../ecmascript/ast"}
swc_ecma_codegen = {version = "0.48.0", path = "../ecmascript/codegen"}
swc_ecma_parser = {version = "0.50.0", path = "../ecmascript/parser"}
swc_ecma_transforms = {version = "0.39.0", path = "../ecmascript/transforms", features = ["optimization"]}
swc_ecma_transforms = {version = "0.40.0", path = "../ecmascript/transforms", features = ["optimization"]}
swc_ecma_utils = {version = "0.30.0", path = "../ecmascript/utils"}
swc_ecma_visit = {version = "0.26.0", path = "../ecmascript/visit"}

Expand All @@ -44,7 +44,7 @@ hex = "0.4"
ntest = "0.7.2"
reqwest = {version = "0.10.8", features = ["blocking"]}
sha-1 = "0.9"
swc_ecma_transforms = {version = "0.39.0", path = "../ecmascript/transforms", features = ["react", "typescript"]}
swc_ecma_transforms = {version = "0.40.0", path = "../ecmascript/transforms", features = ["react", "typescript"]}
tempfile = "3.1.0"
testing = {version = "0.10.3", path = "../testing"}
url = "2.1.1"
Expand Down
4 changes: 2 additions & 2 deletions ecmascript/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_ecmascript"
repository = "https://github.com/swc-project/swc.git"
version = "0.25.0"
version = "0.26.0"

[package.metadata.docs.rs]
all-features = true
Expand All @@ -31,7 +31,7 @@ swc_ecma_ast = {version = "0.40.0", path = "./ast"}
swc_ecma_codegen = {version = "0.48.0", path = "./codegen", optional = true}
swc_ecma_dep_graph = {version = "0.18.0", path = "./dep-graph", optional = true}
swc_ecma_parser = {version = "0.50.0", path = "./parser", optional = true}
swc_ecma_transforms = {version = "0.39.0", path = "./transforms", optional = true}
swc_ecma_transforms = {version = "0.40.0", path = "./transforms", optional = true}
swc_ecma_utils = {version = "0.30.0", path = "./utils", optional = true}
swc_ecma_visit = {version = "0.26.0", path = "./visit", optional = true}

Expand Down
4 changes: 2 additions & 2 deletions ecmascript/preset_env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ documentation = "https://swc.rs/rustdoc/swc_ecma_preset_env/"
edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_preset_env"
version = "0.9.0"
version = "0.10.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -21,7 +21,7 @@ string_enum = {version = "0.3.1", path = "../../macros/string_enum"}
swc_atoms = {version = "0.2", path = "../../atoms"}
swc_common = {version = "0.10.10", path = "../../common"}
swc_ecma_ast = {version = "0.40.0", path = "../ast"}
swc_ecma_transforms = {version = "0.39.0", path = "../transforms", features = ["compat", "proposal"]}
swc_ecma_transforms = {version = "0.40.0", path = "../transforms", features = ["compat", "proposal"]}
swc_ecma_utils = {version = "0.30.0", path = "../utils"}
swc_ecma_visit = {version = "0.26.0", path = "../visit"}
walkdir = "2"
Expand Down
6 changes: 3 additions & 3 deletions ecmascript/transforms/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"
repository = "https://github.com/swc-project/swc.git"
version = "0.39.0"
version = "0.40.0"

[package.metadata.docs.rs]
all-features = true
Expand All @@ -27,10 +27,10 @@ swc_ecma_parser = {version = "0.50.0", path = "../parser"}
swc_ecma_transforms_base = {version = "0.7.0", path = "./base"}
swc_ecma_transforms_compat = {version = "0.8.0", path = "./compat", optional = true}
swc_ecma_transforms_module = {version = "0.8.0", path = "./module", optional = true}
swc_ecma_transforms_optimization = {version = "0.9.0", path = "./optimization", optional = true}
swc_ecma_transforms_optimization = {version = "0.10.0", path = "./optimization", optional = true}
swc_ecma_transforms_proposal = {version = "0.8.0", path = "./proposal", optional = true}
swc_ecma_transforms_react = {version = "0.9.0", path = "./react", optional = true}
swc_ecma_transforms_typescript = {version = "0.8.0", path = "./typescript", optional = true}
swc_ecma_transforms_typescript = {version = "0.9.0", path = "./typescript", optional = true}
swc_ecma_utils = {version = "0.30.0", path = "../utils"}
swc_ecma_visit = {version = "0.26.0", path = "../visit"}
unicode-xid = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions 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.9.0"
version = "0.10.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand All @@ -31,5 +31,5 @@ swc_ecma_transforms_module = {version = "0.8.0", path = "../module"}
swc_ecma_transforms_proposal = {version = "0.8.0", path = "../proposal"}
swc_ecma_transforms_react = {version = "0.9.0", path = "../react"}
swc_ecma_transforms_testing = {version = "0.7.0", path = "../testing"}
swc_ecma_transforms_typescript = {version = "0.8.0", path = "../typescript"}
swc_ecma_transforms_typescript = {version = "0.9.0", path = "../typescript"}
testing = {version = "0.10.0", path = "../../../testing"}
2 changes: 1 addition & 1 deletion ecmascript/transforms/typescript/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_typescript"
repository = "https://github.com/swc-project/swc.git"
version = "0.8.0"
version = "0.9.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
164 changes: 156 additions & 8 deletions ecmascript/transforms/typescript/src/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use swc_atoms::{js_word, JsWord};
use swc_common::{util::move_map::MoveMap, Span, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::MapWithMut;
use swc_ecma_utils::default_constructor;
use swc_ecma_utils::member_expr;
use swc_ecma_utils::prepend_stmts;
use swc_ecma_utils::private_ident;
use swc_ecma_utils::quote_ident;
use swc_ecma_utils::var::VarCollector;
use swc_ecma_utils::ExprFactory;
use swc_ecma_utils::{ident::IdentLike, Id, StmtLike};
Expand All @@ -17,26 +20,33 @@ type EnumValues = FxHashMap<Id, TsLit>;

#[derive(Debug, Serialize, Deserialize)]
#[non_exhaustive]
// TODO(nayeemrmn): The name should be `ImportsNotUsedAsValues`. Rename as a
// breaking change.
pub enum ImportNotUsedAsValues {
pub enum ImportsNotUsedAsValues {
#[serde(rename = "remove")]
Remove,
#[serde(rename = "preserve")]
Preserve,
}

#[deprecated = "ImportNotUsedAsValues is renamed to ImportsNotUsedAsValues"]
pub type ImportNotUsedAsValues = ImportsNotUsedAsValues;

/// This value defaults to `Remove`
impl Default for ImportNotUsedAsValues {
impl Default for ImportsNotUsedAsValues {
fn default() -> Self {
Self::Remove
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Config {
pub import_not_used_as_values: ImportNotUsedAsValues,
#[serde(default)]
pub import_not_used_as_values: ImportsNotUsedAsValues,
/// Use `defineProperty` for class properties.
///
///
/// See https://github.com/swc-project/swc/issues/1472
#[serde(default)]
pub use_define_for_class_fields: bool,
}

pub fn strip_with_config(config: Config) -> impl Fold {
Expand Down Expand Up @@ -135,6 +145,143 @@ impl Strip {
}

impl Strip {
/// Convert class properties to `defineProperty` calls from constructor.
///
/// This should be called after handling constructor parameter properties.
/// This is because this method simply appends `definedProperty` calls to
/// constructor.
fn convert_properties_to_define_property(&mut self, class: &mut Class) {
if !self.config.use_define_for_class_fields {
return;
}

let mut define_property_stmts = vec![];
let mut preserved_members = vec![];
for member in take(&mut class.body) {
match member {
ClassMember::ClassProp(ClassProp {
span,
key,
computed,
value: Some(value),
is_static: false,
..
}) => {
let mut define_property_args = vec![];

define_property_args.push(ThisExpr { span: class.span }.as_arg());
define_property_args.push(match *key {
Expr::Ident(key) if !computed => Lit::Str(Str {
span: key.span,
value: key.sym,
has_escape: false,
kind: StrKind::Normal {
contains_quote: false,
},
})
.as_arg(),
_ => key.as_arg(),
});
define_property_args.push(
ObjectLit {
span: DUMMY_SP,
props: {
let mut props = vec![];

// enumerable: true,
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("enumerable")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
},
))));
// configurable: true,
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("configurable")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
},
))));
// writable: true,
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("writable")),
value: Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: true,
}))),
},
))));

// value: initializer
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!("value")),
value,
},
))));

props
},
}
.as_arg(),
);

let define_property_call = Box::new(Expr::Call(CallExpr {
span,
callee: member_expr!(span, Object.definedProperty).as_callee(),
args: define_property_args,
type_args: Default::default(),
}));
let stmt = Stmt::Expr(ExprStmt {
span,
expr: define_property_call,
});

define_property_stmts.push(stmt);
}
_ => {
preserved_members.push(member);
}
}
}

if !define_property_stmts.is_empty() {
// We should append `definedProperty` calls to a constructor.

// If user have defined a constructor, append to it.
for member in &mut preserved_members {
match member {
ClassMember::Constructor(Constructor {
body: Some(body), ..
}) => {
body.stmts.extend(take(&mut define_property_stmts));
}
_ => {}
}
}

// If there's no constructor, we create one.
if !define_property_stmts.is_empty() {
let mut c = default_constructor(class.super_class.is_some());
c.body
.as_mut()
.unwrap()
.stmts
.extend(take(&mut define_property_stmts));
preserved_members.push(ClassMember::Constructor(c));
}
}

class.body = preserved_members;
}

/// Returns [Some] if the method should be called again.
fn handle_expr<'a>(&mut self, n: &'a mut Expr) -> Vec<&'a mut Expr> {
match n {
Expand Down Expand Up @@ -833,6 +980,7 @@ impl VisitMut for Strip {

n.decorators.visit_mut_with(self);
n.body.visit_mut_with(self);
self.convert_properties_to_define_property(n);
n.super_class.visit_mut_with(self);
}

Expand Down Expand Up @@ -972,8 +1120,8 @@ impl VisitMut for Strip {

if import.specifiers.is_empty() && !self.is_side_effect_import {
self.is_side_effect_import = match self.config.import_not_used_as_values {
ImportNotUsedAsValues::Remove => false,
ImportNotUsedAsValues::Preserve => true,
ImportsNotUsedAsValues::Remove => false,
ImportsNotUsedAsValues::Preserve => true,
};
}
}
Expand Down
Loading

0 comments on commit dcdac2d

Please sign in to comment.