Improve comment api (#277)

swc_ecma_parser:
 - parser now take comments by reference
This commit is contained in:
강동윤 2019-02-26 13:56:58 +09:00 committed by GitHub
parent c8c346d42d
commit 1b28766c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
151 changed files with 313 additions and 145 deletions

View File

@ -116,7 +116,7 @@ fn emit_colors(b: &mut Bencher) {
cfg: swc_ecma_codegen::Config {
..Default::default()
},
comments: parser.take_comments(),
comments: None,
cm: cm.clone(),
wr: box swc_ecma_codegen::text_writer::JsWriter::new(
cm.clone(),

View File

@ -65,7 +65,7 @@ impl<'a, N: Node> Node for &'a N {
pub struct Emitter<'a> {
pub cfg: config::Config,
pub cm: Arc<SourceMap>,
pub comments: Option<Comments>,
pub comments: Option<&'a Comments>,
pub wr: Box<('a + WriteJs)>,
pub handlers: Box<('a + Handlers)>,
pub pos_of_leading_comments: FxHashSet<BytePos>,

View File

@ -27,7 +27,7 @@ impl Builder {
cfg: self.cfg,
cm: self.cm.clone(),
wr: Box::new(text_writer::JsWriter::new(self.cm.clone(), "\n", s, None)),
comments: Some(self.comments),
comments: Some(&self.comments),
handlers: Box::new(Noop),
pos_of_leading_comments: Default::default(),
};
@ -57,21 +57,24 @@ fn parse_then_emit(from: &str, cfg: Config) -> String {
from, src.start_pos, src.end_pos
);
let mut parser = Parser::new(
Session { handler: &handler },
Syntax::default(),
SourceFileInput::from(&*src),
Some(Default::default()),
);
let res = parser.parse_module().map_err(|mut e| {
e.emit();
()
})?;
let comments = Default::default();
let res = {
let mut parser = Parser::new(
Session { handler: &handler },
Syntax::default(),
SourceFileInput::from(&*src),
Some(&comments),
);
parser.parse_module().map_err(|mut e| {
e.emit();
()
})?
};
let out = Builder {
cfg,
cm: cm.clone(),
comments: parser.take_comments().unwrap(),
comments,
}
.text(from, |e| e.emit_module(&res).unwrap());
Ok(out)

View File

@ -1,5 +1,7 @@
a.b('c').d('e', function(f) {
a.b('c').d('e', /*@ngInject*/
function(f) {
return f;
}).g('h', function(i) {
}).g('h', /*@ngInject*/
function(i) {
return i;
});

View File

@ -1 +1,3 @@
/*a
b*/
1;

View File

@ -1,5 +1,6 @@
function a() {
if (false) {
var a = 1;
// because test is not referenced
var a = 1;
}
}

View File

@ -1,2 +1,3 @@
do continue;
while (true)
while (true) // should be empty statement

View File

@ -1 +1,3 @@
// ContinueStatement should be removed.
// And label is not used, then label also should be removed.
a: for(;;)continue a;

View File

@ -1,4 +1,5 @@
(function() {
arguments[1] = 2;
var a = 3;
var a = 3; // should not hoist to parameter
}());

View File

@ -1,3 +1,4 @@
// global, do not optimize
(function() {
a('b');
}());

View File

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

View File

@ -5,8 +5,10 @@ c: {
b();
if (a) break c;
for(var b = 3; b < 4; b++){
if (b > 5) break;
d.e(b);
if (b > 5) break; // this break refers to the for, not to the switch; thus it
// shouldn't ruin our optimization
d.e(b);
}
f();
case 6 + 7:

View File

@ -1,3 +1,4 @@
if (a) {
b();
b(); // Some comment
}

View File

@ -1,4 +1,5 @@
while(true){
break;
break; /* Multiline
Comment */
a;
}

View File

@ -1 +1,2 @@
typeof (1, a);
typeof (1, a); // Don't transform to 0,typeof ident

View File

@ -1,3 +1,6 @@
// TODO(Constellation):
// This transformation sometimes make script bigger size.
// So we should handle it in post processing pass.
(function() {
while(!a || !b()){
c();

View File

@ -1,3 +1,4 @@
function a() {
var { a , b } = c;
// If foo is null or undefined, this should be an exception
var { a , b } = c;
}

View File

@ -1,3 +1,4 @@
// prevent optimization because of this.constructor.arguments access
new function() {
var a = 1;
this.arguments;

View File

@ -1,3 +1,4 @@
// adapted from http://asmjs.org/spec/latest/
function a(b, c, d) {
'use asm';
var e = b.f.e;
@ -7,7 +8,8 @@ function a(b, c, d) {
k = k | 1;
l = l | 2;
var m = 0, n = 3, o = 4;
for(n = k << 5, o = l << 6; (n | 7) < (o | 8); n = (n + 9) | 10){
// asm.js forces byte addressing of the heap by requiring shifting by 3
for(n = k << 5, o = l << 6; (n | 7) < (o | 8); n = (n + 9) | 10){
m = m + +g(h[n >> 11]);
}
return +m;
@ -29,7 +31,8 @@ function q(b, c, d) {
k = k | 15;
l = l | 16;
var m = 0, n = 17, o = 18;
for(n = k << 19, o = l << 20; (n | 21) < (o | 22); n = (n + 23) | 24){
// asm.js forces byte addressing of the heap by requiring shifting by 3
for(n = k << 19, o = l << 20; (n | 21) < (o | 22); n = (n + 23) | 24){
m = m + +g(h[n >> 25]);
}
return +m;

View File

@ -1,3 +1,4 @@
for(var a in b)(function() {
for(var a in b)// do not optimize it
(function() {
c('d');
}());

View File

@ -1,5 +1,7 @@
// reported from issue #60
void function() {
var a;
var a; // this foo should be dropped
a = function() {
return 1;
};

View File

@ -1,4 +1,4 @@
b: while(1){
continue;
continue; /**/
a;
}

View File

@ -1,3 +1,4 @@
/**/
function a() {
function b() {
}

View File

@ -1 +1,2 @@
debugger;
/*Venus*/
debugger; // Mars

View File

@ -1,5 +1,6 @@
if (a) {
(function() {
// optimize it
(function() {
b('c');
}());
try {

View File

@ -1,3 +1,4 @@
// optimize this
(function() {
a('b');
}());

View File

@ -1,4 +1,6 @@
// To avoid JSC bug, we don't distinguish FunctionExpression name scope and it's function scope
(function a() {
var b = 1;
var b = 1; // Don't rename this variable to a name that is the same to function's name
c(b);
}());

View File

@ -1,4 +1,5 @@
(function() {
return;
return; /* Multiline
Comment */
a;
});

View File

@ -1,3 +1,4 @@
// Surpress reducing because of alternate
for(;;){
if (a) {
if (b) {

View File

@ -1,3 +1,4 @@
// 1.
if (a) {
{
{
@ -15,6 +16,7 @@ if (a) {
} else {
d();
}
// 2.
if (a) {
for(var e = 1; e < 2; ++e)if (b) c();
} else {

View File

@ -1,4 +1,5 @@
(function() {
throw 'a';
with (b);
with (b); // This should be removed.
}());

View File

@ -1 +1 @@
1;
1; /* the * answer */

View File

@ -1,5 +1,6 @@
(function() {
var a = 1;
var a = 1; // should not hoist this
arguments[2] = 3;
(function() {
eval('');

View File

@ -1,3 +1,4 @@
/**/
function a() {
function b() {
}

View File

@ -1 +1,2 @@
// line comment
1;

View File

@ -1,4 +1,5 @@
(function() {
var a = 1;
var a = 1; // should not hoist to parameter
eval('');
}());

View File

@ -1,3 +1,4 @@
/* header */
(function() {
var a = 1;
}).b(this);

View File

@ -1 +1,2 @@
a + (b(), c(), d());
a + (b(), c(), d()); // do not transform because of global getter

View File

@ -1,6 +1,7 @@
switch(a){
default:
(function() {
// do not optimize it
(function() {
b('c');
}());
}

View File

@ -1,3 +1,3 @@
if (a) {
b();
b(); /* Some comment */
}

View File

@ -1,5 +1,6 @@
(function() {
var a = 1;
// https://github.com/Constellation/esmangle/issues/65
var a = 1;
var b = 2;
var c = 3;
var d = [].e.f(arguments);

View File

@ -1,4 +1,4 @@
b: while(1){
continue;
continue; /**/
a;
}

View File

@ -1 +1,2 @@
// ContinueStatement should not be removed.
a: while(true)while(true)continue a;

View File

@ -1 +1 @@
1;
1; /**/

View File

@ -1 +1 @@
1;
1; // line comment

View File

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

View File

@ -1,4 +1,5 @@
while(true){
break;
break; // Comment
a;
}

View File

@ -1 +1,2 @@
for(;;)continue;
for(;;)continue; // should be empty statement

View File

@ -1,6 +1,7 @@
switch(a){
case 1:
(function() {
// optimize it
(function() {
b('c');
}());
b('d');

View File

@ -1 +1 @@
var a = 1;
var //fooa = 1;

View File

@ -1,4 +1,5 @@
while(true){
continue;
continue; // Comment
a;
}

View File

@ -1 +1,2 @@
// not reduce this because of ToNumber
+/test/;

View File

@ -1 +1,2 @@
// false
!/test/;

View File

@ -1,3 +1,4 @@
// Surpress reducing because of alternate
for(;;){
if (a) {
if (b) {

View File

@ -1,5 +1,6 @@
d: {
b: {
// this 'test2' to 'a'
b: {
if (a) break b;
if (a) break b;
if (a) break b;

View File

@ -2,7 +2,8 @@ while(a){
try {
} catch (b) {
}
(function() {
// do not optimize it
(function() {
c('d');
}());
}

View File

@ -1,3 +1,4 @@
// Do not remove first if consequent block
if (a) {
if (b) {
true;

View File

@ -1,3 +1,4 @@
// DO NOT DROP i
function a() {
var b;
with (c){

View File

@ -1,5 +1,6 @@
function a(b, c) {
function d() {
// circular reference
function d() {
return e();
}
function e() {

View File

@ -1,3 +1,8 @@
// Do not generate like this
// if(cond)with(cond2)if(cond3)debugger;else ok();
//
//
// See https://github.com/Constellation/escodegen/issues/49
if (a) {
with (b){
if (c) {

View File

@ -2,14 +2,16 @@ if (a) {
try {
} catch (b) {
}
(function() {
// do not optimize it
(function() {
c('d');
}());
} else {
try {
} catch (b) {
}
(function() {
// do not optimize it
(function() {
c('d');
}());
}

View File

@ -2,13 +2,13 @@ function a() {
b();
c = 1;
throw 'd';
if (c) {
// completely discarding the `if` would introduce some // bugs. UglifyJS v1 doesn't deal with this issue; in v2 // we copy any declarations to the upper scope. if (c) {
e();
var c;
function b() {
}
;
(function() {
// but nested declarations should not be kept. (function() {
var f;
function e() {
}

View File

@ -1 +1,2 @@
// global getter to o
a.b = (c(), 1);

View File

@ -1,4 +1,5 @@
var a;
// access to global should be assumed to have side effects
if (b) {
a = 1 + 2;
} else {

View File

@ -3,6 +3,7 @@ for(;;){
if (b) {
continue;
}
c();
c(); // This should not removed and translation should not occur.
}
}

View File

@ -1,3 +1,4 @@
function a() {
for(var b in c);
// Do not remove this i
for(var b in c);
}

View File

@ -1,4 +1,5 @@
b: while(1){
continue;
continue; /*
*/
a;
}

View File

@ -1,4 +1,5 @@
var a;
// compress these
a = true && b;
a = 1 && c.d('a');
a = 2 * 3 && 4 * b;
@ -16,6 +17,7 @@ a = 17 * 18 - 19 && 20 - b;
a = 21 == 22 && b / 23;
a = !'e' && 24 % b;
a = 25 && 26;
// don't compress these
a = b && true;
a = c.d('a') && 27;
a = 28 - b && 'e';

View File

@ -1,2 +1,3 @@
/**/
function a() {
}

View File

@ -1 +1,3 @@
// ContinueStatement should be removed.
// And label is not used, then label also should be removed.
a: while(true)continue a;

View File

@ -1 +1 @@
100000000000000000000, 1000000000000000000000;
// This test is ported from uglify-js 100000000000000000000, 1000000000000000000000;

View File

@ -1 +1,2 @@
// because `with` can observe i lookup
a = a += 1;

View File

@ -1,3 +1,4 @@
// mangle to the same name 'a'
c: {
a('b');
break c;

View File

@ -1,5 +1,6 @@
do {
(function() {
// do not optimize it
(function() {
a('b');
}());
}while (c)

View File

@ -1,5 +1,6 @@
var a = 'b' + 'c' + d() + 'e' + 'b' + f() + 'd' + 'f' + 'g' + h();
var i = 'b' + 1 + d() + 2 + 'j';
var k = 3 + d() + 4 + 'j';
// this CAN'T safely be shortened to 1 + x() + "5boo"
var l = 5 + d() + 6 + 7 + 'j';
var m = 8 + d() + 9 + 'n' + 10 + 'j';

View File

@ -1 +1,4 @@
/**
* @type {number}
*/
var a = 1;

View File

@ -1 +1,2 @@
1;
1; /*the*/
/*answer*/

View File

@ -1 +1,2 @@
1;
1; /*The*/
/*Answer*/

View File

@ -1 +1,3 @@
/*a
c*/
1;

View File

@ -1,4 +1,5 @@
{
throw a;
throw a; /* Multiline
Comment */
a;
}

View File

@ -1 +1,3 @@
/*a
b*/
1;

View File

@ -1,3 +1,4 @@
with (a)(function() {
with (a)// do not optimize it
(function() {
b('c');
}());

View File

@ -1,4 +1,5 @@
(function() {
return;
return; // Comment
a;
});

View File

@ -1,4 +1,5 @@
while(true){
continue;
continue; /* Multiline
Comment */
a;
}

View File

@ -1,3 +1,4 @@
({
/* comment 2 */
a: null
});

View File

@ -1,8 +1,10 @@
(function() {
b: {
// 'a'
b: {
if (a) break b;
(function() {
b: {
// 'a'
b: {
if (a) break b;
c('d');
}

View File

@ -1,10 +1,14 @@
// 1
a();
b();
for(; false;);
// 2
a();
b();
for(c = 1; false;);
// 3
c = (a in b);
for(; false;);
// 4
c = (a in b);
for(d = 2; false;);

View File

@ -1,3 +1,4 @@
// ContinueStatement should not be removed.
a: do do continue a;
while (true)
while (true)

View File

@ -1,16 +1,25 @@
(function() {
var a;
if (b) return;
if (b) return; /* I would
insert something
there, but I'm sort
of lazy so whatever.
*/
a = new c();
return a;
})()(function() {
var a;
if (b) return;
if (b) return; /* I would insert something there, */
/*
but I'm sort of lazy so
*/
/* whatever. */
a = new c();
return a;
})()(function() {
var a;
if (b) return;
if (b) return; // I would insert something there, but I'm sort of lazy so whatever.
a = new c();
return a;
})();

View File

@ -1 +1,2 @@
// do not transform to 0
-1;

View File

@ -1 +1 @@
1;
1; /* The * answer */

View File

@ -1,2 +1,3 @@
//
;
a;

View File

@ -1 +1,7 @@
var a = 1, b = 2;
/**
* @type {number}
*/
var a = 1, /**
* @type {number}
*/
b = 2;

View File

@ -1,3 +1,4 @@
for(var a = 1; a < 2; ++a)(function() {
for(var a = 1; a < 2; ++a)// do not optimize it
(function() {
b('c');
}());

View File

@ -1 +1,2 @@
// NaN => 0/0
'a' - 1;

View File

@ -1,4 +1,5 @@
(function() {
var a = 1;
var a = 1; // should not hoist to parameter
with (b)arguments = 2;
}());

View File

@ -1,4 +1,5 @@
{
throw a;
throw a; // Comment
a;
}

View File

@ -1 +1,6 @@
/* multiline
comment
should
be
ignored */
1;

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