Skip to content

Commit

Permalink
Fix bugs (swc-project#687)
Browse files Browse the repository at this point in the history
- stript type-only export default
- allow `continue` in a for loop
  • Loading branch information
kdy1 authored Feb 22, 2020
1 parent cb43a69 commit 1cf86b0
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ecmascript/transforms/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swc_ecma_transforms"
version = "0.7.0"
version = "0.7.1"
authors = ["강동윤 <[email protected]>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"
Expand Down
100 changes: 98 additions & 2 deletions ecmascript/transforms/src/compat/es2015/block_scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ impl BlockScoping {
})
.collect(),
decorators: Default::default(),
body: Some(match body {
body: Some(match body.fold_with(&mut FlowHelper) {
Stmt::Block(bs) => bs,
_ => BlockStmt {
body => BlockStmt {
span: DUMMY_SP,
stmts: vec![body],
},
Expand Down Expand Up @@ -195,6 +195,7 @@ impl Fold<WhileStmt> for BlockScoping {
impl Fold<ForStmt> for BlockScoping {
fn fold(&mut self, node: ForStmt) -> ForStmt {
let init = node.init.fold_with(self);

let mut vars = find_vars(&init);
let args = vars.clone();

Expand Down Expand Up @@ -502,6 +503,22 @@ impl Visit<Ident> for InfectionFinder<'_> {
}
}

#[derive(Debug)]
struct FlowHelper;

noop_fold_type!(FlowHelper);

impl Fold<Stmt> for FlowHelper {
fn fold(&mut self, node: Stmt) -> Stmt {
let span = node.span();

match node {
Stmt::Continue(..) => return Stmt::Return(ReturnStmt { span, arg: None }),
_ => node.fold_children(self),
}
}
}

#[derive(Debug)]
struct FunctionFinder {
found: bool,
Expand Down Expand Up @@ -674,4 +691,83 @@ foo();",
foo();"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| block_scoping(),
issue_686,
"module.exports = function(values) {
var vars = [];
var elem = null, name, val;
for (var i = 0; i < this.elements.length; i++) {
elem = this.elements[i];
name = elem.name;
if (!name) continue;
val = values[name];
if (val == null) val = '';
switch (elem.type) {
case 'submit':
break;
case 'radio':
case 'checkbox':
elem.checked = val.some(function(str) {
return str.toString() == elem.value;
});
break;
case 'select-multiple':
elem.fill(val);
break;
case 'textarea':
elem.innerText = val;
break;
case 'hidden':
break;
default:
if (elem.fill) {
elem.fill(val);
} else {
elem.value = val;
}
break;
}
}
return vars;
};",
"module.exports = function(values) {
var _loop = function(i) {
elem = this.elements[i];
name = elem.name;
if (!name) return;
val = values[name];
if (val == null) val = '';
switch(elem.type){
case 'submit': break;
case 'radio':
case 'checkbox':
elem.checked = val.some(function(str) {
return str.toString() == elem.value;
});
break;
case 'select-multiple':
elem.fill(val);
break;
case 'textarea':
elem.innerText = val;
break;
case 'hidden': break;
default:
if (elem.fill) {
elem.fill(val);
} else {
elem.value = val;
}
break;
}
};
var vars = [];
var elem = null, name, val;
for(var i = 0; i < this.elements.length; i++)_loop(i);
return vars;
};"
);
}
25 changes: 23 additions & 2 deletions ecmascript/transforms/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fxhash::FxHashMap;
use swc_atoms::js_word;
use swc_common::{util::move_map::MoveMap, Fold, FoldWith, Spanned, Visit, VisitWith, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::Id;
use swc_ecma_utils::{ident::IdentLike, Id};

/// Strips type annotations out.
pub fn strip() -> impl Pass {
Expand Down Expand Up @@ -199,10 +199,12 @@ impl Fold<Vec<Pat>> for Strip {

impl Fold<Vec<ModuleItem>> for Strip {
fn fold(&mut self, items: Vec<ModuleItem>) -> Vec<ModuleItem> {
let old = self.phase;

// First pass
self.phase = Phase::Analysis;
let items = items.fold_children(self);

let old = self.phase;
self.phase = Phase::DropImports;

// Second pass
Expand Down Expand Up @@ -270,6 +272,25 @@ impl Fold<Vec<ModuleItem>> for Strip {
self.handle_enum(e, &mut stmts)
}

ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr: box Expr::Ident(ref i),
..
})) => {
// type MyType = string;
// export default MyType;

let preserve = if let Some(decl_info) = self.scope.decls.get(&i.to_id()) {
decl_info.has_concrete
} else {
true
};

if preserve {
stmts.push(item)
}
}

// Strip out ts-only extensions
ModuleItem::Stmt(Stmt::Decl(Decl::Fn(FnDecl {
function: Function { body: None, .. },
..
Expand Down
30 changes: 30 additions & 0 deletions ecmascript/transforms/tests/typescript_strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,33 @@ test!(
to!(import_type, "import type foo from 'foo'", "");

to!(export_type, "export type { foo }", "");

to!(
issue_685,
"
type MyType = string;
export default MyType;",
""
);

to!(
issue_685_2,
"
class MyType {}
type MyType = string;
export default MyType;",
"
class MyType {}
export default MyType;"
);

to!(
issue_685_3,
"
var MyType = function(){};
type MyType = string;
export default MyType;",
"
var MyType = function(){};
export default MyType;"
);

0 comments on commit 1cf86b0

Please sign in to comment.