source: frontend/node_modules/sucrase/dist/esm/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.2 KB
Line 
1import CJSImportProcessor from "./CJSImportProcessor";
2import computeSourceMap, {} from "./computeSourceMap";
3import {HelperManager} from "./HelperManager";
4import identifyShadowedGlobals from "./identifyShadowedGlobals";
5import NameManager from "./NameManager";
6import {validateOptions} from "./Options";
7
8import {parse} from "./parser";
9
10import TokenProcessor from "./TokenProcessor";
11import RootTransformer from "./transformers/RootTransformer";
12import formatTokens from "./util/formatTokens";
13import getTSImportedNames from "./util/getTSImportedNames";
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28;
29
30export function getVersion() {
31 /* istanbul ignore next */
32 return "3.35.1";
33}
34
35export function transform(code, options) {
36 validateOptions(options);
37 try {
38 const sucraseContext = getSucraseContext(code, options);
39 const transformer = new RootTransformer(
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: computeSourceMap(
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}
71
72/**
73 * Return a string representation of the sucrase tokens, mostly useful for
74 * diagnostic purposes.
75 */
76export function getFormattedTokens(code, options) {
77 const tokens = getSucraseContext(code, options).tokenProcessor.tokens;
78 return formatTokens(code, tokens);
79}
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 */
90function 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 = parse(code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
96 const tokens = file.tokens;
97 const scopes = file.scopes;
98
99 const nameManager = new NameManager(code, tokens);
100 const helperManager = new HelperManager(nameManager);
101 const tokenProcessor = new TokenProcessor(
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 CJSImportProcessor(
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 identifyShadowedGlobals(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 identifyShadowedGlobals(tokenProcessor, scopes, getTSImportedNames(tokenProcessor));
131 }
132 return {tokenProcessor, scopes, nameManager, importProcessor, helperManager};
133}
Note: See TracBrowser for help on using the repository browser.