index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _CJSImportProcessor = require('./CJSImportProcessor'); var _CJSImportProcessor2 = _interopRequireDefault(_CJSImportProcessor);
  2. var _computeSourceMap = require('./computeSourceMap'); var _computeSourceMap2 = _interopRequireDefault(_computeSourceMap);
  3. var _HelperManager = require('./HelperManager');
  4. var _identifyShadowedGlobals = require('./identifyShadowedGlobals'); var _identifyShadowedGlobals2 = _interopRequireDefault(_identifyShadowedGlobals);
  5. var _NameManager = require('./NameManager'); var _NameManager2 = _interopRequireDefault(_NameManager);
  6. var _Options = require('./Options');
  7. var _parser = require('./parser');
  8. var _TokenProcessor = require('./TokenProcessor'); var _TokenProcessor2 = _interopRequireDefault(_TokenProcessor);
  9. var _RootTransformer = require('./transformers/RootTransformer'); var _RootTransformer2 = _interopRequireDefault(_RootTransformer);
  10. var _formatTokens = require('./util/formatTokens'); var _formatTokens2 = _interopRequireDefault(_formatTokens);
  11. var _getTSImportedNames = require('./util/getTSImportedNames'); var _getTSImportedNames2 = _interopRequireDefault(_getTSImportedNames);
  12. ;
  13. function getVersion() {
  14. /* istanbul ignore next */
  15. return "3.35.0";
  16. } exports.getVersion = getVersion;
  17. function transform(code, options) {
  18. _Options.validateOptions.call(void 0, options);
  19. try {
  20. const sucraseContext = getSucraseContext(code, options);
  21. const transformer = new (0, _RootTransformer2.default)(
  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: _computeSourceMap2.default.call(void 0,
  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. } exports.transform = transform;
  53. /**
  54. * Return a string representation of the sucrase tokens, mostly useful for
  55. * diagnostic purposes.
  56. */
  57. function getFormattedTokens(code, options) {
  58. const tokens = getSucraseContext(code, options).tokenProcessor.tokens;
  59. return _formatTokens2.default.call(void 0, code, tokens);
  60. } exports.getFormattedTokens = getFormattedTokens;
  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 = _parser.parse.call(void 0, code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
  76. const tokens = file.tokens;
  77. const scopes = file.scopes;
  78. const nameManager = new (0, _NameManager2.default)(code, tokens);
  79. const helperManager = new (0, _HelperManager.HelperManager)(nameManager);
  80. const tokenProcessor = new (0, _TokenProcessor2.default)(
  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 (0, _CJSImportProcessor2.default)(
  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. _identifyShadowedGlobals2.default.call(void 0, 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. _identifyShadowedGlobals2.default.call(void 0, tokenProcessor, scopes, _getTSImportedNames2.default.call(void 0, tokenProcessor));
  109. }
  110. return {tokenProcessor, scopes, nameManager, importProcessor, helperManager};
  111. }