Skip to content

Commit

Permalink
determine block emptiness at minify time, in case var is activated ou…
Browse files Browse the repository at this point in the history
…tside the block (ugh)
  • Loading branch information
Rich-Harris committed May 14, 2017
1 parent 37cb331 commit 67a7d47
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/program/BlockStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class BlockStatement extends Node {
canSequentialise () {
for ( let i = 0; i < this.body.length; i += 1 ) {
const node = this.body[i];
if ( !node.skip && !node.canSequentialise() ) return false;
if ( !node.skip && !node.canSequentialise() ) return false; // TODO what if it's a block with a late-activated declaration...
}

return true;
Expand Down Expand Up @@ -154,14 +154,15 @@ export default class BlockStatement extends Node {
maybeReturnNode.skip = true;
}
}
}

isEmpty () {
for ( let i = 0; i < this.body.length; i += 1 ) {
const node = this.body[i];
if ( !node.skip ) {
this.skip = false;
break;
}
if ( !node.skip ) return false;
}

return true;
}

minify ( code ) {
Expand Down
4 changes: 4 additions & 0 deletions src/program/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export default class Node {
}
}

isEmpty () {
return this.skip;
}

findVarDeclarations ( varsToHoist ) {
for ( var key of this.keys ) {
const value = this[ key ];
Expand Down
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.skip ) {
if ( this.body.isEmpty() ) {
code.overwrite( this.start + 2, this.test.start, ';while(' );
}

Expand Down
4 changes: 2 additions & 2 deletions src/program/types/IfStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class IfStatement extends Node {
this.consequent.getPrecedence() < targetPrecedence;

// special case – empty consequent
if ( this.consequent.skip ) {
if ( this.consequent.isEmpty() ) {
const canRemoveTest = this.test.type === 'Identifier' || this.test.getValue() !== UNKNOWN; // TODO can this ever happen?

if ( this.alternate ) {
Expand Down Expand Up @@ -185,7 +185,7 @@ export default class IfStatement extends Node {
}

// special case - empty alternate
if ( this.alternate && this.alternate.skip ) {
if ( this.alternate && this.alternate.isEmpty() ) {
// don't minify alternate
this.consequent.minify( code );
code.remove( this.consequent.end, this.end );
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.skip ) {
if ( this.body.isEmpty() ) {
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 @@ -23,7 +23,7 @@ export default class LoopStatement extends Node {
if ( this.scope ) this.scope.mangle( code );

// special case — empty body
if ( this.body.skip ) {
if ( this.body.isEmpty() ) {
code.appendLeft( this.body.start, ';' );
code.remove( this.body.start, this.body.end );
}
Expand Down

0 comments on commit 67a7d47

Please sign in to comment.