|
@ -1,6 +1,6 @@ |
|
|
import Statement from './shared/Statement.js'; |
|
|
import Statement from './shared/Statement.js'; |
|
|
import extractNames from '../utils/extractNames.js'; |
|
|
import extractNames from '../utils/extractNames.js'; |
|
|
import { UNKNOWN } from '../values.js'; |
|
|
import { unknown } from '../values.js'; |
|
|
|
|
|
|
|
|
// Statement types which may contain if-statements as direct children.
|
|
|
// Statement types which may contain if-statements as direct children.
|
|
|
const statementsWithIfStatements = new Set([ |
|
|
const statementsWithIfStatements = new Set([ |
|
@ -39,82 +39,82 @@ function handleVarDeclarations ( node, scope ) { |
|
|
|
|
|
|
|
|
// TODO DRY this out
|
|
|
// TODO DRY this out
|
|
|
export default class IfStatement extends Statement { |
|
|
export default class IfStatement extends Statement { |
|
|
initialise ( scope ) { |
|
|
// initialise ( scope ) {
|
|
|
this.scope = scope; |
|
|
// this.scope = scope;
|
|
|
this.testValue = this.test.getValue(); |
|
|
// this.testValue = this.test.getValue();
|
|
|
|
|
|
//
|
|
|
if ( this.module.bundle.treeshake ) { |
|
|
// if ( this.module.bundle.treeshake ) {
|
|
|
if ( this.testValue === UNKNOWN ) { |
|
|
// if ( this.testValue === unknown ) {
|
|
|
super.initialise( scope ); |
|
|
// super.initialise( scope );
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else if ( this.testValue ) { |
|
|
// else if ( this.testValue ) {
|
|
|
this.consequent.initialise( scope ); |
|
|
// this.consequent.initialise( scope );
|
|
|
|
|
|
//
|
|
|
if ( this.alternate ) this.hoistedVars = handleVarDeclarations( this.alternate, scope ); |
|
|
// if ( this.alternate ) this.hoistedVars = handleVarDeclarations( this.alternate, scope );
|
|
|
this.alternate = null; |
|
|
// this.alternate = null;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else { |
|
|
// else {
|
|
|
if ( this.alternate ) this.alternate.initialise( scope ); |
|
|
// if ( this.alternate ) this.alternate.initialise( scope );
|
|
|
|
|
|
//
|
|
|
this.hoistedVars = handleVarDeclarations( this.consequent, scope ); |
|
|
// this.hoistedVars = handleVarDeclarations( this.consequent, scope );
|
|
|
this.consequent = null; |
|
|
// this.consequent = null;
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else { |
|
|
// else {
|
|
|
super.initialise( scope ); |
|
|
// super.initialise( scope );
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
render ( code, es ) { |
|
|
// render ( code, es ) {
|
|
|
if ( this.module.bundle.treeshake ) { |
|
|
// if ( this.module.bundle.treeshake ) {
|
|
|
if ( this.testValue === UNKNOWN ) { |
|
|
// if ( this.testValue === unknown ) {
|
|
|
super.render( code, es ); |
|
|
// super.render( code, es );
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else { |
|
|
// else {
|
|
|
code.overwrite( this.test.start, this.test.end, JSON.stringify( this.testValue ) ); |
|
|
// code.overwrite( this.test.start, this.test.end, JSON.stringify( this.testValue ) );
|
|
|
|
|
|
//
|
|
|
// TODO if no block-scoped declarations, remove enclosing
|
|
|
// // TODO if no block-scoped declarations, remove enclosing
|
|
|
// curlies and dedent block (if there is a block)
|
|
|
// // curlies and dedent block (if there is a block)
|
|
|
|
|
|
//
|
|
|
if ( this.hoistedVars ) { |
|
|
// if ( this.hoistedVars ) {
|
|
|
const names = this.hoistedVars |
|
|
// const names = this.hoistedVars
|
|
|
.map( name => { |
|
|
// .map( name => {
|
|
|
const declaration = this.scope.findDeclaration( name ); |
|
|
// const declaration = this.scope.findDeclaration( name );
|
|
|
return declaration.activated ? declaration.getName() : null; |
|
|
// return declaration.activated ? declaration.getName() : null;
|
|
|
}) |
|
|
// })
|
|
|
.filter( Boolean ); |
|
|
// .filter( Boolean );
|
|
|
|
|
|
//
|
|
|
if ( names.length > 0 ) { |
|
|
// if ( names.length > 0 ) {
|
|
|
code.insertLeft( this.start, `var ${names.join( ', ' )};\n\n` ); |
|
|
// code.insertLeft( this.start, `var ${names.join( ', ' )};\n\n` );
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
if ( this.testValue ) { |
|
|
// if ( this.testValue ) {
|
|
|
code.remove( this.start, this.consequent.start ); |
|
|
// code.remove( this.start, this.consequent.start );
|
|
|
code.remove( this.consequent.end, this.end ); |
|
|
// code.remove( this.consequent.end, this.end );
|
|
|
this.consequent.render( code, es ); |
|
|
// this.consequent.render( code, es );
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else { |
|
|
// else {
|
|
|
code.remove( this.start, this.alternate ? this.alternate.start : this.next || this.end ); |
|
|
// code.remove( this.start, this.alternate ? this.alternate.start : this.next || this.end );
|
|
|
|
|
|
//
|
|
|
if ( this.alternate ) { |
|
|
// if ( this.alternate ) {
|
|
|
this.alternate.render( code, es ); |
|
|
// this.alternate.render( code, es );
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else if ( statementsWithIfStatements.has( this.parent.type ) ) { |
|
|
// else if ( statementsWithIfStatements.has( this.parent.type ) ) {
|
|
|
code.insertRight( this.start, '{}' ); |
|
|
// code.insertRight( this.start, '{}' );
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
//
|
|
|
else { |
|
|
// else {
|
|
|
super.render( code, es ); |
|
|
// super.render( code, es );
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
} |
|
|