1 |
|
2 |
|
3 | import {IdentifierRole, isDeclaration, isObjectShorthandDeclaration} from "../parser/tokenizer";
|
4 | import {ContextualKeyword} from "../parser/tokenizer/keywords";
|
5 | import {TokenType as tt} from "../parser/tokenizer/types";
|
6 |
|
7 | import elideImportEquals from "../util/elideImportEquals";
|
8 | import getDeclarationInfo, {
|
9 |
|
10 | EMPTY_DECLARATION_INFO,
|
11 | } from "../util/getDeclarationInfo";
|
12 | import shouldElideDefaultExport from "../util/shouldElideDefaultExport";
|
13 |
|
14 |
|
15 | import Transformer from "./Transformer";
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export default class CJSImportTransformer extends Transformer {
|
21 | __init() {this.hadExport = false}
|
22 | __init2() {this.hadNamedExport = false}
|
23 | __init3() {this.hadDefaultExport = false}
|
24 |
|
25 |
|
26 | constructor(
|
27 | rootTransformer,
|
28 | tokens,
|
29 | importProcessor,
|
30 | nameManager,
|
31 | reactHotLoaderTransformer,
|
32 | enableLegacyBabel5ModuleInterop,
|
33 | isTypeScriptTransformEnabled,
|
34 | ) {
|
35 | super();this.rootTransformer = rootTransformer;this.tokens = tokens;this.importProcessor = importProcessor;this.nameManager = nameManager;this.reactHotLoaderTransformer = reactHotLoaderTransformer;this.enableLegacyBabel5ModuleInterop = enableLegacyBabel5ModuleInterop;this.isTypeScriptTransformEnabled = isTypeScriptTransformEnabled;CJSImportTransformer.prototype.__init.call(this);CJSImportTransformer.prototype.__init2.call(this);CJSImportTransformer.prototype.__init3.call(this);;
|
36 | this.declarationInfo = isTypeScriptTransformEnabled
|
37 | ? getDeclarationInfo(tokens)
|
38 | : EMPTY_DECLARATION_INFO;
|
39 | }
|
40 |
|
41 | getPrefixCode() {
|
42 | let prefix = "";
|
43 | if (this.hadExport) {
|
44 | prefix += 'Object.defineProperty(exports, "__esModule", {value: true});';
|
45 | }
|
46 | return prefix;
|
47 | }
|
48 |
|
49 | getSuffixCode() {
|
50 | if (this.enableLegacyBabel5ModuleInterop && this.hadDefaultExport && !this.hadNamedExport) {
|
51 | return "\nmodule.exports = exports.default;\n";
|
52 | }
|
53 | return "";
|
54 | }
|
55 |
|
56 | process() {
|
57 |
|
58 | if (this.tokens.matches3(tt._import, tt.name, tt.eq)) {
|
59 | return this.processImportEquals();
|
60 | }
|
61 | if (this.tokens.matches1(tt._import)) {
|
62 | this.processImport();
|
63 | return true;
|
64 | }
|
65 | if (this.tokens.matches2(tt._export, tt.eq)) {
|
66 | this.tokens.replaceToken("module.exports");
|
67 | return true;
|
68 | }
|
69 | if (this.tokens.matches1(tt._export) && !this.tokens.currentToken().isType) {
|
70 | this.hadExport = true;
|
71 | return this.processExport();
|
72 | }
|
73 | if (this.tokens.matches2(tt.name, tt.postIncDec)) {
|
74 |
|
75 | if (this.processPostIncDec()) {
|
76 | return true;
|
77 | }
|
78 | }
|
79 | if (this.tokens.matches1(tt.name) || this.tokens.matches1(tt.jsxName)) {
|
80 | return this.processIdentifier();
|
81 | }
|
82 | if (this.tokens.matches1(tt.eq)) {
|
83 | return this.processAssignment();
|
84 | }
|
85 | if (this.tokens.matches1(tt.assign)) {
|
86 | return this.processComplexAssignment();
|
87 | }
|
88 | if (this.tokens.matches1(tt.preIncDec)) {
|
89 | return this.processPreIncDec();
|
90 | }
|
91 | return false;
|
92 | }
|
93 |
|
94 | processImportEquals() {
|
95 | const importName = this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 1);
|
96 | if (this.importProcessor.isTypeName(importName)) {
|
97 |
|
98 | elideImportEquals(this.tokens);
|
99 | } else {
|
100 |
|
101 | this.tokens.replaceToken("const");
|
102 | }
|
103 | return true;
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | processImport() {
|
116 | if (this.tokens.matches2(tt._import, tt.parenL)) {
|
117 | this.tokens.replaceToken("Promise.resolve().then(() => require");
|
118 | const contextId = this.tokens.currentToken().contextId;
|
119 | if (contextId == null) {
|
120 | throw new Error("Expected context ID on dynamic import invocation.");
|
121 | }
|
122 | this.tokens.copyToken();
|
123 | while (!this.tokens.matchesContextIdAndLabel(tt.parenR, contextId)) {
|
124 | this.rootTransformer.processToken();
|
125 | }
|
126 | this.tokens.replaceToken("))");
|
127 | return;
|
128 | }
|
129 |
|
130 | const wasOnlyTypes = this.removeImportAndDetectIfType();
|
131 |
|
132 | if (wasOnlyTypes) {
|
133 | this.tokens.removeToken();
|
134 | } else {
|
135 | const path = this.tokens.stringValue();
|
136 | this.tokens.replaceTokenTrimmingLeftWhitespace(this.importProcessor.claimImportCode(path));
|
137 | this.tokens.appendCode(this.importProcessor.claimImportCode(path));
|
138 | }
|
139 | if (this.tokens.matches1(tt.semi)) {
|
140 | this.tokens.removeToken();
|
141 | }
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | removeImportAndDetectIfType() {
|
151 | this.tokens.removeInitialToken();
|
152 | if (
|
153 | this.tokens.matchesContextual(ContextualKeyword._type) &&
|
154 | !this.tokens.matches1AtIndex(this.tokens.currentIndex() + 1, tt.comma) &&
|
155 | !this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, ContextualKeyword._from)
|
156 | ) {
|
157 |
|
158 | this.removeRemainingImport();
|
159 | return true;
|
160 | }
|
161 |
|
162 | if (this.tokens.matches1(tt.name) || this.tokens.matches1(tt.star)) {
|
163 |
|
164 |
|
165 | this.removeRemainingImport();
|
166 | return false;
|
167 | }
|
168 |
|
169 | if (this.tokens.matches1(tt.string)) {
|
170 |
|
171 | return false;
|
172 | }
|
173 |
|
174 | let foundNonType = false;
|
175 | while (!this.tokens.matches1(tt.string)) {
|
176 |
|
177 |
|
178 | if ((!foundNonType && this.tokens.matches1(tt.braceL)) || this.tokens.matches1(tt.comma)) {
|
179 | this.tokens.removeToken();
|
180 | if (
|
181 | this.tokens.matches2(tt.name, tt.comma) ||
|
182 | this.tokens.matches2(tt.name, tt.braceR) ||
|
183 | this.tokens.matches4(tt.name, tt.name, tt.name, tt.comma) ||
|
184 | this.tokens.matches4(tt.name, tt.name, tt.name, tt.braceR)
|
185 | ) {
|
186 | foundNonType = true;
|
187 | }
|
188 | }
|
189 | this.tokens.removeToken();
|
190 | }
|
191 | return !foundNonType;
|
192 | }
|
193 |
|
194 | removeRemainingImport() {
|
195 | while (!this.tokens.matches1(tt.string)) {
|
196 | this.tokens.removeToken();
|
197 | }
|
198 | }
|
199 |
|
200 | processIdentifier() {
|
201 | const token = this.tokens.currentToken();
|
202 | if (token.shadowsGlobal) {
|
203 | return false;
|
204 | }
|
205 |
|
206 | if (token.identifierRole === IdentifierRole.ObjectShorthand) {
|
207 | return this.processObjectShorthand();
|
208 | }
|
209 |
|
210 | if (token.identifierRole !== IdentifierRole.Access) {
|
211 | return false;
|
212 | }
|
213 | const replacement = this.importProcessor.getIdentifierReplacement(
|
214 | this.tokens.identifierNameForToken(token),
|
215 | );
|
216 | if (!replacement) {
|
217 | return false;
|
218 | }
|
219 |
|
220 |
|
221 | let possibleOpenParenIndex = this.tokens.currentIndex() + 1;
|
222 | while (
|
223 | possibleOpenParenIndex < this.tokens.tokens.length &&
|
224 | this.tokens.tokens[possibleOpenParenIndex].type === tt.parenR
|
225 | ) {
|
226 | possibleOpenParenIndex++;
|
227 | }
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | if (this.tokens.tokens[possibleOpenParenIndex].type === tt.parenL) {
|
233 | if (
|
234 | this.tokens.tokenAtRelativeIndex(1).type === tt.parenL &&
|
235 | this.tokens.tokenAtRelativeIndex(-1).type !== tt._new
|
236 | ) {
|
237 | this.tokens.replaceToken(`${replacement}.call(void 0, `);
|
238 |
|
239 | this.tokens.removeToken();
|
240 |
|
241 | this.rootTransformer.processBalancedCode();
|
242 | this.tokens.copyExpectedToken(tt.parenR);
|
243 | } else {
|
244 |
|
245 | this.tokens.replaceToken(`(0, ${replacement})`);
|
246 | }
|
247 | } else {
|
248 | this.tokens.replaceToken(replacement);
|
249 | }
|
250 | return true;
|
251 | }
|
252 |
|
253 | processObjectShorthand() {
|
254 | const identifier = this.tokens.identifierName();
|
255 | const replacement = this.importProcessor.getIdentifierReplacement(identifier);
|
256 | if (!replacement) {
|
257 | return false;
|
258 | }
|
259 | this.tokens.replaceToken(`${identifier}: ${replacement}`);
|
260 | return true;
|
261 | }
|
262 |
|
263 | processExport() {
|
264 | if (
|
265 | this.tokens.matches2(tt._export, tt._enum) ||
|
266 | this.tokens.matches3(tt._export, tt._const, tt._enum)
|
267 | ) {
|
268 |
|
269 | return false;
|
270 | }
|
271 | if (this.tokens.matches2(tt._export, tt._default)) {
|
272 | this.processExportDefault();
|
273 | this.hadDefaultExport = true;
|
274 | return true;
|
275 | }
|
276 | this.hadNamedExport = true;
|
277 | if (
|
278 | this.tokens.matches2(tt._export, tt._var) ||
|
279 | this.tokens.matches2(tt._export, tt._let) ||
|
280 | this.tokens.matches2(tt._export, tt._const)
|
281 | ) {
|
282 | this.processExportVar();
|
283 | return true;
|
284 | } else if (
|
285 | this.tokens.matches2(tt._export, tt._function) ||
|
286 |
|
287 | this.tokens.matches3(tt._export, tt.name, tt._function)
|
288 | ) {
|
289 | this.processExportFunction();
|
290 | return true;
|
291 | } else if (
|
292 | this.tokens.matches2(tt._export, tt._class) ||
|
293 | this.tokens.matches3(tt._export, tt._abstract, tt._class)
|
294 | ) {
|
295 | this.processExportClass();
|
296 | return true;
|
297 | } else if (this.tokens.matches2(tt._export, tt.braceL)) {
|
298 | this.processExportBindings();
|
299 | return true;
|
300 | } else if (this.tokens.matches2(tt._export, tt.star)) {
|
301 | this.processExportStar();
|
302 | return true;
|
303 | } else if (
|
304 | this.tokens.matches3(tt._export, tt.name, tt.braceL) &&
|
305 | this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, ContextualKeyword._type)
|
306 | ) {
|
307 |
|
308 | this.tokens.removeInitialToken();
|
309 | while (!this.tokens.matches1(tt.braceR)) {
|
310 | this.tokens.removeToken();
|
311 | }
|
312 | this.tokens.removeToken();
|
313 |
|
314 |
|
315 | if (
|
316 | this.tokens.matchesContextual(ContextualKeyword._from) &&
|
317 | this.tokens.matches1AtIndex(this.tokens.currentIndex() + 1, tt.string)
|
318 | ) {
|
319 | this.tokens.removeToken();
|
320 | this.tokens.removeToken();
|
321 | }
|
322 | return true;
|
323 | } else {
|
324 | throw new Error("Unrecognized export syntax.");
|
325 | }
|
326 | }
|
327 |
|
328 | processAssignment() {
|
329 | const index = this.tokens.currentIndex();
|
330 | const identifierToken = this.tokens.tokens[index - 1];
|
331 |
|
332 |
|
333 | if (identifierToken.isType || identifierToken.type !== tt.name) {
|
334 | return false;
|
335 | }
|
336 | if (identifierToken.shadowsGlobal) {
|
337 | return false;
|
338 | }
|
339 | if (index >= 2 && this.tokens.matches1AtIndex(index - 2, tt.dot)) {
|
340 | return false;
|
341 | }
|
342 | if (index >= 2 && [tt._var, tt._let, tt._const].includes(this.tokens.tokens[index - 2].type)) {
|
343 |
|
344 |
|
345 |
|
346 | return false;
|
347 | }
|
348 | const assignmentSnippet = this.importProcessor.resolveExportBinding(
|
349 | this.tokens.identifierNameForToken(identifierToken),
|
350 | );
|
351 | if (!assignmentSnippet) {
|
352 | return false;
|
353 | }
|
354 | this.tokens.copyToken();
|
355 | this.tokens.appendCode(` ${assignmentSnippet} =`);
|
356 | return true;
|
357 | }
|
358 |
|
359 | |
360 |
|
361 |
|
362 | processComplexAssignment() {
|
363 | const index = this.tokens.currentIndex();
|
364 | const identifierToken = this.tokens.tokens[index - 1];
|
365 | if (identifierToken.type !== tt.name) {
|
366 | return false;
|
367 | }
|
368 | if (identifierToken.shadowsGlobal) {
|
369 | return false;
|
370 | }
|
371 | if (index >= 2 && this.tokens.matches1AtIndex(index - 2, tt.dot)) {
|
372 | return false;
|
373 | }
|
374 | const assignmentSnippet = this.importProcessor.resolveExportBinding(
|
375 | this.tokens.identifierNameForToken(identifierToken),
|
376 | );
|
377 | if (!assignmentSnippet) {
|
378 | return false;
|
379 | }
|
380 | this.tokens.appendCode(` = ${assignmentSnippet}`);
|
381 | this.tokens.copyToken();
|
382 | return true;
|
383 | }
|
384 |
|
385 | |
386 |
|
387 |
|
388 | processPreIncDec() {
|
389 | const index = this.tokens.currentIndex();
|
390 | const identifierToken = this.tokens.tokens[index + 1];
|
391 | if (identifierToken.type !== tt.name) {
|
392 | return false;
|
393 | }
|
394 | if (identifierToken.shadowsGlobal) {
|
395 | return false;
|
396 | }
|
397 |
|
398 | if (
|
399 | index + 2 < this.tokens.tokens.length &&
|
400 | (this.tokens.matches1AtIndex(index + 2, tt.dot) ||
|
401 | this.tokens.matches1AtIndex(index + 2, tt.bracketL) ||
|
402 | this.tokens.matches1AtIndex(index + 2, tt.parenL))
|
403 | ) {
|
404 | return false;
|
405 | }
|
406 | const identifierName = this.tokens.identifierNameForToken(identifierToken);
|
407 | const assignmentSnippet = this.importProcessor.resolveExportBinding(identifierName);
|
408 | if (!assignmentSnippet) {
|
409 | return false;
|
410 | }
|
411 | this.tokens.appendCode(`${assignmentSnippet} = `);
|
412 | this.tokens.copyToken();
|
413 | return true;
|
414 | }
|
415 |
|
416 | |
417 |
|
418 |
|
419 |
|
420 | processPostIncDec() {
|
421 | const index = this.tokens.currentIndex();
|
422 | const identifierToken = this.tokens.tokens[index];
|
423 | const operatorToken = this.tokens.tokens[index + 1];
|
424 | if (identifierToken.type !== tt.name) {
|
425 | return false;
|
426 | }
|
427 | if (identifierToken.shadowsGlobal) {
|
428 | return false;
|
429 | }
|
430 | if (index >= 1 && this.tokens.matches1AtIndex(index - 1, tt.dot)) {
|
431 | return false;
|
432 | }
|
433 | const identifierName = this.tokens.identifierNameForToken(identifierToken);
|
434 | const assignmentSnippet = this.importProcessor.resolveExportBinding(identifierName);
|
435 | if (!assignmentSnippet) {
|
436 | return false;
|
437 | }
|
438 | const operatorCode = this.tokens.rawCodeForToken(operatorToken);
|
439 |
|
440 |
|
441 | const base = this.importProcessor.getIdentifierReplacement(identifierName) || identifierName;
|
442 | if (operatorCode === "++") {
|
443 | this.tokens.replaceToken(`(${base} = ${assignmentSnippet} = ${base} + 1, ${base} - 1)`);
|
444 | } else if (operatorCode === "--") {
|
445 | this.tokens.replaceToken(`(${base} = ${assignmentSnippet} = ${base} - 1, ${base} + 1)`);
|
446 | } else {
|
447 | throw new Error(`Unexpected operator: ${operatorCode}`);
|
448 | }
|
449 | this.tokens.removeToken();
|
450 | return true;
|
451 | }
|
452 |
|
453 | processExportDefault() {
|
454 | if (
|
455 | this.tokens.matches4(tt._export, tt._default, tt._function, tt.name) ||
|
456 |
|
457 | this.tokens.matches5(tt._export, tt._default, tt.name, tt._function, tt.name)
|
458 | ) {
|
459 | this.tokens.removeInitialToken();
|
460 | this.tokens.removeToken();
|
461 |
|
462 |
|
463 | const name = this.processNamedFunction();
|
464 | this.tokens.appendCode(` exports.default = ${name};`);
|
465 | } else if (
|
466 | this.tokens.matches4(tt._export, tt._default, tt._class, tt.name) ||
|
467 | this.tokens.matches5(tt._export, tt._default, tt._abstract, tt._class, tt.name)
|
468 | ) {
|
469 | this.tokens.removeInitialToken();
|
470 | this.tokens.removeToken();
|
471 | if (this.tokens.matches1(tt._abstract)) {
|
472 | this.tokens.removeToken();
|
473 | }
|
474 | const name = this.rootTransformer.processNamedClass();
|
475 | this.tokens.appendCode(` exports.default = ${name};`);
|
476 | } else if (this.tokens.matches3(tt._export, tt._default, tt.at)) {
|
477 | throw new Error("Export default statements with decorators are not yet supported.");
|
478 |
|
479 | } else if (
|
480 | shouldElideDefaultExport(this.isTypeScriptTransformEnabled, this.tokens, this.declarationInfo)
|
481 | ) {
|
482 |
|
483 |
|
484 |
|
485 | this.tokens.removeInitialToken();
|
486 | this.tokens.removeToken();
|
487 | this.tokens.removeToken();
|
488 | } else if (this.reactHotLoaderTransformer) {
|
489 |
|
490 |
|
491 | const defaultVarName = this.nameManager.claimFreeName("_default");
|
492 | this.tokens.replaceToken(`let ${defaultVarName}; exports.`);
|
493 | this.tokens.copyToken();
|
494 | this.tokens.appendCode(` = ${defaultVarName} =`);
|
495 | this.reactHotLoaderTransformer.setExtractedDefaultExportName(defaultVarName);
|
496 | } else {
|
497 |
|
498 | this.tokens.replaceToken("exports.");
|
499 | this.tokens.copyToken();
|
500 | this.tokens.appendCode(" =");
|
501 | }
|
502 | }
|
503 |
|
504 | |
505 |
|
506 |
|
507 | processExportVar() {
|
508 | if (this.isSimpleExportVar()) {
|
509 | this.processSimpleExportVar();
|
510 | } else {
|
511 | this.processComplexExportVar();
|
512 | }
|
513 | }
|
514 |
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 | isSimpleExportVar() {
|
521 | let tokenIndex = this.tokens.currentIndex();
|
522 |
|
523 | tokenIndex++;
|
524 |
|
525 | tokenIndex++;
|
526 | if (!this.tokens.matches1AtIndex(tokenIndex, tt.name)) {
|
527 | return false;
|
528 | }
|
529 | tokenIndex++;
|
530 | while (tokenIndex < this.tokens.tokens.length && this.tokens.tokens[tokenIndex].isType) {
|
531 | tokenIndex++;
|
532 | }
|
533 | if (!this.tokens.matches1AtIndex(tokenIndex, tt.eq)) {
|
534 | return false;
|
535 | }
|
536 | return true;
|
537 | }
|
538 |
|
539 | |
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 | processSimpleExportVar() {
|
553 |
|
554 | this.tokens.removeInitialToken();
|
555 |
|
556 | this.tokens.copyToken();
|
557 | const varName = this.tokens.identifierName();
|
558 |
|
559 | while (!this.tokens.matches1(tt.eq)) {
|
560 | this.rootTransformer.processToken();
|
561 | }
|
562 | const endIndex = this.tokens.currentToken().rhsEndIndex;
|
563 | if (endIndex == null) {
|
564 | throw new Error("Expected = token with an end index.");
|
565 | }
|
566 | while (this.tokens.currentIndex() < endIndex) {
|
567 | this.rootTransformer.processToken();
|
568 | }
|
569 | this.tokens.appendCode(`; exports.${varName} = ${varName}`);
|
570 | }
|
571 |
|
572 | |
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 | processComplexExportVar() {
|
580 | this.tokens.removeInitialToken();
|
581 | this.tokens.removeToken();
|
582 | const needsParens = this.tokens.matches1(tt.braceL);
|
583 | if (needsParens) {
|
584 | this.tokens.appendCode("(");
|
585 | }
|
586 |
|
587 | let depth = 0;
|
588 | while (true) {
|
589 | if (
|
590 | this.tokens.matches1(tt.braceL) ||
|
591 | this.tokens.matches1(tt.dollarBraceL) ||
|
592 | this.tokens.matches1(tt.bracketL)
|
593 | ) {
|
594 | depth++;
|
595 | this.tokens.copyToken();
|
596 | } else if (this.tokens.matches1(tt.braceR) || this.tokens.matches1(tt.bracketR)) {
|
597 | depth--;
|
598 | this.tokens.copyToken();
|
599 | } else if (
|
600 | depth === 0 &&
|
601 | !this.tokens.matches1(tt.name) &&
|
602 | !this.tokens.currentToken().isType
|
603 | ) {
|
604 | break;
|
605 | } else if (this.tokens.matches1(tt.eq)) {
|
606 |
|
607 |
|
608 | const endIndex = this.tokens.currentToken().rhsEndIndex;
|
609 | if (endIndex == null) {
|
610 | throw new Error("Expected = token with an end index.");
|
611 | }
|
612 | while (this.tokens.currentIndex() < endIndex) {
|
613 | this.rootTransformer.processToken();
|
614 | }
|
615 | } else {
|
616 | const token = this.tokens.currentToken();
|
617 | if (isDeclaration(token)) {
|
618 | const name = this.tokens.identifierName();
|
619 | let replacement = this.importProcessor.getIdentifierReplacement(name);
|
620 | if (replacement === null) {
|
621 | throw new Error(`Expected a replacement for ${name} in \`export var\` syntax.`);
|
622 | }
|
623 | if (isObjectShorthandDeclaration(token)) {
|
624 | replacement = `${name}: ${replacement}`;
|
625 | }
|
626 | this.tokens.replaceToken(replacement);
|
627 | } else {
|
628 | this.rootTransformer.processToken();
|
629 | }
|
630 | }
|
631 | }
|
632 |
|
633 | if (needsParens) {
|
634 |
|
635 | const endIndex = this.tokens.currentToken().rhsEndIndex;
|
636 | if (endIndex == null) {
|
637 | throw new Error("Expected = token with an end index.");
|
638 | }
|
639 | while (this.tokens.currentIndex() < endIndex) {
|
640 | this.rootTransformer.processToken();
|
641 | }
|
642 | this.tokens.appendCode(")");
|
643 | }
|
644 | }
|
645 |
|
646 | |
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | processExportFunction() {
|
653 | this.tokens.replaceToken("");
|
654 | const name = this.processNamedFunction();
|
655 | this.tokens.appendCode(` exports.${name} = ${name};`);
|
656 | }
|
657 |
|
658 | |
659 |
|
660 |
|
661 | processNamedFunction() {
|
662 | if (this.tokens.matches1(tt._function)) {
|
663 | this.tokens.copyToken();
|
664 | } else if (this.tokens.matches2(tt.name, tt._function)) {
|
665 | if (!this.tokens.matchesContextual(ContextualKeyword._async)) {
|
666 | throw new Error("Expected async keyword in function export.");
|
667 | }
|
668 | this.tokens.copyToken();
|
669 | this.tokens.copyToken();
|
670 | }
|
671 | if (this.tokens.matches1(tt.star)) {
|
672 | this.tokens.copyToken();
|
673 | }
|
674 | if (!this.tokens.matches1(tt.name)) {
|
675 | throw new Error("Expected identifier for exported function name.");
|
676 | }
|
677 | const name = this.tokens.identifierName();
|
678 | this.tokens.copyToken();
|
679 | if (this.tokens.currentToken().isType) {
|
680 | this.tokens.removeInitialToken();
|
681 | while (this.tokens.currentToken().isType) {
|
682 | this.tokens.removeToken();
|
683 | }
|
684 | }
|
685 | this.tokens.copyExpectedToken(tt.parenL);
|
686 | this.rootTransformer.processBalancedCode();
|
687 | this.tokens.copyExpectedToken(tt.parenR);
|
688 | this.rootTransformer.processPossibleTypeRange();
|
689 | this.tokens.copyExpectedToken(tt.braceL);
|
690 | this.rootTransformer.processBalancedCode();
|
691 | this.tokens.copyExpectedToken(tt.braceR);
|
692 | return name;
|
693 | }
|
694 |
|
695 | |
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 | processExportClass() {
|
702 | this.tokens.removeInitialToken();
|
703 | if (this.tokens.matches1(tt._abstract)) {
|
704 | this.tokens.removeToken();
|
705 | }
|
706 | const name = this.rootTransformer.processNamedClass();
|
707 | this.tokens.appendCode(` exports.${name} = ${name};`);
|
708 | }
|
709 |
|
710 | |
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 | processExportBindings() {
|
726 | this.tokens.removeInitialToken();
|
727 | this.tokens.removeToken();
|
728 |
|
729 | const exportStatements = [];
|
730 | while (true) {
|
731 | if (this.tokens.matches1(tt.braceR)) {
|
732 | this.tokens.removeToken();
|
733 | break;
|
734 | }
|
735 |
|
736 | const localName = this.tokens.identifierName();
|
737 | let exportedName;
|
738 | this.tokens.removeToken();
|
739 | if (this.tokens.matchesContextual(ContextualKeyword._as)) {
|
740 | this.tokens.removeToken();
|
741 | exportedName = this.tokens.identifierName();
|
742 | this.tokens.removeToken();
|
743 | } else {
|
744 | exportedName = localName;
|
745 | }
|
746 | if (!this.shouldElideExportedIdentifier(localName)) {
|
747 | const newLocalName = this.importProcessor.getIdentifierReplacement(localName);
|
748 | exportStatements.push(`exports.${exportedName} = ${newLocalName || localName};`);
|
749 | }
|
750 |
|
751 | if (this.tokens.matches1(tt.braceR)) {
|
752 | this.tokens.removeToken();
|
753 | break;
|
754 | }
|
755 | if (this.tokens.matches2(tt.comma, tt.braceR)) {
|
756 | this.tokens.removeToken();
|
757 | this.tokens.removeToken();
|
758 | break;
|
759 | } else if (this.tokens.matches1(tt.comma)) {
|
760 | this.tokens.removeToken();
|
761 | } else {
|
762 | throw new Error(`Unexpected token: ${JSON.stringify(this.tokens.currentToken())}`);
|
763 | }
|
764 | }
|
765 |
|
766 | if (this.tokens.matchesContextual(ContextualKeyword._from)) {
|
767 |
|
768 |
|
769 | this.tokens.removeToken();
|
770 | const path = this.tokens.stringValue();
|
771 | this.tokens.replaceTokenTrimmingLeftWhitespace(this.importProcessor.claimImportCode(path));
|
772 | } else {
|
773 |
|
774 | this.tokens.appendCode(exportStatements.join(" "));
|
775 | }
|
776 |
|
777 | if (this.tokens.matches1(tt.semi)) {
|
778 | this.tokens.removeToken();
|
779 | }
|
780 | }
|
781 |
|
782 | processExportStar() {
|
783 | this.tokens.removeInitialToken();
|
784 | while (!this.tokens.matches1(tt.string)) {
|
785 | this.tokens.removeToken();
|
786 | }
|
787 | const path = this.tokens.stringValue();
|
788 | this.tokens.replaceTokenTrimmingLeftWhitespace(this.importProcessor.claimImportCode(path));
|
789 | if (this.tokens.matches1(tt.semi)) {
|
790 | this.tokens.removeToken();
|
791 | }
|
792 | }
|
793 |
|
794 | shouldElideExportedIdentifier(name) {
|
795 | return this.isTypeScriptTransformEnabled && !this.declarationInfo.valueDeclarations.has(name);
|
796 | }
|
797 | }
|