Browse Source

Merge pull request #512 from kzc/issue476

Detect side effect in member expression assignment when not top level
gh-669
Rich Harris 9 years ago
parent
commit
2e8d55367a
  1. 2
      src/utils/run.js
  2. 10
      test/function/member-expression-assignment-in-function/_config.js
  3. 9
      test/function/member-expression-assignment-in-function/main.js

2
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 );

10
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 );
}
}

9
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);
Loading…
Cancel
Save