-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
VariableDeclarator.ts
74 lines (67 loc) · 2.36 KB
/
VariableDeclarator.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
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { isReassignedExportsMember } from '../../utils/reassignedExportsMember';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
type RenderOptions
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { ObjectPath } from '../utils/PathTracker';
import { UNDEFINED_EXPRESSION } from '../values';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { type ExpressionNode, type IncludeChildren, NodeBase } from './shared/Node';
import type { PatternNode } from './shared/Pattern';
export default class VariableDeclarator extends NodeBase {
declare id: PatternNode;
declare init: ExpressionNode | null;
declare type: NodeType.tVariableDeclarator;
declareDeclarator(kind: string): void {
this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
}
deoptimizePath(path: ObjectPath): void {
this.id.deoptimizePath(path);
}
hasEffects(context: HasEffectsContext): boolean {
const initEffect = this.init?.hasEffects(context);
this.id.markDeclarationReached();
return initEffect || this.id.hasEffects(context);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
this.init?.include(context, includeChildrenRecursively, {
includeWithoutParameterDefaults: true
});
this.id.markDeclarationReached();
if (includeChildrenRecursively || this.id.shouldBeIncluded(context)) {
this.id.include(context, includeChildrenRecursively);
}
}
render(code: MagicString, options: RenderOptions): void {
const {
exportNamesByVariable,
snippets: { _ }
} = options;
const renderId = this.id.included;
if (renderId) {
this.id.render(code, options);
} else {
const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', this.id.end);
code.remove(this.start, findNonWhiteSpace(code.original, operatorPos + 1));
}
if (this.init) {
this.init.render(
code,
options,
renderId ? BLANK : { renderedSurroundingElement: NodeType.ExpressionStatement }
);
} else if (
this.id instanceof Identifier &&
isReassignedExportsMember(this.id.variable!, exportNamesByVariable)
) {
code.appendLeft(this.end, `${_}=${_}void 0`);
}
}
protected applyDeoptimizations() {}
}