forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Property.ts
72 lines (64 loc) · 2.46 KB
/
Property.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
import type MagicString from 'magic-string';
import type { NormalizedTreeshakingOptions } from '../../rollup/types';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { HasEffectsContext } from '../ExecutionContext';
import { UnknownKey } from '../utils/PathTracker';
import type LocalVariable from '../variables/LocalVariable';
import type * as NodeType from './NodeType';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import { type ExpressionEntity, UNKNOWN_EXPRESSION } from './shared/Expression';
import MethodBase from './shared/MethodBase';
import type { ExpressionNode } from './shared/Node';
import type { PatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export default class Property extends MethodBase implements PatternNode {
declare key: ExpressionNode;
declare kind: 'init' | 'get' | 'set';
declare type: NodeType.tProperty;
private declarationInit: ExpressionEntity | null = null;
//declare method: boolean;
get method(): boolean {
return isFlagSet(this.flags, Flag.method);
}
set method(value: boolean) {
this.flags = setFlag(this.flags, Flag.method, value);
}
//declare shorthand: boolean;
get shorthand(): boolean {
return isFlagSet(this.flags, Flag.shorthand);
}
set shorthand(value: boolean) {
this.flags = setFlag(this.flags, Flag.shorthand, value);
}
declare(kind: VariableKind, init: ExpressionEntity): LocalVariable[] {
this.declarationInit = init;
return (this.value as PatternNode).declare(kind, UNKNOWN_EXPRESSION);
}
hasEffects(context: HasEffectsContext): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
const propertyReadSideEffects = (
this.scope.context.options.treeshake as NormalizedTreeshakingOptions
).propertyReadSideEffects;
return (
(this.parent.type === 'ObjectPattern' && propertyReadSideEffects === 'always') ||
this.key.hasEffects(context) ||
this.value.hasEffects(context)
);
}
markDeclarationReached(): void {
(this.value as PatternNode).markDeclarationReached();
}
render(code: MagicString, options: RenderOptions): void {
if (!this.shorthand) {
this.key.render(code, options);
}
this.value.render(code, options, { isShorthandProperty: this.shorthand });
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
if (this.declarationInit !== null) {
this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
this.scope.context.requestTreeshakingPass();
}
}
}