-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ExportDefaultDeclaration.ts
170 lines (156 loc) · 5.15 KB
/
ExportDefaultDeclaration.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
NodeRenderOptions,
RenderOptions
} from '../../utils/renderHelpers';
import { treeshakeNode } from '../../utils/treeshakeNode';
import ModuleScope from '../scopes/ModuleScope';
import ExportDefaultVariable from '../variables/ExportDefaultVariable';
import ClassDeclaration, { isClassDeclaration } from './ClassDeclaration';
import FunctionDeclaration, { isFunctionDeclaration } from './FunctionDeclaration';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, NodeBase } from './shared/Node';
const WHITESPACE = /\s/;
// The header ends at the first non-white-space after "default"
function getDeclarationStart(code: string, start = 0) {
start = findFirstOccurrenceOutsideComment(code, 'default', start) + 7;
while (WHITESPACE.test(code[start])) start++;
return start;
}
function getIdInsertPosition(code: string, declarationKeyword: string, start = 0) {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '{', declarationEnd));
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
}
return declarationEnd + generatorStarPos + 1;
}
export function isExportDefaultDeclaration(node: Node): node is ExportDefaultDeclaration {
return node.type === NodeType.ExportDefaultDeclaration;
}
export default class ExportDefaultDeclaration extends NodeBase {
declaration: FunctionDeclaration | ClassDeclaration | ExpressionNode;
needsBoundaries: true;
scope: ModuleScope;
type: NodeType.tExportDefaultDeclaration;
variable: ExportDefaultVariable;
private declarationName: string;
include(includeAllChildrenRecursively: boolean) {
super.include(includeAllChildrenRecursively);
if (includeAllChildrenRecursively) {
this.context.includeVariable(this.variable);
}
}
initialise() {
this.included = false;
this.declarationName =
((<FunctionDeclaration | ClassDeclaration>this.declaration).id &&
(<FunctionDeclaration | ClassDeclaration>this.declaration).id.name) ||
(<Identifier>this.declaration).name;
this.variable = this.scope.addExportDefaultDeclaration(
this.declarationName || this.context.getModuleName(),
this,
this.context
);
this.context.addExport(this);
}
render(code: MagicString, options: RenderOptions, { start, end }: NodeRenderOptions = BLANK) {
const declarationStart = getDeclarationStart(code.original, this.start);
if (isFunctionDeclaration(this.declaration)) {
this.renderNamedDeclaration(
code,
declarationStart,
'function',
this.declaration.id === null,
options
);
} else if (isClassDeclaration(this.declaration)) {
this.renderNamedDeclaration(
code,
declarationStart,
'class',
this.declaration.id === null,
options
);
} else if (this.variable.referencesOriginal()) {
// Remove altogether to prevent re-declaring the same variable
if (options.format === 'system' && this.variable.exportName) {
code.overwrite(
start,
end,
`exports('${this.variable.exportName}', ${this.variable.getName()});`
);
} else {
treeshakeNode(this, code, start, end);
}
return;
} else if (this.variable.included) {
this.renderVariableDeclaration(code, declarationStart, options);
} else {
code.remove(this.start, declarationStart);
this.declaration.render(code, options, {
isCalleeOfRenderedParent: false,
renderedParentType: NodeType.ExpressionStatement
});
if (code.original[this.end - 1] !== ';') {
code.appendLeft(this.end, ';');
}
return;
}
this.declaration.render(code, options);
}
private renderNamedDeclaration(
code: MagicString,
declarationStart: number,
declarationKeyword: string,
needsId: boolean,
options: RenderOptions
) {
const name = this.variable.getName();
// Remove `export default`
code.remove(this.start, declarationStart);
if (needsId) {
code.appendLeft(
getIdInsertPosition(code.original, declarationKeyword, declarationStart),
` ${name}`
);
}
if (
options.format === 'system' &&
isClassDeclaration(this.declaration) &&
this.variable.exportName
) {
code.appendLeft(this.end, ` exports('${this.variable.exportName}', ${name});`);
}
}
private renderVariableDeclaration(
code: MagicString,
declarationStart: number,
options: RenderOptions
) {
const systemBinding =
options.format === 'system' && this.variable.exportName
? `exports('${this.variable.exportName}', `
: '';
code.overwrite(
this.start,
declarationStart,
`${options.varOrConst} ${this.variable.getName()} = ${systemBinding}`
);
const hasTrailingSemicolon = code.original.charCodeAt(this.end - 1) === 59; /*";"*/
if (systemBinding) {
code.appendRight(
hasTrailingSemicolon ? this.end - 1 : this.end,
')' + (hasTrailingSemicolon ? '' : ';')
);
} else if (!hasTrailingSemicolon) {
code.appendLeft(this.end, ';');
}
}
}
ExportDefaultDeclaration.prototype.needsBoundaries = true;