-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ObjectExpression.ts
154 lines (143 loc) · 4.34 KB
/
ObjectExpression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import type { CallOptions } from '../CallOptions';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeEvent } from '../NodeEvents';
import {
EMPTY_PATH,
type ObjectPath,
type PathTracker,
SHARED_RECURSION_TRACKER,
UnknownKey
} from '../utils/PathTracker';
import Identifier from './Identifier';
import Literal from './Literal';
import * as NodeType from './NodeType';
import type Property from './Property';
import SpreadElement from './SpreadElement';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UnknownValue
} from './shared/Expression';
import { NodeBase } from './shared/Node';
import { ObjectEntity, type ObjectProperty } from './shared/ObjectEntity';
import { OBJECT_PROTOTYPE } from './shared/ObjectPrototype';
export default class ObjectExpression extends NodeBase implements DeoptimizableEntity {
declare properties: readonly (Property | SpreadElement)[];
declare type: NodeType.tObjectExpression;
private objectEntity: ObjectEntity | null = null;
deoptimizeCache(): void {
this.getObjectEntity().deoptimizeAllProperties();
}
deoptimizePath(path: ObjectPath): void {
this.getObjectEntity().deoptimizePath(path);
}
deoptimizeThisOnEventAtPath(
event: NodeEvent,
path: ObjectPath,
thisParameter: ExpressionEntity,
recursionTracker: PathTracker
): void {
this.getObjectEntity().deoptimizeThisOnEventAtPath(
event,
path,
thisParameter,
recursionTracker
);
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): ExpressionEntity {
return this.getObjectEntity().getReturnExpressionWhenCalledAtPath(
path,
callOptions,
recursionTracker,
origin
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context);
}
hasEffectsWhenAssignedAtPath(
path: ObjectPath,
context: HasEffectsContext,
ignoreAccessors: boolean
): boolean {
return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context, ignoreAccessors);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
): boolean {
return this.getObjectEntity().hasEffectsWhenCalledAtPath(path, callOptions, context);
}
render(
code: MagicString,
options: RenderOptions,
{ renderedSurroundingElement }: NodeRenderOptions = BLANK
): void {
super.render(code, options);
if (
renderedSurroundingElement === NodeType.ExpressionStatement ||
renderedSurroundingElement === NodeType.ArrowFunctionExpression
) {
code.appendRight(this.start, '(');
code.prependLeft(this.end, ')');
}
}
private getObjectEntity(): ObjectEntity {
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) {
properties.push({ key: UnknownKey, kind: 'init', property });
continue;
}
let key: string;
if (property.computed) {
const keyValue = property.key.getLiteralValueAtPath(
EMPTY_PATH,
SHARED_RECURSION_TRACKER,
this
);
if (keyValue === UnknownValue) {
properties.push({ key: UnknownKey, kind: property.kind, property });
continue;
} else {
key = String(keyValue);
}
} else {
key =
property.key instanceof Identifier
? property.key.name
: String((property.key as Literal).value);
if (key === '__proto__' && property.kind === 'init') {
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, prototype));
}
}