UNPKG

1.11 kBJavaScriptView Raw
1import {TokenType as tt} from "../parser/tokenizer/types";
2
3
4
5/**
6 * Common method sharing code between CJS and ESM cases, since they're the same here.
7 */
8export default function shouldElideDefaultExport(
9 isTypeScriptTransformEnabled,
10 tokens,
11 declarationInfo,
12) {
13 if (!isTypeScriptTransformEnabled) {
14 return false;
15 }
16 const exportToken = tokens.currentToken();
17 if (exportToken.rhsEndIndex == null) {
18 throw new Error("Expected non-null rhsEndIndex on export token.");
19 }
20 // The export must be of the form `export default a` or `export default a;`.
21 const numTokens = exportToken.rhsEndIndex - tokens.currentIndex();
22 if (
23 numTokens !== 3 &&
24 !(numTokens === 4 && tokens.matches1AtIndex(exportToken.rhsEndIndex - 1, tt.semi))
25 ) {
26 return false;
27 }
28 const identifierToken = tokens.tokenAtRelativeIndex(2);
29 if (identifierToken.type !== tt.name) {
30 return false;
31 }
32 const exportedName = tokens.identifierNameForToken(identifierToken);
33 return (
34 declarationInfo.typeDeclarations.has(exportedName) &&
35 !declarationInfo.valueDeclarations.has(exportedName)
36 );
37}