diff --git a/src/ast/nodes/ArrowFunctionExpression.js b/src/ast/nodes/ArrowFunctionExpression.js index 6ea7770..f9b07b4 100644 --- a/src/ast/nodes/ArrowFunctionExpression.js +++ b/src/ast/nodes/ArrowFunctionExpression.js @@ -1,36 +1,13 @@ import Node from '../Node.js'; import Scope from '../scopes/Scope.js'; import extractNames from '../utils/extractNames.js'; +import FunctionValue from './shared/FunctionValue.js'; export default class ArrowFunctionExpression extends Node { bind ( scope ) { super.bind( this.scope || scope ); } - call ( context, args ) { - // TODO account for `this` and `arguments` - if ( this.isCalling ) return; // recursive functions - this.isCalling = true; - - this.body.scope.initialise(); - - args.forEach( ( arg, i ) => { - const param = this.params[i]; - - if ( param.type !== 'Identifier' ) { - throw new Error( 'TODO desctructuring' ); - } - - throw new Error( 'TODO setValue' ); - }); - - for ( const node of this.body.body ) { - node.run(); - } - - this.isCalling = false; - } - findScope ( functionScope ) { return this.scope || this.parent.findScope( functionScope ); } @@ -62,8 +39,7 @@ export default class ArrowFunctionExpression extends Node { super.initialise( this.scope ); } - markReturnStatements () { - // TODO implicit returns - this.returnStatements.forEach( statement => statement.mark() ); + run () { + return new FunctionValue( this ); } } diff --git a/src/ast/nodes/AssignmentExpression.js b/src/ast/nodes/AssignmentExpression.js index e336cd3..7ea142e 100644 --- a/src/ast/nodes/AssignmentExpression.js +++ b/src/ast/nodes/AssignmentExpression.js @@ -43,9 +43,6 @@ export default class AssignmentExpression extends Node { initialise ( scope ) { this.scope = scope; - - this.module.bundle.potentialEffects.push( this ); - super.initialise( scope ); } @@ -54,6 +51,8 @@ export default class AssignmentExpression extends Node { } run () { + this.module.bundle.potentialEffects.push( this ); + const rightValue = this.right.run(); if ( this.operator === '=' ) { diff --git a/src/ast/nodes/FunctionExpression.js b/src/ast/nodes/FunctionExpression.js index 3e97e42..ef0354f 100644 --- a/src/ast/nodes/FunctionExpression.js +++ b/src/ast/nodes/FunctionExpression.js @@ -21,31 +21,6 @@ export default class FunctionExpression extends Node { this.body.bind(); } - call ( context, args ) { - if ( this.isCalling ) return; // recursive functions - this.isCalling = true; - - this.body.scope.initialise(); - - args.forEach( ( arg, i ) => { - const param = this.params[i]; - - if ( !param ) return; - - if ( param.type !== 'Identifier' ) { - throw new Error( 'TODO desctructuring' ); - } - - this.body.scope.setValue( param.name, arg ); - }); - - for ( const node of this.body.body ) { - node.run(); - } - - this.isCalling = false; - } - getName () { return this.name; } @@ -74,10 +49,6 @@ export default class FunctionExpression extends Node { super.mark(); } - markReturnStatements () { - this.returnStatements.forEach( statement => statement.mark() ); - } - run () { return new FunctionValue( this ); } diff --git a/src/ast/nodes/MemberExpression.js b/src/ast/nodes/MemberExpression.js index 7ccd39d..82b2ca2 100644 --- a/src/ast/nodes/MemberExpression.js +++ b/src/ast/nodes/MemberExpression.js @@ -132,6 +132,11 @@ export default class MemberExpression extends Node { const objectValue = this.object.run(); const propValue = this.computed ? this.property.run() : this.property.name; + if ( !objectValue ) { + console.log( this.object ) + console.log( `${this}` ) + } + if ( !objectValue.setProperty ) { console.log( `${this}` ) console.log( objectValue ); diff --git a/src/ast/nodes/NewExpression.js b/src/ast/nodes/NewExpression.js index 590a407..3764334 100644 --- a/src/ast/nodes/NewExpression.js +++ b/src/ast/nodes/NewExpression.js @@ -42,6 +42,6 @@ export default class NewExpression extends Node { } // TODO context... - return this.callee.call( this.arguments ); + return this.callee.call( undefined, this.arguments ); } } diff --git a/src/ast/nodes/ThrowStatement.js b/src/ast/nodes/ThrowStatement.js index b9a8aa3..7238430 100644 --- a/src/ast/nodes/ThrowStatement.js +++ b/src/ast/nodes/ThrowStatement.js @@ -4,4 +4,8 @@ export default class ThrowStatement extends Node { hasEffects () { return true; } + + run () { + this.mark(); + } } diff --git a/src/ast/nodes/VariableDeclaration.js b/src/ast/nodes/VariableDeclaration.js index 8d92b8e..cc575ae 100644 --- a/src/ast/nodes/VariableDeclaration.js +++ b/src/ast/nodes/VariableDeclaration.js @@ -25,8 +25,6 @@ export default class VariableDeclaration extends Node { render ( code, es ) { const treeshake = this.module.bundle.treeshake; - console.group( `rendering ${this}` ) - let shouldSeparate = false; let separator; @@ -46,7 +44,6 @@ export default class VariableDeclaration extends Node { if ( declarator.id.type === 'Identifier' ) { const proxy = declarator.proxies.get( declarator.id.name ); const isExportedAndReassigned = !es && proxy.exportName && proxy.isReassigned; - console.log( `declarator.init && declarator.init.isMarked`, declarator.init && declarator.init.isMarked ) if ( isExportedAndReassigned ) { if ( declarator.init ) { @@ -93,8 +90,6 @@ export default class VariableDeclaration extends Node { declarator.render( code, es ); } - console.log( `empty`, empty ) - if ( treeshake && empty ) { code.remove( this.leadingCommentStart || this.start, this.next || this.end ); } else { @@ -108,7 +103,5 @@ export default class VariableDeclaration extends Node { this.insertSemicolon( code ); } } - - console.groupEnd() } } diff --git a/src/ast/nodes/VariableDeclarator.js b/src/ast/nodes/VariableDeclarator.js index 53d35cf..066248a 100644 --- a/src/ast/nodes/VariableDeclarator.js +++ b/src/ast/nodes/VariableDeclarator.js @@ -52,8 +52,6 @@ export default class VariableDeclarator extends Node { if ( this.activated ) return; this.activated = true; - console.log( `activating ${this}` ) - this.mark(); if ( this.init ) this.init.markChildren(); } diff --git a/src/ast/nodes/shared/FunctionValue.js b/src/ast/nodes/shared/FunctionValue.js index c311528..d1ebf58 100644 --- a/src/ast/nodes/shared/FunctionValue.js +++ b/src/ast/nodes/shared/FunctionValue.js @@ -34,10 +34,12 @@ export default class FunctionValue { throw new Error( 'TODO desctructuring' ); } - this.node.body.scope.setValue( param.name, arg ); + this.node.body.scope.setValue( param.name, arg.run() ); }); - for ( const node of this.node.body.body ) { + const body = this.node.type === 'ArrowFunctionExpression' && this.node.body.type !== 'BlockStatement' ? [ this.node.body ] : this.node.body.body; + + for ( const node of body ) { node.run(); if ( node.type === 'ReturnStatement' ) { returnValue = node.argument ? node.argument.run() : undefined; // TODO represent undefined diff --git a/test/form/exclude-unnecessary-modifications/_config.js b/test/form/exclude-unnecessary-modifications/_config.js index a77c54e..2b3de2b 100644 --- a/test/form/exclude-unnecessary-modifications/_config.js +++ b/test/form/exclude-unnecessary-modifications/_config.js @@ -1,4 +1,3 @@ module.exports = { - solo: true, description: 'statements that modify definitions within unused functions are excluded' }; diff --git a/test/form/side-effect-h/_expected/amd.js b/test/form/side-effect-h/_expected/amd.js index ab252ed..6c907c7 100644 --- a/test/form/side-effect-h/_expected/amd.js +++ b/test/form/side-effect-h/_expected/amd.js @@ -2,7 +2,7 @@ define(function () { 'use strict'; function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } } diff --git a/test/form/side-effect-h/_expected/cjs.js b/test/form/side-effect-h/_expected/cjs.js index 1d43729..ec44812 100644 --- a/test/form/side-effect-h/_expected/cjs.js +++ b/test/form/side-effect-h/_expected/cjs.js @@ -2,7 +2,7 @@ function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } } diff --git a/test/form/side-effect-h/_expected/es.js b/test/form/side-effect-h/_expected/es.js index 1462a92..55856fb 100644 --- a/test/form/side-effect-h/_expected/es.js +++ b/test/form/side-effect-h/_expected/es.js @@ -1,6 +1,6 @@ function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } } diff --git a/test/form/side-effect-h/_expected/iife.js b/test/form/side-effect-h/_expected/iife.js index 286de2a..3d50da9 100644 --- a/test/form/side-effect-h/_expected/iife.js +++ b/test/form/side-effect-h/_expected/iife.js @@ -3,7 +3,7 @@ var myBundle = (function () { function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } } diff --git a/test/form/side-effect-h/_expected/umd.js b/test/form/side-effect-h/_expected/umd.js index 8fabe32..ff9525f 100644 --- a/test/form/side-effect-h/_expected/umd.js +++ b/test/form/side-effect-h/_expected/umd.js @@ -6,7 +6,7 @@ function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } } diff --git a/test/form/side-effect-h/main.js b/test/form/side-effect-h/main.js index 16d7346..fda56ac 100644 --- a/test/form/side-effect-h/main.js +++ b/test/form/side-effect-h/main.js @@ -1,6 +1,6 @@ function foo ( ok ) { if ( !ok ) { - throw new Error( 'this will be ignored' ); + throw new Error( 'this will be included' ); } }