UNPKG

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