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
- git submodule update --init --recursive
install:
- cargo test --no-run --color always --all
script:
- cargo test --color always --all
- RUST_BACKTRACE=full cargo test --color always --all
before_deploy:
- cargo doc --color always
after_success:
# Temporarily disabled because cargo tarpaulin does not set CARGO_MANIFEST_DIR.
#
@ -50,7 +56,6 @@ env:
global:
- CASHER_TIME_OUT=600
- RUST_MIN_STACK=4194304
- RUST_BACKTRACE=full
- CARGO_INCREMENTAL=0
- 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=

View File

@ -7,7 +7,7 @@ impl<'a> Emitter<'a> {
pos: BytePos,
prefix_space: bool,
) -> Result {
if !self.enable_comments {
if !self.cfg.enable_comments {
return Ok(());
}
// 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 {
if !self.enable_comments {
if !self.cfg.enable_comments {
return Ok(());
}
// debug_assert!(self.file.contains(pos));

View File

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

View File

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

View File

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

View File

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

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()) {
new a(1);
} 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() {
var a ;
var a;
eval('a');
function b() {
a = a += 1;

View File

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

View File

@ -1,3 +1,3 @@
(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) {
var c ;
var c;
d();
} else {
e();
@ -8,6 +8,6 @@ if (a && !(a + "1") && b) {
if (a || !!(a + "1") || b) {
d();
} else {
var f ;
var f;
e();
}

View File

@ -1,5 +1,5 @@
(function() {
var a ;
var a;
function b() {
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;

View File

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

View File

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

View File

@ -1,5 +1,5 @@
function a() {
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() {
var a ;
var a;
with (b){
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() {
var a ;
var a;
( 1, a)();
}());

View File

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

View File

@ -1,5 +1,5 @@
void function() {
var a ;
var a;
a = function() {
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() {
var a ;
var a;
b(typeof a ===" 'c');
}());

View File

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

View File

@ -1,3 +1,4 @@
(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() {
if (b) {
let c ;
let d ;
var e ;
var f ;
let c;
let d;
var e;
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();){
if (b()) c(); else break;
if (b()) c();
else break;
d();
e();
}

View File

@ -1,3 +1,3 @@
(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;

View File

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

View File

@ -1,4 +1,4 @@
(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);
}());

View File

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

View File

@ -1,3 +1,3 @@
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() {
var b ;
var b;
with (c){
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;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
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 = 1 && c.d("a");
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;
}

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());

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() {
if (a) return b; else return c;
if (a) return b;
else return c;
}());

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
(function() {
var a ;
var a;
with (b){
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() {
var a ;
var a;
if (b) return;
a = new c();
return a;
})()(function() {
var a ;
var a;
if (b) return;
a = new c();
return a;
})()(function() {
var a ;
var a;
if (b) return;
a = new c();
return a;

View File

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

View File

@ -1,7 +1,8 @@
for(; a();){
b();
c();
if (d()) e(); else break;
if (d()) e();
else break;
f();
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()) {
a = new b(1);
} else {

View File

@ -1,3 +1,4 @@
(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