source: frontend/node_modules/sucrase/dist/esm/util/getTSImportedNames.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.1 KB
Line 
1import {TokenType as tt} from "../parser/tokenizer/types";
2
3import getImportExportSpecifierInfo from "./getImportExportSpecifierInfo";
4
5/**
6 * Special case code to scan for imported names in ESM TypeScript. We need to do this so we can
7 * properly get globals so we can compute shadowed globals.
8 *
9 * This is similar to logic in CJSImportProcessor, but trimmed down to avoid logic with CJS
10 * replacement and flow type imports.
11 */
12export default function getTSImportedNames(tokens) {
13 const importedNames = new Set();
14 for (let i = 0; i < tokens.tokens.length; i++) {
15 if (
16 tokens.matches1AtIndex(i, tt._import) &&
17 !tokens.matches3AtIndex(i, tt._import, tt.name, tt.eq)
18 ) {
19 collectNamesForImport(tokens, i, importedNames);
20 }
21 }
22 return importedNames;
23}
24
25function collectNamesForImport(
26 tokens,
27 index,
28 importedNames,
29) {
30 index++;
31
32 if (tokens.matches1AtIndex(index, tt.parenL)) {
33 // Dynamic import, so nothing to do
34 return;
35 }
36
37 if (tokens.matches1AtIndex(index, tt.name)) {
38 importedNames.add(tokens.identifierNameAtIndex(index));
39 index++;
40 if (tokens.matches1AtIndex(index, tt.comma)) {
41 index++;
42 }
43 }
44
45 if (tokens.matches1AtIndex(index, tt.star)) {
46 // * as
47 index += 2;
48 importedNames.add(tokens.identifierNameAtIndex(index));
49 index++;
50 }
51
52 if (tokens.matches1AtIndex(index, tt.braceL)) {
53 index++;
54 collectNamesForNamedImport(tokens, index, importedNames);
55 }
56}
57
58function collectNamesForNamedImport(
59 tokens,
60 index,
61 importedNames,
62) {
63 while (true) {
64 if (tokens.matches1AtIndex(index, tt.braceR)) {
65 return;
66 }
67
68 const specifierInfo = getImportExportSpecifierInfo(tokens, index);
69 index = specifierInfo.endIndex;
70 if (!specifierInfo.isType) {
71 importedNames.add(specifierInfo.rightName);
72 }
73
74 if (tokens.matches2AtIndex(index, tt.comma, tt.braceR)) {
75 return;
76 } else if (tokens.matches1AtIndex(index, tt.braceR)) {
77 return;
78 } else if (tokens.matches1AtIndex(index, tt.comma)) {
79 index++;
80 } else {
81 throw new Error(`Unexpected token: ${JSON.stringify(tokens.tokens[index])}`);
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.