Browse Source

assume functions can mutate their arguments

contingency-plan
Rich-Harris 10 years ago
parent
commit
3e0b919b71
  1. 22
      src/ast/analyse.js
  2. 3
      test/samples/function-side-effects/_config.js
  3. 3
      test/samples/function-side-effects/main.js
  4. 3
      test/samples/function-side-effects/mutate.js
  5. 6
      test/samples/function-side-effects/object.js

22
src/ast/analyse.js

@ -145,19 +145,27 @@ export default function analyse ( ast, magicString, module ) {
}
function checkForWrites ( node ) {
if ( node.type === 'AssignmentExpression' ) {
let assignee = node.left;
while ( assignee.type === 'MemberExpression' ) {
assignee = assignee.object;
function addNode ( node ) {
while ( node.type === 'MemberExpression' ) {
node = node.object;
}
if ( assignee.type !== 'Identifier' ) { // could be a ThisExpression
if ( node.type !== 'Identifier' ) {
return;
}
statement._modifies[ assignee.name ] = true;
statement._modifies[ node.name ] = true;
}
if ( node.type === 'AssignmentExpression' ) {
addNode( node.left );
}
else if ( node.type === 'CallExpression' ) {
node.arguments.forEach( addNode );
}
// TODO UpdateExpression
}
walk( statement, {

3
test/samples/function-side-effects/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'assumes functions may mutate objects passed in'
};

3
test/samples/function-side-effects/main.js

@ -0,0 +1,3 @@
import object from './object';
assert.ok( object.mutated );

3
test/samples/function-side-effects/mutate.js

@ -0,0 +1,3 @@
export default function mutate ( object ) {
object.mutated = true;
}

6
test/samples/function-side-effects/object.js

@ -0,0 +1,6 @@
import mutate from './mutate';
var object = {};
mutate( object );
export default object;
Loading…
Cancel
Save