Improve codegen (#57)

* travis: make it faster

* better codegen

* Move enable_comments field to config
This commit is contained in:
강동윤 2018-11-15 21:43:04 +09:00 committed by GitHub
parent 6234fd7a21
commit 7b56b0bc74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
180 changed files with 283 additions and 218 deletions

View File

@ -18,10 +18,16 @@ before_install:
- sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
- git submodule update --init --recursive - git submodule update --init --recursive
install:
- cargo test --no-run --color always --all
script: script:
- cargo test --color always --all - RUST_BACKTRACE=full cargo test --color always --all
before_deploy:
- cargo doc --color always - cargo doc --color always
after_success: after_success:
# Temporarily disabled because cargo tarpaulin does not set CARGO_MANIFEST_DIR. # Temporarily disabled because cargo tarpaulin does not set CARGO_MANIFEST_DIR.
# #
@ -50,7 +56,6 @@ env:
global: global:
- CASHER_TIME_OUT=600 - CASHER_TIME_OUT=600
- RUST_MIN_STACK=4194304 - RUST_MIN_STACK=4194304
- RUST_BACKTRACE=full
- CARGO_INCREMENTAL=0 - CARGO_INCREMENTAL=0
- RUSTFLAGS="--cfg procmacro2_semver_exempt" - RUSTFLAGS="--cfg procmacro2_semver_exempt"
- secure: Z4RSNlpg/12Qx2fIjS+7TToYxPJQgK70X7u9A5lJiCIa0JfzWCxr1ZEKXfAVLG9o4nQok+nWOZa+vxR1IgyUVnf6oSanjjWl1pSRbvccxMS799NvHmGzIAiqSKAlxiSJuxf7MQbs1XBuI3XahsWLfXGPo7vPT6sKe4AAf9gT6igJr61D5hpHkVIXK7P6rnrWQALQYplyaox0NlU9UlqSXXBjdJfp3138rl7FIeYRsMMow44unBNPvs+mhVP8PWpeFWeop0jxbNbTHwnJUbCm4ZWrvqnJ/m70IMlBMN1AskLmz4KeXOhPx+XR9VtdWBX4q8lJ7s9J0hMBxrEnxgiYVBPMlLoEX+wW3zwZ5F+DQs7uLpRHYRUpxpi/7ZuQjp+uT3mN9PMMSvbHwHLH2r/CC9olKYWySXIsGsGVyyMnZeUwvgzwxiYLoeCWe4zZY99zc7jvGKbSmk0RtPu6hApPwL5A6novXbXL2QsXzqqeWpgMLmZyb7KYhM5IGIAB1oPQIqI++Re9Z+/ea/DRSUJOsA96yRQ+vVbiuClrVgDhaAaJOGYCtR1XZ5N2zRb9+Spu/ECtfisLOb9Xs1584DyRbqG69nRdjuscjYOTFZUlOoOeFvuADY65Jt0kF6u7g8NIDkJ1ROb3heKQtY/bAQUrBNUJydOQnn5tBwn8Z618+Ac= - secure: Z4RSNlpg/12Qx2fIjS+7TToYxPJQgK70X7u9A5lJiCIa0JfzWCxr1ZEKXfAVLG9o4nQok+nWOZa+vxR1IgyUVnf6oSanjjWl1pSRbvccxMS799NvHmGzIAiqSKAlxiSJuxf7MQbs1XBuI3XahsWLfXGPo7vPT6sKe4AAf9gT6igJr61D5hpHkVIXK7P6rnrWQALQYplyaox0NlU9UlqSXXBjdJfp3138rl7FIeYRsMMow44unBNPvs+mhVP8PWpeFWeop0jxbNbTHwnJUbCm4ZWrvqnJ/m70IMlBMN1AskLmz4KeXOhPx+XR9VtdWBX4q8lJ7s9J0hMBxrEnxgiYVBPMlLoEX+wW3zwZ5F+DQs7uLpRHYRUpxpi/7ZuQjp+uT3mN9PMMSvbHwHLH2r/CC9olKYWySXIsGsGVyyMnZeUwvgzwxiYLoeCWe4zZY99zc7jvGKbSmk0RtPu6hApPwL5A6novXbXL2QsXzqqeWpgMLmZyb7KYhM5IGIAB1oPQIqI++Re9Z+/ea/DRSUJOsA96yRQ+vVbiuClrVgDhaAaJOGYCtR1XZ5N2zRb9+Spu/ECtfisLOb9Xs1584DyRbqG69nRdjuscjYOTFZUlOoOeFvuADY65Jt0kF6u7g8NIDkJ1ROb3heKQtY/bAQUrBNUJydOQnn5tBwn8Z618+Ac=

View File

@ -7,7 +7,7 @@ impl<'a> Emitter<'a> {
pos: BytePos, pos: BytePos,
prefix_space: bool, prefix_space: bool,
) -> Result { ) -> Result {
if !self.enable_comments { if !self.cfg.enable_comments {
return Ok(()); return Ok(());
} }
// debug_assert!(self.file.contains(pos)); // debug_assert!(self.file.contains(pos));
@ -29,7 +29,7 @@ impl<'a> Emitter<'a> {
} }
pub(super) fn emit_leading_comments_of_pos(&mut self, pos: BytePos) -> Result { pub(super) fn emit_leading_comments_of_pos(&mut self, pos: BytePos) -> Result {
if !self.enable_comments { if !self.cfg.enable_comments {
return Ok(()); return Ok(());
} }
// debug_assert!(self.file.contains(pos)); // debug_assert!(self.file.contains(pos));

View File

@ -1,5 +1,6 @@
#[derive(Debug, Default, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub struct Config { pub struct Config {
pub enable_comments: bool,
pub omit_trailing_semi: bool, pub omit_trailing_semi: bool,
pub sourcemap: Option<SourceMapConfig>, pub sourcemap: Option<SourceMapConfig>,
} }

View File

@ -60,9 +60,8 @@ impl<'a> Emitter<'a> {
pub fn emit_var_declator(&mut self, node: &VarDeclarator) -> Result { pub fn emit_var_declator(&mut self, node: &VarDeclarator) -> Result {
emit!(node.name); emit!(node.name);
formatting_space!();
if let Some(ref init) = node.init { if let Some(ref init) = node.init {
formatting_space!();
punct!("="); punct!("=");
formatting_space!(); formatting_space!();
emit!(init); emit!(init);

View File

@ -13,6 +13,8 @@ extern crate swc_atoms;
#[macro_use] #[macro_use]
extern crate swc_common; extern crate swc_common;
extern crate swc_ecma_ast; extern crate swc_ecma_ast;
pub use self::config::{Config, SourceMapConfig};
use self::{ use self::{
list::ListFormat, list::ListFormat,
text_writer::WriteJs, text_writer::WriteJs,
@ -27,7 +29,7 @@ use swc_ecma_ast::*;
#[macro_use] #[macro_use]
pub mod macros; pub mod macros;
mod comments; mod comments;
pub mod config; mod config;
mod decl; mod decl;
pub mod list; pub mod list;
#[cfg(test)] #[cfg(test)]
@ -59,7 +61,6 @@ impl<'a, N: Node> Node for &'a N {
pub struct Emitter<'a> { pub struct Emitter<'a> {
pub cfg: config::Config, pub cfg: config::Config,
pub cm: Rc<SourceMap>, pub cm: Rc<SourceMap>,
pub enable_comments: bool,
pub wr: Box<('a + WriteJs)>, pub wr: Box<('a + WriteJs)>,
pub handlers: Box<('a + Handlers)>, pub handlers: Box<('a + Handlers)>,
pub pos_of_leading_comments: HashSet<BytePos>, pub pos_of_leading_comments: HashSet<BytePos>,
@ -588,7 +589,11 @@ impl<'a> Emitter<'a> {
pub fn emit_block_stmt_or_expr(&mut self, node: &BlockStmtOrExpr) -> Result { pub fn emit_block_stmt_or_expr(&mut self, node: &BlockStmtOrExpr) -> Result {
match *node { match *node {
BlockStmtOrExpr::BlockStmt(ref block_stmt) => emit!(block_stmt), BlockStmtOrExpr::BlockStmt(ref block_stmt) => emit!(block_stmt),
BlockStmtOrExpr::Expr(ref expr) => emit!(expr), BlockStmtOrExpr::Expr(ref expr) => {
self.wr.increase_indent()?;
emit!(expr);
self.wr.decrease_indent()?;
}
} }
} }
@ -1159,7 +1164,10 @@ impl<'a> Emitter<'a> {
emit!(e); emit!(e);
semi!(); semi!();
} }
Stmt::Block(ref e) => emit!(e), Stmt::Block(ref e) => {
emit!(e);
return Ok(());
}
Stmt::Empty(ref e) => emit!(e), Stmt::Empty(ref e) => emit!(e),
Stmt::Debugger(ref e) => emit!(e), Stmt::Debugger(ref e) => emit!(e),
Stmt::With(ref e) => emit!(e), Stmt::With(ref e) => emit!(e),
@ -1260,10 +1268,16 @@ impl<'a> Emitter<'a> {
punct!(")"); punct!(")");
space!(); space!();
let is_block_stmt = match *node.cons {
Stmt::Block(_) => true,
_ => false,
};
emit!(node.cons); emit!(node.cons);
if let Some(ref alt) = node.alt { if let Some(ref alt) = node.alt {
space!(); if is_block_stmt {
space!();
}
keyword!("else"); keyword!("else");
space!(); space!();
emit!(alt); emit!(alt);
@ -1449,7 +1463,6 @@ fn get_text_of_node<T: Spanned>(
node: &T, node: &T,
_include_travia: bool, _include_travia: bool,
) -> Option<String> { ) -> Option<String> {
return None;
let span = node.span(); let span = node.span();
if span.is_dummy() || span.ctxt() != SyntaxContext::empty() { if span.is_dummy() || span.ctxt() != SyntaxContext::empty() {
// This node is transformed so we shoukld not use original source code. // This node is transformed so we shoukld not use original source code.

View File

@ -41,7 +41,6 @@ impl Builder {
cm: self.cm.clone(), cm: self.cm.clone(),
wr: box text_writer::JsWriter::new(self.cm.clone(), "\n", s, &mut src_map_builder), wr: box text_writer::JsWriter::new(self.cm.clone(), "\n", s, &mut src_map_builder),
handlers: box Noop, handlers: box Noop,
enable_comments: true,
pos_of_leading_comments: Default::default(), pos_of_leading_comments: Default::default(),
}; };
@ -109,13 +108,13 @@ fn test_from_to(from: &str, to: &str) {
#[test] #[test]
fn empty_stmt() { fn empty_stmt() {
test_from_to(";", ";"); test_from_to(";", ";\n");
} }
#[test] #[test]
#[ignore] #[ignore]
fn simple_if_else_stmt() { fn simple_if_else_stmt() {
test_from_to("if(true);else;", "if (true) ; else ;"); test_from_to("if(true);else;", "if (true) ; else ;\n");
} }
#[test] #[test]

View File

@ -1 +1 @@
for(var { a , b } in c); for(var { a , b } in c);

View File

@ -1 +1 @@
for(const a in b)c(a); for(const a in b)c(a);

View File

@ -1,4 +1,4 @@
var a ; var a;
if (b()) { if (b()) {
new a(1); new a(1);
} else { } else {

View File

@ -1 +1 @@
var _፩፪፫፬፭፮፯፰፱ ; var _፩፪፫፬፭፮፯፰፱;

View File

@ -1 +1,2 @@
do continue;while (true) do continue;
while (true)

View File

@ -1 +1 @@
var A\u{42}C ; var A\u{42}C;

View File

@ -1,5 +1,5 @@
(function() { (function() {
var a ; var a;
eval('a'); eval('a');
function b() { function b() {
a = a += 1; a = a += 1;

View File

@ -1 +1 @@
var 𞸊𞸋 ; var 𞸊𞸋;

View File

@ -1,3 +1,3 @@
(function() { (function() {
for(var a ; a < 1; ++a); for(var a; a < 1; ++a);
}()); }());

View File

@ -1,6 +1,6 @@
var a , b ; var a, b;
if (a && !(a + "1") && b) { if (a && !(a + "1") && b) {
var c ; var c;
d(); d();
} else { } else {
e(); e();
@ -8,6 +8,6 @@ if (a && !(a + "1") && b) {
if (a || !!(a + "1") || b) { if (a || !!(a + "1") || b) {
d(); d();
} else { } else {
var f ; var f;
e(); e();
} }

View File

@ -1,5 +1,5 @@
(function() { (function() {
var a ; var a;
function b() { function b() {
a = a += 1; a = a += 1;
} }

View File

@ -1,2 +1,2 @@
var a , b , c , d ; var a, b, c, d;
a = ( b(), c(), d()) ? 1 : 2; a = ( b(), c(), d()) ? 1 : 2;

View File

@ -1,3 +1,3 @@
for(var a = 1;;){ for(var a = 1;;){
let a ; let a;
} }

View File

@ -1,2 +1,2 @@
var a ; var a;
(a); (a);

View File

@ -1,5 +1,5 @@
function a() { function a() {
while(true){ while(true){
} }
var b ; var b;
} }

View File

@ -1 +1 @@
for(var [a, , b] in c); for(var [a, , b] in c);

View File

@ -1 +1 @@
var ; var ;

View File

@ -1,5 +1,5 @@
(function() { (function() {
var a ; var a;
with (b){ with (b){
a.c = ( d(), e()); a.c = ( d(), e());
} }

View File

@ -1 +1,2 @@
export { };1; export { };
1;

View File

@ -1 +1 @@
for(var { a , b } of c); for(var { a , b } of c);

View File

@ -1,4 +1,4 @@
(function() { (function() {
var a ; var a;
( 1, a)(); ( 1, a)();
}()); }());

View File

@ -1,3 +1,3 @@
for(var a in b)(function() { for(var a in b)(function() {
c('d'); c('d');
}()); }());

View File

@ -1,5 +1,5 @@
void function() { void function() {
var a ; var a;
a = function() { a = function() {
return 1; return 1;
}; };

View File

@ -1 +1 @@
for(let a in a); for(let a in a);

View File

@ -1 +1 @@
var a ; var a;

View File

@ -1 +1,2 @@
if (true) a(); else ; if (true) a();
else ;

View File

@ -1 +1 @@
let a ; let a;

View File

@ -1 +1 @@
for(let a in [1, 2])3; for(let a in [1, 2])3;

View File

@ -1,4 +1,4 @@
(function() { (function() {
var a ; var a;
b(typeof a ===" 'c'); b(typeof a ===" 'c');
}()); }());

View File

@ -1,5 +1,5 @@
function a() { function a() {
var b ; var b;
if (b = 'b') { if (b = 'b') {
return b; return b;
} else { } else {

View File

@ -1,3 +1,4 @@
(function() { (function() {
if (a) return; else return; if (a) return;
else return;
}()); }());

View File

@ -1 +1 @@
var yield ; var yield;

View File

@ -1,8 +1,8 @@
function a() { function a() {
if (b) { if (b) {
let c ; let c;
let d ; let d;
var e ; var e;
var f ; var f;
} }
} }

View File

@ -1 +1 @@
let a , b ; let a, b;

View File

@ -1 +1 @@
for(const a of b)c(a); for(const a of b)c(a);

View File

@ -1,3 +1,3 @@
{ {
let a ; let a;
} }

View File

@ -1 +1,2 @@
for(;;)if (a()) b(); else break; for(;;)if (a()) b();
else break;

View File

@ -1 +1,2 @@
if (true) a(); else ; if (true) a();
else ;

View File

@ -1 +1 @@
export var a ;; export var a;;

View File

@ -1 +1,2 @@
do a();while (true) do a();
while (true)

View File

@ -1,5 +1,6 @@
for(; a();){ for(; a();){
if (b()) c(); else break; if (b()) c();
else break;
d(); d();
e(); e();
} }

View File

@ -1,3 +1,3 @@
(a)=>({ (a)=>({
b: 1 b: 1
}); });

View File

@ -1 +1 @@
let ; let ;

View File

@ -1,4 +1,4 @@
var a ; var a;
a = b ? true : false; a = b ? true : false;
a = !b ? true : false; a = !b ? true : false;
a = b() ? true : false; a = b() ? true : false;

View File

@ -1 +1 @@
for(const { a , b } of c); for(const { a , b } of c);

View File

@ -1,4 +1,4 @@
(function() { (function() {
var a , b , c = 1, d , e , f = 2; var a, b, c = 1, d, e, f = 2;
( a, b, c) + ( d, e, f); ( a, b, c) + ( d, e, f);
}()); }());

View File

@ -1 +1 @@
var \u2118 ; var \u2118;

View File

@ -1,3 +1,3 @@
switch(a){ switch(a){
case 1: let b ; case 1: let b;
} }

View File

@ -1 +1 @@
for(let a in b)c(a); for(let a in b)c(a);

View File

@ -1 +1 @@
for(var a of b); for(var a of b);

View File

@ -1 +1 @@
var \u{41}BC ; var \u{41}BC;

View File

@ -1 +1,2 @@
if (a) b; else c; if (a) b;
else c;

View File

@ -1 +1 @@
for(var a ; b; c); for(var a; b; c);

View File

@ -1,5 +1,5 @@
function a() { function a() {
var b ; var b;
with (c){ with (c){
b; b;
} }

View File

@ -1 +1 @@
export let a ;; export let a;;

View File

@ -1,3 +1,3 @@
for(var a in b){ for(var a in b){
c; c;
} }

View File

@ -1 +1 @@
var _\u{1EE03} ; var _\u{1EE03};

View File

@ -4,12 +4,12 @@ function a() {
throw "d"; throw "d";
if (c) { if (c) {
e(); e();
var c ; var c;
function b() { function b() {
} }
; ;
(function() { (function() {
var f ; var f;
function e() { function e() {
} }
; ;

View File

@ -1 +1 @@
var \u{1EE00} ; var \u{1EE00};

View File

@ -1,4 +1,4 @@
var a ; var a;
if (b) { if (b) {
a = 1 + 2; a = 1 + 2;
} else { } else {

View File

@ -1,4 +1,4 @@
var a , b ; var a, b;
a.c = ( a = { a.c = ( a = {
}, 1); }, 1);
b = ( b = { b = ( b = {

View File

@ -1,3 +1,3 @@
function a() { function a() {
for(var b in c); for(var b in c);
} }

View File

@ -1,4 +1,4 @@
var a ; var a;
if (b()) { if (b()) {
a(); a();
} else { } else {

View File

@ -1,3 +1,4 @@
while(true){ while(true){
if (a) break; else b; if (a) break;
else b;
} }

View File

@ -1 +1 @@
var let ; var let;

View File

@ -1,4 +1,4 @@
var a ; var a;
a = true && b; a = true && b;
a = 1 && c.d("a"); a = 1 && c.d("a");
a = 2 * 3 && 4 * b; a = 2 * 3 && 4 * b;

View File

@ -1 +1 @@
export var a ;; export var a;;

View File

@ -1,4 +1,5 @@
{ {
do ;while (false) do ;
while (false)
false; false;
} }

View File

@ -1 +1 @@
var a , b ; var a, b;

View File

@ -1 +1 @@
for(let a of [1, 2])3; for(let a of [1, 2])3;

View File

@ -1 +1,2 @@
do ;while (true) do ;
while (true)

View File

@ -1 +1 @@
var \u{41}\u{42}\u{43} ; var \u{41}\u{42}\u{43};

View File

@ -1 +1,2 @@
do a();while (true) do a();
while (true)

View File

@ -1 +1 @@
for(var a of b)c(a); for(var a of b)c(a);

View File

@ -1 +1,2 @@
do continue;while (1) do continue;
while (1)

View File

@ -1 +1,2 @@
do a();while (true) do a();
while (true)

View File

@ -1,2 +1,2 @@
var a , b , c , d ; var a, b, c, d;
a = !( b(), c(), d()); a = !( b(), c(), d());

View File

@ -1 +1 @@
var a , [a] = 1; var a, [a] = 1;

View File

@ -1,2 +1,2 @@
for(var [a, , b] in c){ for(var [a, , b] in c){
} }

View File

@ -1 +1 @@
for(let a of b); for(let a of b);

View File

@ -1 +1 @@
for(const a of b); for(const a of b);

View File

@ -1,3 +1,4 @@
(function() { (function() {
if (a) return b; else return c; if (a) return b;
else return c;
}()); }());

View File

@ -1,2 +1,3 @@
export function a() { export function a() {
};;1; };;
1;

View File

@ -1 +1 @@
var a ; var a;

View File

@ -1,3 +1,4 @@
(function() { (function() {
if (a) throw b; else throw c; if (a) throw b;
else throw c;
}()); }());

View File

@ -1,5 +1,5 @@
(function() { (function() {
var a ; var a;
with (b){ with (b){
a; a;
} }

View File

@ -1 +1,3 @@
a: do do continue a;while (true)while (true) a: do do continue a;
while (true)
while (true)

View File

@ -1,15 +1,15 @@
(function() { (function() {
var a ; var a;
if (b) return; if (b) return;
a = new c(); a = new c();
return a; return a;
})()(function() { })()(function() {
var a ; var a;
if (b) return; if (b) return;
a = new c(); a = new c();
return a; return a;
})()(function() { })()(function() {
var a ; var a;
if (b) return; if (b) return;
a = new c(); a = new c();
return a; return a;

View File

@ -8,5 +8,6 @@ if (a) {
with (a)if (b) throw c; with (a)if (b) throw c;
} else d; } else d;
if (a) { if (a) {
do if (b) throw c;while (a) do if (b) throw c;
while (a)
} else d; } else d;

View File

@ -1,7 +1,8 @@
for(; a();){ for(; a();){
b(); b();
c(); c();
if (d()) e(); else break; if (d()) e();
else break;
f(); f();
g(); g();
} }

View File

@ -1,3 +1,3 @@
{ {
let a ; let a;
} }

View File

@ -1,4 +1,4 @@
var a , b ; var a, b;
if (c()) { if (c()) {
a = new b(1); a = new b(1);
} else { } else {

View File

@ -1,3 +1,4 @@
(function() { (function() {
if (a) return 1; else return; if (a) return 1;
else return;
}()); }());

View File

@ -1 +1 @@
for(let [a, , b] of c); for(let [a, , b] of c);

Some files were not shown because too many files have changed in this diff Show More