Skip to content

Commit

Permalink
Revert function parameter tracking logic for now (#5487)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 23, 2024
1 parent b9a62fd commit 74da135
Show file tree
Hide file tree
Showing 90 changed files with 56 additions and 1,129 deletions.
10 changes: 1 addition & 9 deletions src/ast/nodes/ArrowFunctionExpression.ts
Expand Up @@ -5,9 +5,8 @@ import type ChildScope from '../scopes/ChildScope';
import ReturnValueScope from '../scopes/ReturnValueScope';
import { type ObjectPath } from '../utils/PathTracker';
import type BlockStatement from './BlockStatement';
import type CallExpression from './CallExpression';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import type * as NodeType from './NodeType';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import FunctionBase from './shared/FunctionBase';
import type { ExpressionNode, IncludeChildren } from './shared/Node';
Expand Down Expand Up @@ -68,13 +67,6 @@ export default class ArrowFunctionExpression extends FunctionBase {
return false;
}

protected onlyFunctionCallUsed(): boolean {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this;
return isIIFE || super.onlyFunctionCallUsed();
}

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
super.include(context, includeChildrenRecursively);
for (const parameter of this.params) {
Expand Down
25 changes: 7 additions & 18 deletions src/ast/nodes/ConditionalExpression.ts
@@ -1,5 +1,5 @@
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { BLANK, EMPTY_ARRAY } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import {
findFirstOccurrenceOutsideComment,
Expand Down Expand Up @@ -46,26 +46,15 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
}

deoptimizeCache(): void {
if (
this.usedBranch ||
this.isBranchResolutionAnalysed ||
this.expressionsToBeDeoptimized.length > 0
) {
// Request another pass because we need to ensure "include" runs again if it is rendered
this.scope.context.requestTreeshakingPass();
}
const { expressionsToBeDeoptimized } = this;
if (expressionsToBeDeoptimized.length > 0) {
this.expressionsToBeDeoptimized = [];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
}
this.isBranchResolutionAnalysed = false;
if (this.usedBranch !== null) {
const unusedBranch = this.usedBranch === this.consequent ? this.alternate : this.consequent;
this.usedBranch = null;
unusedBranch.deoptimizePath(UNKNOWN_PATH);
const { expressionsToBeDeoptimized } = this;
this.expressionsToBeDeoptimized = EMPTY_ARRAY as unknown as DeoptimizableEntity[];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
}
}

Expand All @@ -84,9 +73,9 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
this.expressionsToBeDeoptimized.push(origin);
const usedBranch = this.getUsedBranch();
if (!usedBranch) return UnknownValue;
this.expressionsToBeDeoptimized.push(origin);
return usedBranch.getLiteralValueAtPath(path, recursionTracker, origin);
}

Expand Down
5 changes: 0 additions & 5 deletions src/ast/nodes/FunctionDeclaration.ts
Expand Up @@ -14,11 +14,6 @@ export default class FunctionDeclaration extends FunctionNode {
}
}

protected onlyFunctionCallUsed(): boolean {
// call super.onlyFunctionCallUsed for export default anonymous function
return this.id?.variable.getOnlyFunctionCallUsed() ?? super.onlyFunctionCallUsed();
}

