shouldElideDefaultExport.js 1.1 KB

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