Skip to content

Commit

Permalink
Do not rename keywords in meta properties (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Sep 9, 2020
1 parent 9be8bf6 commit 3a26d3d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ecmascript/transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ swc_atoms = {version = "0.2.0", path = "../../atoms"}
swc_common = {version = "0.10.0", path = "../../common"}
swc_ecma_ast = {version = "0.31.0", path = "../ast"}
swc_ecma_parser = {version = "0.37.0", path = "../parser"}
swc_ecma_transforms_macros = {version = "0.1.0", path = "./macros"}
swc_ecma_transforms_macros = {version = "0.1.1", path = "./macros"}
swc_ecma_utils = {version = "0.21.0", path = "../utils"}
swc_ecma_visit = {version = "0.17.0", path = "../visit"}
unicode-xid = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swc_ecma_transforms_macros"
version = "0.1.0"
version = "0.1.1"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"
Expand Down
8 changes: 7 additions & 1 deletion ecmascript/transforms/macros/src/fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Expander {
},
{
fn method(&mut self, node: &mut Type) {
node.visit_mut_with(self)
node.visit_mut_children_with(self)
}
}
),
Expand All @@ -118,6 +118,12 @@ impl Expander {
FnArg::Receiver(_) => unreachable!(),
FnArg::Typed(ty) => ty,
};
if m.sig.ident == "visit_mut_ident" || m.sig.ident == "fold_ident" {
return m;
}
if m.block.stmts.is_empty() {
return m;
}

let arg = match &*ty_arg.pat {
Pat::Ident(i) => &i.ident,
Expand Down
80 changes: 38 additions & 42 deletions ecmascript/transforms/src/compat/reserved_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,55 @@ use crate::perf::Check;
use swc_atoms::JsWord;
use swc_ecma_ast::*;
use swc_ecma_transforms_macros::fast_path;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Node, Visit};
use swc_ecma_visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Node, Visit, VisitMut, VisitMutWith,
};

pub fn reserved_words() -> impl 'static + Fold {
EsReservedWord {}
as_folder(EsReservedWord)
}

struct EsReservedWord {}
struct EsReservedWord;

#[fast_path(ShouldWork)]
impl Fold for EsReservedWord {
noop_fold_type!();
impl VisitMut for EsReservedWord {
noop_visit_mut_type!();

fn fold_export_specifier(&mut self, n: ExportSpecifier) -> ExportSpecifier {
n
}
fn visit_mut_export_specifier(&mut self, _n: &mut ExportSpecifier) {}

fn fold_ident(&mut self, i: Ident) -> Ident {
let sym = rename_ident(i.sym, true);
fn visit_mut_meta_prop_expr(&mut self, _n: &mut MetaPropExpr) {}

Ident { sym, ..i }
fn visit_mut_ident(&mut self, i: &mut Ident) {
rename_ident(&mut i.sym, true);
}

fn fold_import_named_specifier(&mut self, s: ImportNamedSpecifier) -> ImportNamedSpecifier {
if s.imported.is_some() {
ImportNamedSpecifier {
local: s.local.fold_with(self),
..s
}
} else {
ImportNamedSpecifier {
imported: s.imported.fold_with(self),
..s
}
}
fn visit_mut_import_named_specifier(&mut self, s: &mut ImportNamedSpecifier) {
s.local.visit_mut_with(self);
}

fn fold_member_expr(&mut self, e: MemberExpr) -> MemberExpr {
fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
match &e.obj {
ExprOrSuper::Super(_) => {}
ExprOrSuper::Expr(obj) => match &**obj {
Expr::Ident(Ident {
sym: js_word!("new"),
..
})
| Expr::Ident(Ident {
sym: js_word!("import"),
..
}) => return,
_ => {}
},
}
e.obj.visit_mut_with(self);

if e.computed {
MemberExpr {
obj: e.obj.fold_with(self),
prop: e.prop.fold_with(self),
..e
}
} else {
MemberExpr {
obj: e.obj.fold_with(self),
..e
}
e.prop.visit_mut_with(self);
}
}

fn fold_prop_name(&mut self, n: PropName) -> PropName {
n
}
fn visit_mut_prop_name(&mut self, _n: &mut PropName) {}
}

fn is_reserved(sym: &JsWord) -> bool {
Expand All @@ -70,12 +65,11 @@ fn is_reserved(sym: &JsWord) -> bool {
}
}

fn rename_ident(sym: JsWord, _strict: bool) -> JsWord {
fn rename_ident(sym: &mut JsWord, _strict: bool) {
// Es
if is_reserved(&sym) {
format!("_{}", sym).into()
} else {
sym
if is_reserved(&*sym) {
let s = format!("_{}", sym).into();
*sym = s;
}
}
#[derive(Default)]
Expand All @@ -84,6 +78,8 @@ struct ShouldWork {
}

impl Visit for ShouldWork {
noop_visit_type!();

fn visit_ident(&mut self, i: &Ident, _: &dyn Node) {
if is_reserved(&i.sym) {
self.found = true;
Expand Down
8 changes: 8 additions & 0 deletions tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,11 @@ fn issue_895() {
assert!(!s.contains("_url."));
assert!(!s.contains("_url,"));
}

#[test]
fn issue_1052() {
let f = file("tests/projects/issue-1052/input.ts").unwrap();
println!("{}", f);

assert!(!f.contains("_new"))
}
8 changes: 8 additions & 0 deletions tests/projects/issue-1052/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2018"
}
}
7 changes: 7 additions & 0 deletions tests/projects/issue-1052/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ArgumentValidationError extends Error {
constructor(public validationErrors: ValidationError[]) {
super("Argument Validation Error");

Object.setPrototypeOf(this, new.target.prototype);
}
}

0 comments on commit 3a26d3d

Please sign in to comment.