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