From 819d619aa14d27e0d59e2bcd3ed93f2dccff8833 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 18 Sep 2016 10:50:08 -0400 Subject: [PATCH 1/2] include all ancestors of expression with effects, up to function boundary - fixes #930 --- src/Bundle.js | 4 +++- src/ast/nodes/UpdateExpression.js | 4 +++- test/function/if-statement-with-assignment/_config.js | 3 +++ test/function/if-statement-with-assignment/main.js | 4 ++++ test/function/if-statement-with-update/_config.js | 3 +++ test/function/if-statement-with-update/main.js | 4 ++++ 6 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/function/if-statement-with-assignment/_config.js create mode 100644 test/function/if-statement-with-assignment/main.js create mode 100644 test/function/if-statement-with-update/_config.js create mode 100644 test/function/if-statement-with-update/main.js diff --git a/src/Bundle.js b/src/Bundle.js index 2716102..d56f4a5 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -141,7 +141,9 @@ export default class Bundle { let i = this.dependentExpressions.length; while ( i-- ) { const expression = this.dependentExpressions[i]; - const statement = expression.findParent( /ExpressionStatement/ ); + + let statement = expression; + while ( statement.parent && !/Function/.test( statement.parent.type ) ) statement = statement.parent; if ( !statement || statement.ran ) { this.dependentExpressions.splice( i, 1 ); diff --git a/src/ast/nodes/UpdateExpression.js b/src/ast/nodes/UpdateExpression.js index c5b9263..656da69 100644 --- a/src/ast/nodes/UpdateExpression.js +++ b/src/ast/nodes/UpdateExpression.js @@ -28,11 +28,13 @@ export default class UpdateExpression extends Node { } initialise ( scope ) { + this.scope = scope; + this.module.bundle.dependentExpressions.push( this ); super.initialise( scope ); } isUsedByBundle () { - return isUsedByBundle( this.findScope(), this.subject ); + return isUsedByBundle( this.scope, this.subject ); } } diff --git a/test/function/if-statement-with-assignment/_config.js b/test/function/if-statement-with-assignment/_config.js new file mode 100644 index 0000000..6c573d5 --- /dev/null +++ b/test/function/if-statement-with-assignment/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'update assignments to names are preserved (#930)' +}; diff --git a/test/function/if-statement-with-assignment/main.js b/test/function/if-statement-with-assignment/main.js new file mode 100644 index 0000000..612436b --- /dev/null +++ b/test/function/if-statement-with-assignment/main.js @@ -0,0 +1,4 @@ +var result = 0; +if ( Math.random() <= 1 ) result += 1; + +assert.equal( result, 1 ); diff --git a/test/function/if-statement-with-update/_config.js b/test/function/if-statement-with-update/_config.js new file mode 100644 index 0000000..8601e12 --- /dev/null +++ b/test/function/if-statement-with-update/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'updates to names are preserved (#930)' +}; diff --git a/test/function/if-statement-with-update/main.js b/test/function/if-statement-with-update/main.js new file mode 100644 index 0000000..9239794 --- /dev/null +++ b/test/function/if-statement-with-update/main.js @@ -0,0 +1,4 @@ +var result = 0; +if ( Math.random() <= 1 ) ++result; + +assert.equal( result, 1 ); From f143b01c6206366a1a7a702d79b514ffcaedd696 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 18 Sep 2016 11:28:34 -0400 Subject: [PATCH 2/2] beef up test --- test/function/if-statement-with-assignment/main.js | 4 +++- test/function/if-statement-with-update/main.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/function/if-statement-with-assignment/main.js b/test/function/if-statement-with-assignment/main.js index 612436b..e885c22 100644 --- a/test/function/if-statement-with-assignment/main.js +++ b/test/function/if-statement-with-assignment/main.js @@ -1,4 +1,6 @@ var result = 0; -if ( Math.random() <= 1 ) result += 1; +if ( Math.random() <= 1 ) { + if ( Math.random() <= 1 ) result += 1; +} assert.equal( result, 1 ); diff --git a/test/function/if-statement-with-update/main.js b/test/function/if-statement-with-update/main.js index 9239794..7ab3055 100644 --- a/test/function/if-statement-with-update/main.js +++ b/test/function/if-statement-with-update/main.js @@ -1,4 +1,6 @@ var result = 0; -if ( Math.random() <= 1 ) ++result; +if ( Math.random() <= 1 ) { + if ( Math.random() <= 1 ) ++result; +} assert.equal( result, 1 );