From d46d67f1c71d3ad143d3d03a0f681c7f8885d308 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 2 Jun 2015 14:55:30 -0400 Subject: [PATCH] method call is assumed to mutate method owner (#13) --- src/Statement.js | 5 +++++ test/function/method-call-mutates-this/_config.js | 3 +++ test/function/method-call-mutates-this/foo.js | 15 +++++++++++++++ test/function/method-call-mutates-this/main.js | 3 +++ 4 files changed, 26 insertions(+) create mode 100644 test/function/method-call-mutates-this/_config.js create mode 100644 test/function/method-call-mutates-this/foo.js create mode 100644 test/function/method-call-mutates-this/main.js diff --git a/src/Statement.js b/src/Statement.js index 1c46cc0..fdc0cab 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -188,6 +188,11 @@ export default class Statement { else if ( node.type === 'CallExpression' ) { node.arguments.forEach( arg => addNode( arg, false ) ); + + // `foo.bar()` is assumed to mutate foo + if ( node.callee.type === 'MemberExpression' ) { + addNode( node.callee ); + } } } diff --git a/test/function/method-call-mutates-this/_config.js b/test/function/method-call-mutates-this/_config.js new file mode 100644 index 0000000..66fe25b --- /dev/null +++ b/test/function/method-call-mutates-this/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'method calls are assumed to mutate the owner' +}; diff --git a/test/function/method-call-mutates-this/foo.js b/test/function/method-call-mutates-this/foo.js new file mode 100644 index 0000000..b58506d --- /dev/null +++ b/test/function/method-call-mutates-this/foo.js @@ -0,0 +1,15 @@ +var obj = { + set: function ( key, value ) { + this[ key ] = value; + }, + + get: function ( key ) { + return this[ key ]; + } +}; + +obj.set( 'answer', 42 ); + +export default function ( key ) { + return obj.get( key ); +} diff --git a/test/function/method-call-mutates-this/main.js b/test/function/method-call-mutates-this/main.js new file mode 100644 index 0000000..354a618 --- /dev/null +++ b/test/function/method-call-mutates-this/main.js @@ -0,0 +1,3 @@ +import foo from './foo'; + +assert.equal( foo( 'answer' ), 42 );