Skip to content

Commit

Permalink
Track reassignments of array elements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 20, 2018
1 parent 7911882 commit dd35b6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ast/nodes/ArrayExpression.ts
Expand Up @@ -5,7 +5,8 @@ import {
getMemberReturnExpressionWhenCalled,
hasMemberEffectWhenCalled,
ObjectPath,
UNKNOWN_EXPRESSION
UNKNOWN_EXPRESSION,
UNKNOWN_PATH
} from '../values';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
Expand All @@ -15,6 +16,13 @@ export default class ArrayExpression extends NodeBase {
type: NodeType.tArrayExpression;
elements: (ExpressionNode | SpreadElement | null)[];

bind() {
super.bind();
for (const element of this.elements) {
if (element !== null) element.reassignPath(UNKNOWN_PATH);
}
}

getReturnExpressionWhenCalledAtPath(path: ObjectPath) {
if (path.length !== 1) return UNKNOWN_EXPRESSION;
return getMemberReturnExpressionWhenCalled(arrayMembers, path[0]);
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'makes sure reassignments of array elements are tracked'
};
8 changes: 8 additions & 0 deletions test/function/samples/reassign-array-literal-elements/main.js
@@ -0,0 +1,8 @@
var foo = {x: true};
var bar = {x: true};
var baz = [foo, bar];

baz[0].x = false;
baz[1].x = false;

if (foo.x) assert.fail('foo was not reassigned');

0 comments on commit dd35b6e

Please sign in to comment.