Skip to content

Commit

Permalink
Ensure parameter defaults are deoptimized, just like arguments (rollu…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert authored and pos777 committed Jun 18, 2022
1 parent 75feb1d commit b09d401
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ast/nodes/ArrowFunctionExpression.ts
Expand Up @@ -25,6 +25,7 @@ export default class ArrowFunctionExpression extends FunctionBase {
}

hasEffects(): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
return false;
}

Expand Down
11 changes: 11 additions & 0 deletions src/ast/nodes/shared/FunctionBase.ts
Expand Up @@ -175,6 +175,7 @@ export default abstract class FunctionBase extends NodeBase implements Deoptimiz
includeChildrenRecursively: IncludeChildren,
{ includeWithoutParameterDefaults }: InclusionOptions = BLANK
): void {
if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
const { brokenFlow } = context;
context.brokenFlow = BROKEN_FLOW_NONE;
Expand Down Expand Up @@ -244,6 +245,16 @@ export default abstract class FunctionBase extends NodeBase implements Deoptimiz
super.parseNode(esTreeNode);
}

protected applyDeoptimizations() {
// We currently do not track deoptimizations of default values, deoptimize them
// just as we deoptimize call arguments
for (const param of this.params) {
if (param instanceof AssignmentPattern) {
param.right.deoptimizePath(UNKNOWN_PATH);
}
}
}

protected abstract getObjectEntity(): ObjectEntity;
}

Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/shared/FunctionNode.ts
Expand Up @@ -39,6 +39,7 @@ export default class FunctionNode extends FunctionBase {
}

hasEffects(): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
return !!this.id?.hasEffects();
}

Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'deoptimizes functions supplied as default values'
};
@@ -0,0 +1,4 @@
const foo = (a = 'fallback') => a;
const bar = (a = foo) => a;

assert.strictEqual(bar()(), 'fallback');

0 comments on commit b09d401

Please sign in to comment.