index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import CJSImportProcessor from "./CJSImportProcessor";
  2. import computeSourceMap, {} from "./computeSourceMap";
  3. import {HelperManager} from "./HelperManager";
  4. import identifyShadowedGlobals from "./identifyShadowedGlobals";
  5. import NameManager from "./NameManager";
  6. import {validateOptions} from "./Options";
  7. import {parse} from "./parser";
  8. import TokenProcessor from "./TokenProcessor";
  9. import RootTransformer from "./transformers/RootTransformer";
  10. import formatTokens from "./util/formatTokens";
  11. import getTSImportedNames from "./util/getTSImportedNames";
  12. ;
  13. export function getVersion() {
  14. /* istanbul ignore next */
  15. return "3.35.0";
  16. }
  17. export function transform(code, options) {
  18. validateOptions(options);
  19. try {
  20. const sucraseContext = getSucraseContext(code, options);
  21. const transformer = new RootTransformer(
  22. sucraseContext,
  23. options.transforms,
  24. Boolean(options.enableLegacyBabel5ModuleInterop),
  25. options,
  26. );
  27. const transformerResult = transformer.transform();
  28. let result = {code: transformerResult.code};
  29. if (options.sourceMapOptions) {
  30. if (!options.filePath) {
  31. throw new Error("filePath must be specified when generating a source map.");
  32. }
  33. result = {
  34. ...result,
  35. sourceMap: computeSourceMap(
  36. transformerResult,
  37. options.filePath,
  38. options.sourceMapOptions,
  39. code,
  40. sucraseContext.tokenProcessor.tokens,
  41. ),
  42. };
  43. }
  44. return result;
  45. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  46. } catch (e) {
  47. if (options.filePath) {
  48. e.message = `Error transforming ${options.filePath}: ${e.message}`;
  49. }
  50. throw e;
  51. }
  52. }
  53. /**
  54. * Return a string representation of the sucrase tokens, mostly useful for
  55. * diagnostic purposes.
  56. */
  57. export function getFormattedTokens(code, options) {
  58. const tokens = getSucraseContext(code, options).tokenProcessor.tokens;
  59. return formatTokens(code, tokens);
  60. }
  61. /**
  62. * Call into the parser/tokenizer and do some further preprocessing:
  63. * - Come up with a set of used names so that we can assign new names.
  64. * - Preprocess all import/export statements so we know which globals we are interested in.
  65. * - Compute situations where any of those globals are shadowed.
  66. *
  67. * In the future, some of these preprocessing steps can be skipped based on what actual work is
  68. * being done.
  69. */
  70. function getSucraseContext(code, options) {
  71. const isJSXEnabled = options.transforms.includes("jsx");
  72. const isTypeScriptEnabled = options.transforms.includes("typescript");
  73. const isFlowEnabled = options.transforms.includes("flow");
  74. const disableESTransforms = options.disableESTransforms === true;
  75. const file = parse(code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
  76. const tokens = file.tokens;
  77. const scopes = file.scopes;
  78. const nameManager = new NameManager(code, tokens);
  79. const helperManager = new HelperManager(nameManager);
  80. const tokenProcessor = new TokenProcessor(
  81. code,
  82. tokens,
  83. isFlowEnabled,
  84. disableESTransforms,
  85. helperManager,
  86. );
  87. const enableLegacyTypeScriptModuleInterop = Boolean(options.enableLegacyTypeScriptModuleInterop);
  88. let importProcessor = null;
  89. if (options.transforms.includes("imports")) {
  90. importProcessor = new CJSImportProcessor(
  91. nameManager,
  92. tokenProcessor,
  93. enableLegacyTypeScriptModuleInterop,
  94. options,
  95. options.transforms.includes("typescript"),
  96. Boolean(options.keepUnusedImports),
  97. helperManager,
  98. );
  99. importProcessor.preprocessTokens();
  100. // We need to mark shadowed globals after processing imports so we know that the globals are,
  101. // but before type-only import pruning, since that relies on shadowing information.
  102. identifyShadowedGlobals(tokenProcessor, scopes, importProcessor.getGlobalNames());
  103. if (options.transforms.includes("typescript") && !options.keepUnusedImports) {
  104. importProcessor.pruneTypeOnlyImports();
  105. }
  106. } else if (options.transforms.includes("typescript") && !options.keepUnusedImports) {
  107. // Shadowed global detection is needed for TS implicit elision of imported names.
  108. identifyShadowedGlobals(tokenProcessor, scopes, getTSImportedNames(tokenProcessor));
  109. }
  110. return {tokenProcessor, scopes, nameManager, importProcessor, helperManager};
  111. }