parseNode(esTreeNode: GenericEsTreeNode): this {
if (esTreeNode.id !== null) {
this.id = new Identifier(this, this.scope.parent as ChildScope).parseNode(
Expand Down
9 changes: 0 additions & 9 deletions src/ast/nodes/FunctionExpression.ts
Expand Up @@ -2,7 +2,6 @@ import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import ChildScope from '../scopes/ChildScope';
import type CallExpression from './CallExpression';
import type { IdentifierWithVariable } from './Identifier';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
Expand All @@ -26,14 +25,6 @@ export default class FunctionExpression extends FunctionNode {
return super.parseNode(esTreeNode);
}

protected onlyFunctionCallUsed(): boolean {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this &&
(this.id === null || this.id.variable.getOnlyFunctionCallUsed());
return isIIFE || super.onlyFunctionCallUsed();
}

render(
code: MagicString,
options: RenderOptions,
Expand Down
5 changes: 0 additions & 5 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -60,12 +60,10 @@ export default class Identifier extends NodeBase implements PatternNode {
}
}

private isReferenceVariable = false;
bind(): void {
if (!this.variable && isReference(this, this.parent as NodeWithFieldDefinition)) {
this.variable = this.scope.findVariable(this.name);
this.variable.addReference(this);
this.isReferenceVariable = true;
}
}

Expand Down Expand Up @@ -297,9 +295,6 @@ export default class Identifier extends NodeBase implements PatternNode {
this.variable.consolidateInitializers();
this.scope.context.requestTreeshakingPass();
}
if (this.isReferenceVariable) {
this.variable!.addUsedPlace(this);
}
}

private getVariableRespectingTDZ(): ExpressionEntity | null {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -7,7 +7,7 @@ import { EMPTY_PATH, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import BlockStatement from './BlockStatement';
import type Identifier from './Identifier';
import * as NodeType from './NodeType';
import { type LiteralValueOrUnknown } from './shared/Expression';
import { type LiteralValueOrUnknown, UnknownValue } from './shared/Expression';
import {
type ExpressionNode,
type GenericEsTreeNode,
Expand All @@ -29,7 +29,7 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
private testValue: LiteralValueOrUnknown | typeof unset = unset;

deoptimizeCache(): void {
this.testValue = unset;
this.testValue = UnknownValue;
}

hasEffects(context: HasEffectsContext): boolean {
Expand Down
31 changes: 13 additions & 18 deletions src/ast/nodes/LogicalExpression.ts
@@ -1,5 +1,5 @@
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { BLANK, EMPTY_ARRAY } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
Expand Down Expand Up @@ -57,26 +57,21 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
}

deoptimizeCache(): void {
if (
this.usedBranch ||
this.isBranchResolutionAnalysed ||
this.expressionsToBeDeoptimized.length > 0
) {
// Request another pass because we need to ensure "include" runs again if it is rendered
this.scope.context.requestTreeshakingPass();
}
const { expressionsToBeDeoptimized } = this;
if (expressionsToBeDeoptimized.length > 0) {
this.expressionsToBeDeoptimized = [];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
}
this.isBranchResolutionAnalysed = false;
if (this.usedBranch) {
const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
this.usedBranch = null;
unusedBranch.deoptimizePath(UNKNOWN_PATH);
const {
scope: { context },
expressionsToBeDeoptimized
} = this;
this.expressionsToBeDeoptimized = EMPTY_ARRAY as unknown as DeoptimizableEntity[];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
// Request another pass because we need to ensure "include" runs again if
// it is rendered
context.requestTreeshakingPass();
}
}

Expand All @@ -95,9 +90,9 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
this.expressionsToBeDeoptimized.push(origin);
const usedBranch = this.getUsedBranch();
if (!usedBranch) return UnknownValue;
this.expressionsToBeDeoptimized.push(origin);
return usedBranch.getLiteralValueAtPath(path, recursionTracker, origin);
}

Expand Down
7 changes: 2 additions & 5 deletions src/ast/nodes/MemberExpression.ts
@@ -1,7 +1,7 @@
import type MagicString from 'magic-string';
import type { AstContext } from '../../Module';
import type { NormalizedTreeshakingOptions } from '../../rollup/types';
import { BLANK } from '../../utils/blank';
import { BLANK, EMPTY_ARRAY } from '../../utils/blank';
import { LOGLEVEL_WARN } from '../../utils/logging';
import { logIllegalImportReassignment, logMissingExport } from '../../utils/logs';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
Expand Down Expand Up @@ -185,7 +185,7 @@ export default class MemberExpression

deoptimizeCache(): void {
const { expressionsToBeDeoptimized, object } = this;
this.expressionsToBeDeoptimized = [];
this.expressionsToBeDeoptimized = EMPTY_ARRAY as unknown as DeoptimizableEntity[];
this.propertyKey = UnknownKey;
object.deoptimizePath(UNKNOWN_PATH);
for (const expression of expressionsToBeDeoptimized) {
Expand Down Expand Up @@ -396,9 +396,6 @@ export default class MemberExpression
);
this.scope.context.requestTreeshakingPass();
}
if (this.variable) {
this.variable.addUsedPlace(this);
}
}

private applyAssignmentDeoptimization(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/UpdateExpression.ts
Expand Up @@ -87,7 +87,7 @@ export default class UpdateExpression extends NodeBase {
this.argument.deoptimizePath(EMPTY_PATH);
if (this.argument instanceof Identifier) {
const variable = this.scope.findVariable(this.argument.name);
variable.markReassigned();
variable.isReassigned = true;
}
this.scope.context.requestTreeshakingPass();
}
Expand Down

0 comments on commit 74da135

Please sign in to comment.