swc_ecma_codegen:
 - fix codegen of shebang (#177)
 - add test for #197.

swc_ecma_transforms:
 - fix object rest spread pass (#162)
This commit is contained in:
강동윤 2019-02-14 13:24:01 +09:00 committed by GitHub
parent b27829825e
commit 631eff9e8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 33 deletions

View File

@ -93,7 +93,8 @@ impl<'a> Emitter<'a> {
pub fn emit_module(&mut self, node: &Module) -> Result {
if let Some(ref shebang) = node.shebang {
punct!("#!");
self.wr.write_lit(DUMMY_SP, &*shebang)?;
self.wr.write_str_lit(DUMMY_SP, &*shebang)?;
self.wr.write_line()?;
}
for stmt in &node.body {
emit!(stmt);
@ -104,7 +105,8 @@ impl<'a> Emitter<'a> {
pub fn emit_script(&mut self, node: &Script) -> Result {
if let Some(ref shebang) = node.shebang {
punct!("#!");
self.wr.write_lit(DUMMY_SP, &*shebang)?;
self.wr.write_str_lit(DUMMY_SP, &*shebang)?;
self.wr.write_line()?;
}
for stmt in &node.body {
emit!(stmt);

View File

@ -97,4 +97,24 @@ mod tests {
assert_min(r#"const a = fn() + '\r\n';"#, r#"const a=fn()+'\r\n';"#);
}
#[test]
fn issue_177() {
assert_min(
"#!/usr/bin/env node
let x = 4;",
"#!/usr/bin/env node
let x=4;",
);
}
#[test]
fn issue_197() {
assert_pretty(
"// type Foo = 'Oops';
const Link = 'Boo';",
"// type Foo = 'Oops';
const Link = 'Boo';",
);
}
}

View File

@ -1063,7 +1063,7 @@ export default App"#;
}
#[test]
fn shebang() {
fn shebang_01() {
let src = "#!/usr/bin/env node";
test_parser(
src,
@ -1079,6 +1079,24 @@ export default App"#;
);
}
#[test]
fn shebang_02() {
let src = "#!/usr/bin/env node
let x = 4";
test_parser(
src,
Syntax::Es(EsConfig {
..Default::default()
}),
|p| {
p.parse_module().map_err(|mut e| {
e.emit();
()
})
},
);
}
#[test]
fn empty() {
test_parser(

View File

@ -1,6 +1,6 @@
use crate::{
pass::Pass,
util::{alias_ident_for, ExprFactory, StmtLike},
util::{alias_ident_for, var::VarCollector, ExprFactory, StmtLike},
};
use ast::*;
use std::{iter, mem};
@ -271,29 +271,6 @@ impl Fold<Expr> for RestFolder {
}
}
/// Removes rest pattern from object pattern.
///
/// Used **only** for handling `export var {b, ...c};`
struct ExporedtNameFinder {
names: Vec<Ident>,
}
impl Visit<Pat> for ExporedtNameFinder {
fn visit(&mut self, node: &Pat) {
match *node {
Pat::Ident(ref i) => self.names.push(i.clone()),
_ => node.visit_children(self),
}
}
}
impl Visit<ObjectPatProp> for ExporedtNameFinder {
fn visit(&mut self, node: &ObjectPatProp) {
match *node {
ObjectPatProp::Assign(AssignPatProp { ref key, .. }) => self.names.push(key.clone()),
_ => node.visit_children(self),
}
}
}
/// export var { b, ...c } = asdf2;
impl Fold<ModuleDecl> for RestFolder {
fn fold(&mut self, decl: ModuleDecl) -> ModuleDecl {
@ -307,14 +284,14 @@ impl Fold<ModuleDecl> for RestFolder {
match decl {
ModuleDecl::ExportDecl(Decl::Var(var_decl)) => {
let specifiers = {
let mut finder = ExporedtNameFinder { names: vec![] };
let mut found = vec![];
let mut finder = VarCollector { to: &mut found };
var_decl.visit_with(&mut finder);
finder
.names
found
.into_iter()
.map(|orig| NamedExportSpecifier {
span: orig.span,
orig,
.map(|(sym, ctxt)| NamedExportSpecifier {
span: DUMMY_SP,
orig: Ident::new(sym, DUMMY_SP.with_ctxt(ctxt)),
exported: None,
})
.map(ExportSpecifier::Named)

View File

@ -7,6 +7,28 @@ fn tr() -> impl Fold<Module> {
object_rest_spread()
}
test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
issue_162,
r#"
export const good = {
a(bad1) {
(...bad2) => { };
}
};
"#,
r#"
var good = {
a (bad1) {
(...bad2)=>{
};
}
};
export { good }
"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),