Skip to content

Commit

Permalink
Merge branch 'master' into gh-4093-object-spread-side-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 29, 2021
2 parents 0ade205 + 1dd8ba7 commit 855b37b
Show file tree
Hide file tree
Showing 23 changed files with 88 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Module.ts
Expand Up @@ -109,6 +109,7 @@ export interface AstContext {
moduleContext: string;
nodeConstructors: { [name: string]: typeof NodeBase };
options: NormalizedInputOptions;
requestTreeshakingPass: () => void;
traceExport: (name: string) => Variable | null;
traceVariable: (name: string) => Variable | null;
usesTopLevelAwait: boolean;
Expand Down Expand Up @@ -726,6 +727,7 @@ export default class Module {
moduleContext: this.context,
nodeConstructors,
options: this.options,
requestTreeshakingPass: () => (this.graph.needsTreeshakingPass = true),
traceExport: this.getVariableForExportName.bind(this),
traceVariable: this.traceVariable.bind(this),
usesTopLevelAwait: false,
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/AssignmentExpression.ts
Expand Up @@ -126,5 +126,6 @@ export default class AssignmentExpression extends NodeBase {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/AssignmentPattern.ts
Expand Up @@ -48,5 +48,6 @@ export default class AssignmentPattern extends NodeBase implements PatternNode {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/CallExpression.ts
Expand Up @@ -298,6 +298,7 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
// This will make sure all properties of parameters behave as "unknown"
argument.deoptimizePath(UNKNOWN_PATH);
}
this.context.requestTreeshakingPass();
}

private getReturnExpression(
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/ForInStatement.ts
Expand Up @@ -65,5 +65,6 @@ export default class ForInStatement extends StatementBase {
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.context.requestTreeshakingPass();
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/ForOfStatement.ts
Expand Up @@ -50,5 +50,6 @@ export default class ForOfStatement extends StatementBase {
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.context.requestTreeshakingPass();
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -178,6 +178,7 @@ export default class Identifier extends NodeBase implements PatternNode {
this.deoptimized = true;
if (this.variable !== null && this.variable instanceof LocalVariable) {
this.variable.consolidateInitializers();
this.context.requestTreeshakingPass();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/MemberExpression.ts
Expand Up @@ -329,6 +329,7 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
SHARED_RECURSION_TRACKER
);
}
this.context.requestTreeshakingPass();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/NewExpression.ts
Expand Up @@ -46,5 +46,6 @@ export default class NewExpression extends NodeBase {
// This will make sure all properties of parameters behave as "unknown"
argument.deoptimizePath(UNKNOWN_PATH);
}
this.context.requestTreeshakingPass();
}
}
15 changes: 7 additions & 8 deletions src/ast/nodes/ObjectExpression.ts
Expand Up @@ -17,12 +17,7 @@ import Literal from './Literal';
import * as NodeType from './NodeType';
import Property from './Property';
import SpreadElement from './SpreadElement';
import {
ExpressionEntity,
LiteralValueOrUnknown,
UNKNOWN_EXPRESSION,
UnknownValue
} from './shared/Expression';
import { ExpressionEntity, LiteralValueOrUnknown, UnknownValue } from './shared/Expression';
import { NodeBase } from './shared/Node';
import { ObjectEntity, ObjectProperty } from './shared/ObjectEntity';
import { OBJECT_PROTOTYPE } from './shared/ObjectPrototype';
Expand Down Expand Up @@ -112,6 +107,7 @@ export default class ObjectExpression extends NodeBase implements DeoptimizableE
if (this.objectEntity !== null) {
return this.objectEntity;
}
let prototype: ExpressionEntity | null = OBJECT_PROTOTYPE;
const properties: ObjectProperty[] = [];
for (const property of this.properties) {
if (property instanceof SpreadElement) {
Expand All @@ -137,12 +133,15 @@ export default class ObjectExpression extends NodeBase implements DeoptimizableE
? property.key.name
: String((property.key as Literal).value);
if (key === '__proto__' && property.kind === 'init') {
properties.unshift({ key: UnknownKey, kind: 'init', property: UNKNOWN_EXPRESSION });
prototype =
property.value instanceof Literal && property.value.value === null
? null
: property.value;
continue;
}
}
properties.push({ key, kind: property.kind, property });
}
return (this.objectEntity = new ObjectEntity(properties, OBJECT_PROTOTYPE));
return (this.objectEntity = new ObjectEntity(properties, prototype));
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/Property.ts
Expand Up @@ -46,6 +46,7 @@ export default class Property extends MethodBase implements PatternNode {
this.deoptimized = true;
if (this.declarationInit !== null) {
this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
this.context.requestTreeshakingPass();
}
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/RestElement.ts
Expand Up @@ -37,6 +37,7 @@ export default class RestElement extends NodeBase implements PatternNode {
this.deoptimized = true;
if (this.declarationInit !== null) {
this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
this.context.requestTreeshakingPass();
}
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/SpreadElement.ts
Expand Up @@ -38,5 +38,6 @@ export default class SpreadElement extends NodeBase {
// Only properties of properties of the argument could become subject to reassignment
// This will also reassign the return values of iterators
this.argument.deoptimizePath([UnknownKey, UnknownKey]);
this.context.requestTreeshakingPass();
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/UnaryExpression.ts
Expand Up @@ -59,6 +59,7 @@ export default class UnaryExpression extends NodeBase {
this.deoptimized = true;
if (this.operator === 'delete') {
this.argument.deoptimizePath(EMPTY_PATH);
this.context.requestTreeshakingPass();
}
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/UpdateExpression.ts
Expand Up @@ -87,5 +87,6 @@ export default class UpdateExpression extends NodeBase {
const variable = this.scope.findVariable(this.argument.name);
variable.isReassigned = true;
}
this.context.requestTreeshakingPass();
}
}
6 changes: 5 additions & 1 deletion src/ast/nodes/YieldExpression.ts
Expand Up @@ -30,6 +30,10 @@ export default class YieldExpression extends NodeBase {

protected applyDeoptimizations(): void {
this.deoptimized = true;
this.argument?.deoptimizePath(UNKNOWN_PATH);
const { argument } = this;
if (argument) {
argument.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}
}
3 changes: 3 additions & 0 deletions test/form/samples/proto-null/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles getters and setters on __proto__ properties'
};
2 changes: 2 additions & 0 deletions test/form/samples/proto-null/_expected.js
@@ -0,0 +1,2 @@
console.log('retained');
console.log('retained');
5 changes: 5 additions & 0 deletions test/form/samples/proto-null/main.js
@@ -0,0 +1,5 @@
const a = { __proto__: null };
if (a.hasOwnProperty) console.log('removed');
else console.log('retained');
if (a.foo) console.log('removed');
else console.log('retained');
@@ -0,0 +1,10 @@
const assert = require('assert');
const result = { value: 0 };

module.exports = {
description: 'makes sure to request additional passes when a variable is deoptimized',
context: { result },
exports() {
assert.strictEqual(result.value, 2);
}
};
17 changes: 17 additions & 0 deletions test/function/samples/deoptimize-request-treeshaking-pass/main.js
@@ -0,0 +1,17 @@
function heisenbug() {
var a = false;
function f(b) {
if (a) a === b.c ? result.value++ : result.value--;
a = b.c;
}
function g() {}
function h() {
f({
c: g
});
}
h();
h();
}
heisenbug();
heisenbug();
3 changes: 3 additions & 0 deletions test/function/samples/proto-accessors/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles getters and setters on __proto__ properties'
};
21 changes: 21 additions & 0 deletions test/function/samples/proto-accessors/main.js
@@ -0,0 +1,21 @@
let getter_effect = 'FAIL';
let setter_effect = 'FAIL';
let proto = {
get foo() {
getter_effect = 'PASS';
},
set bar(value) {
setter_effect = 'PASS';
}
};
let obj1 = {
__proto__: proto
};
let obj2 = {
__proto__: proto
};
let unused = obj1.foo;
obj2.bar = 0;

assert.strictEqual(getter_effect, 'PASS');
assert.strictEqual(setter_effect, 'PASS');

0 comments on commit 855b37b

Please sign in to comment.