Skip to content

Commit

Permalink
Attempt to handle possibly-empty-looking loops better
Browse files Browse the repository at this point in the history
... but it's failing and I'm not sure why
  • Loading branch information
jbt committed May 13, 2017
1 parent e1c2b2a commit 629c560
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/program/types/DoWhileStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Node from '../Node.js';
export default class DoWhileStatement extends Node {
minify ( code ) {
// special case
if ( this.body.body.length === 0 || this.body.body[0].type === 'EmptyStatement' ) {
if ( this.body.body.every( node => node.type === 'EmptyStatement' ) ) {
code.overwrite( this.start + 2, this.test.start, ';while(' );
}

Expand Down
2 changes: 1 addition & 1 deletion src/program/types/WhileStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class WhileStatement extends Node {
}

// special case — empty body
if ( this.body.body.length === 0 || this.body.body[0].type === 'EmptyStatement' ) {
if ( this.body.body.every( node => node.type === 'EmptyStatement' ) ) {
code.appendLeft( this.body.start, ';' );
code.remove( this.body.start, this.body.end );
}
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/shared/LoopStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class LoopStatement extends Node {

minify ( code ) {
// special case — empty body
if ( this.body.body.length === 0 || this.body.body[0].type === 'EmptyStatement' ) {
if ( this.body.body.every( node => node.type === 'EmptyStatement' ) ) {
code.appendLeft( this.body.start, ';' );
code.remove( this.body.start, this.body.end );
}
Expand Down
42 changes: 39 additions & 3 deletions test/samples/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ module.exports = [
output: `for(;;x());`
},

{
description: 'handles essentially-empty while loop body',
input: `while ( x() ) { ; ; }`,
output: `while(x());`
},

{
description: 'handles essentially-empty do-while loop body',
input: `do { ; ; } while ( x() );`,
output: `do;while(x())`
},

{
description: 'handles essentially-empty for loop body',
input: `for ( ; ; x() ) { ; ; }`,
output: `for(;;x());`
},

{
description: 'handles non-empty while loop body starting with empty statement',
input: `while ( x() ) { ; a(); }`,
output: `while(x()) a();`
},

{
description: 'handles non-empty do-while loop body starting with empty statement',
input: `do { ; a(); } while ( x() );`,
output: `do a();while(x())`
},

{
description: 'handles non-empty for loop body starting with empty statement',
input: `for ( ; ; x() ) { ; a(); }`,
output: `for(;;x()) a();`
},

{
description: 'handles duplicate let declarations in for-of loop head',
input: `
Expand All @@ -179,9 +215,9 @@ module.exports = [
}
})();`,
output: `!(()=>{let a=0;for(;;)f(a)})()`
},
{
},

{
description: 'preserves semi-colon for body-less while loop at end of body',
input: `
function f() {
Expand Down

0 comments on commit 629c560

Please sign in to comment.