From b748464f711f0b81da287c83ed2c02716853efab Mon Sep 17 00:00:00 2001 From: kzc Date: Fri, 12 Feb 2016 11:51:43 -0500 Subject: [PATCH] Detect side effect in member expression assignment when not top level. Fixes #476. --- src/utils/run.js | 2 ++ .../_config.js | 10 ++++++++++ .../member-expression-assignment-in-function/main.js | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100644 test/function/member-expression-assignment-in-function/_config.js create mode 100644 test/function/member-expression-assignment-in-function/main.js diff --git a/src/utils/run.js b/src/utils/run.js index 4470305..b57149d 100644 --- a/src/utils/run.js +++ b/src/utils/run.js @@ -95,6 +95,8 @@ export default function run ( node, scope, statement, strongDependencies, force if ( declaration ) { if ( declaration.isParam ) hasSideEffect = true; + } else if ( !scope.isTopLevel ) { + hasSideEffect = true; } else { declaration = statement.module.trace( subject.name ); diff --git a/test/function/member-expression-assignment-in-function/_config.js b/test/function/member-expression-assignment-in-function/_config.js new file mode 100644 index 0000000..bf7e6fc --- /dev/null +++ b/test/function/member-expression-assignment-in-function/_config.js @@ -0,0 +1,10 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'detect side effect in member expression assignment when not top level', + code: function ( code ) { + assert.equal( code.indexOf( 'function set(key, value) { foo[key] = value; }' ) >= 0, true, code ); + assert.equal( code.indexOf( 'set("bar", 2);' ) >= 0, true, code ); + assert.equal( code.indexOf( 'set("qux", 3);' ) >= 0, true, code ); + } +} diff --git a/test/function/member-expression-assignment-in-function/main.js b/test/function/member-expression-assignment-in-function/main.js new file mode 100644 index 0000000..f627721 --- /dev/null +++ b/test/function/member-expression-assignment-in-function/main.js @@ -0,0 +1,9 @@ +var foo = {}; + +function set(key, value) { foo[key] = value; } + +set("bar", 2); +set("qux", 3); + +console.log(foo); +