Skip to content
Permalink
Browse files
Make sure property mutations on function arguments are not lost
  • Loading branch information
lukastaegert committed May 15, 2018
1 parent 4bb67cb commit 0f9b30e6c49c60551b791c20362300aa8513910b
Showing 6 changed files with 50 additions and 3 deletions.
@@ -1,11 +1,11 @@
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import SpreadElement from './SpreadElement';
import { isIdentifier } from './Identifier';
import Identifier from './Identifier';
import { ForEachReturnExpressionCallback, SomeReturnExpressionCallback } from './shared/Expression';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { ObjectPath } from '../values';
import { ObjectPath, UNKNOWN_KEY } from '../values';

export default class CallExpression extends NodeBase {
type: NodeType.tCallExpression;
@@ -16,7 +16,7 @@ export default class CallExpression extends NodeBase {

bind() {
super.bind();
if (isIdentifier(this.callee)) {
if (this.callee instanceof Identifier) {
const variable = this.scope.findVariable(this.callee.name);

if (variable.isNamespace) {
@@ -40,6 +40,10 @@ export default class CallExpression extends NodeBase {
);
}
}
for (const argument of this.arguments) {
// This will make sure all properties of parameters behave as "unknown"
argument.reassignPath([UNKNOWN_KEY], ExecutionPathOptions.create());
}
}

forEachReturnExpressionWhenCalledAtPath(
@@ -97,6 +97,7 @@ export default class LocalVariable extends Variable {
callOptions: CallOptions,
options: ExecutionPathOptions
) {
// The "included && path.length > 0" check is needed for mutating methods on arrays
if (path.length > MAX_PATH_DEPTH || (this.included && path.length > 0)) return true;
return (
this.reassignments.isPathReassigned(path) ||
@@ -0,0 +1,8 @@
var assert = require('assert');

module.exports = {
description: 'Associates value mutations across return values',
exports: function(exports) {
assert.equal(exports.bar, 'present');
}
};
@@ -0,0 +1,14 @@
const foo = {value: false};
const exported = {};

function getFoo() {
return foo;
}

getFoo().value = true;

if (foo.value) {
exported.bar = 'present'
}

export default exported;
@@ -0,0 +1,3 @@
module.exports = {
description: 'track reassignments of properties of function parameters'
};
@@ -0,0 +1,17 @@
const obj = {
flag: false
};

function reassign(x) {
x.flag = true;
}

reassign(obj);

let hasBeenReassigned = false;

if (obj.flag) {
hasBeenReassigned = true;
}

assert.ok(hasBeenReassigned);

0 comments on commit 0f9b30e

Please sign in to comment.