diff --git a/src/ast/NodeInteractions.ts b/src/ast/NodeInteractions.ts index e73af1a122e..35113cb5b0f 100644 --- a/src/ast/NodeInteractions.ts +++ b/src/ast/NodeInteractions.ts @@ -1,7 +1,6 @@ import SpreadElement from './nodes/SpreadElement'; import { ExpressionEntity, UNKNOWN_EXPRESSION } from './nodes/shared/Expression'; -// TODO Lukas for usages and see if caching makes sense export const INTERACTION_ACCESSED = 0; export const INTERACTION_ASSIGNED = 1; export const INTERACTION_CALLED = 2; @@ -11,7 +10,7 @@ export interface NodeInteractionAccessed { type: typeof INTERACTION_ACCESSED; } -export const NODE_INTERACTION_ACCESS: NodeInteractionAccessed = { +export const NODE_INTERACTION_UNKNOWN_ACCESS: NodeInteractionAccessed = { thisArg: null, type: INTERACTION_ACCESSED }; @@ -39,6 +38,16 @@ export interface NodeInteractionCalled { export const NO_ARGS = []; +// While this is technically a call without arguments, we can compare against +// this reference in places where precise values or thisArg would make a +// difference +export const NODE_INTERACTION_UNKNOWN_CALL: NodeInteractionCalled = { + args: NO_ARGS, + thisArg: null, + type: INTERACTION_CALLED, + withNew: false +}; + export type NodeInteraction = | NodeInteractionAccessed | NodeInteractionAssigned diff --git a/src/ast/nodes/Identifier.ts b/src/ast/nodes/Identifier.ts index 73bb9f90f59..b4ddde25c0f 100644 --- a/src/ast/nodes/Identifier.ts +++ b/src/ast/nodes/Identifier.ts @@ -10,7 +10,7 @@ import { INTERACTION_ACCESSED, INTERACTION_ASSIGNED, INTERACTION_CALLED, - NODE_INTERACTION_ACCESS, + NODE_INTERACTION_UNKNOWN_ACCESS, NodeInteraction, NodeInteractionCalled } from '../NodeInteractions'; @@ -140,7 +140,11 @@ export default class Identifier extends NodeBase implements PatternNode { return ( (this.context.options.treeshake as NormalizedTreeshakingOptions).unknownGlobalSideEffects && this.variable instanceof GlobalVariable && - this.variable.hasEffectsOnInteractionAtPath(EMPTY_PATH, NODE_INTERACTION_ACCESS, context) + this.variable.hasEffectsOnInteractionAtPath( + EMPTY_PATH, + NODE_INTERACTION_UNKNOWN_ACCESS, + context + ) ); } diff --git a/src/ast/nodes/SpreadElement.ts b/src/ast/nodes/SpreadElement.ts index 0a9c4f31505..4a863c1b785 100644 --- a/src/ast/nodes/SpreadElement.ts +++ b/src/ast/nodes/SpreadElement.ts @@ -1,7 +1,7 @@ import type { NormalizedTreeshakingOptions } from '../../rollup/types'; import type { HasEffectsContext } from '../ExecutionContext'; import type { NodeInteractionWithThisArg } from '../NodeInteractions'; -import { NODE_INTERACTION_ACCESS } from '../NodeInteractions'; +import { NODE_INTERACTION_UNKNOWN_ACCESS } from '../NodeInteractions'; import { type ObjectPath, type PathTracker, UNKNOWN_PATH, UnknownKey } from '../utils/PathTracker'; import type * as NodeType from './NodeType'; import { type ExpressionNode, NodeBase } from './shared/Node'; @@ -34,7 +34,7 @@ export default class SpreadElement extends NodeBase { (propertyReadSideEffects === 'always' || this.argument.hasEffectsOnInteractionAtPath( UNKNOWN_PATH, - NODE_INTERACTION_ACCESS, + NODE_INTERACTION_UNKNOWN_ACCESS, context ))) ); diff --git a/src/ast/nodes/shared/FunctionBase.ts b/src/ast/nodes/shared/FunctionBase.ts index 6bd2c4d0152..a1517cb7e08 100644 --- a/src/ast/nodes/shared/FunctionBase.ts +++ b/src/ast/nodes/shared/FunctionBase.ts @@ -6,9 +6,9 @@ import { type InclusionContext } from '../../ExecutionContext'; import { - INTERACTION_ACCESSED, INTERACTION_CALLED, - NO_ARGS, + NODE_INTERACTION_UNKNOWN_ACCESS, + NODE_INTERACTION_UNKNOWN_CALL, NodeInteraction, NodeInteractionCalled, NodeInteractionWithThisArg @@ -105,14 +105,14 @@ export default abstract class FunctionBase extends NodeBase { if ( returnExpression.hasEffectsOnInteractionAtPath( ['then'], - { args: NO_ARGS, thisArg: returnExpression, type: INTERACTION_CALLED, withNew: false }, + NODE_INTERACTION_UNKNOWN_CALL, context ) || (propertyReadSideEffects && (propertyReadSideEffects === 'always' || returnExpression.hasEffectsOnInteractionAtPath( ['then'], - { thisArg: returnExpression, type: INTERACTION_ACCESSED }, + NODE_INTERACTION_UNKNOWN_ACCESS, context ))) ) { diff --git a/src/ast/nodes/shared/MethodBase.ts b/src/ast/nodes/shared/MethodBase.ts index a8bdb505166..fd76f898643 100644 --- a/src/ast/nodes/shared/MethodBase.ts +++ b/src/ast/nodes/shared/MethodBase.ts @@ -5,6 +5,7 @@ import { INTERACTION_ASSIGNED, INTERACTION_CALLED, NO_ARGS, + NODE_INTERACTION_UNKNOWN_CALL, NodeInteraction, NodeInteractionCalled, NodeInteractionWithThisArg @@ -139,7 +140,7 @@ export default class MethodBase extends NodeBase implements DeoptimizableEntity this.accessedValue = UNKNOWN_EXPRESSION; return (this.accessedValue = this.value.getReturnExpressionWhenCalledAtPath( EMPTY_PATH, - { args: NO_ARGS, thisArg: null, type: INTERACTION_CALLED, withNew: false }, + NODE_INTERACTION_UNKNOWN_CALL, SHARED_RECURSION_TRACKER, this )); diff --git a/src/ast/nodes/shared/MethodTypes.ts b/src/ast/nodes/shared/MethodTypes.ts index bebc12f7376..993ebbc91c1 100644 --- a/src/ast/nodes/shared/MethodTypes.ts +++ b/src/ast/nodes/shared/MethodTypes.ts @@ -2,8 +2,8 @@ import type { HasEffectsContext } from '../../ExecutionContext'; import { INTERACTION_ACCESSED, INTERACTION_CALLED, - NO_ARGS, NODE_INTERACTION_UNKNOWN_ASSIGNMENT, + NODE_INTERACTION_UNKNOWN_CALL, NodeInteraction, NodeInteractionCalled, NodeInteractionWithThisArg @@ -84,12 +84,7 @@ export class Method extends ExpressionEntity { if ( interaction.args[argIndex]?.hasEffectsOnInteractionAtPath( EMPTY_PATH, - { - args: NO_ARGS, - thisArg: null, - type: INTERACTION_CALLED, - withNew: false - }, + NODE_INTERACTION_UNKNOWN_CALL, context ) ) { diff --git a/src/ast/values.ts b/src/ast/values.ts index c5e22d1aecc..c180143921c 100644 --- a/src/ast/values.ts +++ b/src/ast/values.ts @@ -2,7 +2,7 @@ import type { HasEffectsContext } from './ExecutionContext'; import { INTERACTION_ACCESSED, INTERACTION_CALLED, - NO_ARGS, + NODE_INTERACTION_UNKNOWN_CALL, NodeInteraction, NodeInteractionCalled } from './NodeInteractions'; @@ -153,16 +153,7 @@ const stringReplace: RawMemberDescription = { (typeof arg1.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, { deoptimizeCache() {} }) === 'symbol' && - arg1.hasEffectsOnInteractionAtPath( - EMPTY_PATH, - { - args: NO_ARGS, - thisArg: null, - type: INTERACTION_CALLED, - withNew: false - }, - context - )) + arg1.hasEffectsOnInteractionAtPath(EMPTY_PATH, NODE_INTERACTION_UNKNOWN_CALL, context)) ); }, returns: UNKNOWN_LITERAL_STRING