Skip to content

Commit

Permalink
Track access side effects when using object spread operator (#4113)
Browse files Browse the repository at this point in the history
* Check for access side effects when using the object spread operator

* Make test a little more robust
  • Loading branch information
lukastaegert committed May 29, 2021
1 parent 1dd8ba7 commit 4eeef6e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/ast/nodes/SpreadElement.ts
@@ -1,5 +1,6 @@
import { HasEffectsContext } from '../ExecutionContext';
import { NodeEvent } from '../NodeEvents';
import { ObjectPath, PathTracker, UnknownKey } from '../utils/PathTracker';
import { ObjectPath, PathTracker, UNKNOWN_PATH, UnknownKey } from '../utils/PathTracker';
import * as NodeType from './NodeType';
import { ExpressionEntity } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
Expand All @@ -25,6 +26,13 @@ export default class SpreadElement extends NodeBase {
}
}

hasEffects(context: HasEffectsContext): boolean {
return (
this.argument.hasEffects(context) ||
this.argument.hasEffectsWhenAccessedAtPath(UNKNOWN_PATH, context)
);
}

protected applyDeoptimizations(): void {
this.deoptimized = true;
// Only properties of properties of the argument could become subject to reassignment
Expand Down
Expand Up @@ -23,7 +23,7 @@ module.exports = {
format: 'es'
}
}),
500
1000
);
});
`
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/object-spread-side-effect/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'triggers getter side effects when spreading objects'
};
9 changes: 9 additions & 0 deletions test/function/samples/object-spread-side-effect/main.js
@@ -0,0 +1,9 @@
let result = 'FAIL';
const unused = {
...{
get prop() {
result = 'PASS';
}
}
};
assert.strictEqual(result, 'PASS');

0 comments on commit 4eeef6e

Please sign in to comment.