forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Literal.ts
109 lines (99 loc) · 2.92 KB
/
Literal.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
import type MagicString from 'magic-string';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeInteraction } from '../NodeInteractions';
import {
INTERACTION_ACCESSED,
INTERACTION_ASSIGNED,
INTERACTION_CALLED
} from '../NodeInteractions';
import type { ObjectPath } from '../utils/PathTracker';
import {
getLiteralMembersForValue,
getMemberReturnExpressionWhenCalled,
hasMemberEffectWhenCalled,
type MemberDescription
} from '../values';
import type * as NodeType from './NodeType';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UNKNOWN_RETURN_EXPRESSION,
UnknownValue
} from './shared/Expression';
import { type GenericEsTreeNode, NodeBase } from './shared/Node';
export type LiteralValue = string | boolean | null | number | RegExp | undefined;
export type LiteralValueOrBigInt = LiteralValue | bigint;
export default class Literal<
T extends LiteralValueOrBigInt = LiteralValueOrBigInt
> extends NodeBase {
declare bigint?: string;
declare raw?: string;
declare regex?: {
flags: string;
pattern: string;
};
declare type: NodeType.tLiteral;
declare value: T;
private declare members: Record<string, MemberDescription>;
deoptimizeArgumentsOnInteractionAtPath(): void {}
getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (
path.length > 0 ||
// unknown literals can also be null but do not start with an "n"
(this.value === null && this.scope.context.code.charCodeAt(this.start) !== 110) ||
typeof this.value === 'bigint' ||
// to support shims for regular expressions
this.scope.context.code.charCodeAt(this.start) === 47
) {
return UnknownValue;
}
return this.value;
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath
): [expression: ExpressionEntity, isPure: boolean] {
if (path.length !== 1) return UNKNOWN_RETURN_EXPRESSION;
return getMemberReturnExpressionWhenCalled(this.members, path[0]);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
switch (interaction.type) {
case INTERACTION_ACCESSED: {
return path.length > (this.value === null ? 0 : 1);
}
case INTERACTION_ASSIGNED: {
return true;
}
case INTERACTION_CALLED: {
if (
this.included &&
this.value instanceof RegExp &&
(this.value.global || this.value.sticky)
) {
return true;
}
return (
path.length !== 1 ||
hasMemberEffectWhenCalled(this.members, path[0], interaction, context)
);
}
}
}
initialise(): void {
super.initialise();
this.members = getLiteralMembersForValue(this.value);
}
parseNode(esTreeNode: GenericEsTreeNode): this {
this.value = esTreeNode.value;
this.regex = esTreeNode.regex;
return super.parseNode(esTreeNode);
}
render(code: MagicString): void {
if (typeof this.value === 'string') {
(code.indentExclusionRanges as [number, number][]).push([this.start + 1, this.end - 1]);
}
}
}