1 | import type { Token } from "../parser/tokenizer";
|
2 | import type TokenProcessor from "../TokenProcessor";
|
3 | import type RootTransformer from "./RootTransformer";
|
4 | import Transformer from "./Transformer";
|
5 | export default class TypeScriptTransformer extends Transformer {
|
6 | readonly rootTransformer: RootTransformer;
|
7 | readonly tokens: TokenProcessor;
|
8 | readonly isImportsTransformEnabled: boolean;
|
9 | constructor(rootTransformer: RootTransformer, tokens: TokenProcessor, isImportsTransformEnabled: boolean);
|
10 | process(): boolean;
|
11 | processEnum(isExport?: boolean): void;
|
12 | /**
|
13 | * Transform an enum into equivalent JS. This has complexity in a few places:
|
14 | * - TS allows string enums, numeric enums, and a mix of the two styles within an enum.
|
15 | * - Enum keys are allowed to be referenced in later enum values.
|
16 | * - Enum keys are allowed to be strings.
|
17 | * - When enum values are omitted, they should follow an auto-increment behavior.
|
18 | */
|
19 | processEnumBody(enumName: string): void;
|
20 | /**
|
21 | * Detect name information about this enum key, which will be used to determine which code to emit
|
22 | * and whether we should declare a variable as part of this declaration.
|
23 | *
|
24 | * Some cases to keep in mind:
|
25 | * - Enum keys can be implicitly referenced later, e.g. `X = 1, Y = X`. In Sucrase, we implement
|
26 | * this by declaring a variable `X` so that later expressions can use it.
|
27 | * - In addition to the usual identifier key syntax, enum keys are allowed to be string literals,
|
28 | * e.g. `"hello world" = 3,`. Template literal syntax is NOT allowed.
|
29 | * - Even if the enum key is defined as a string literal, it may still be referenced by identifier
|
30 | * later, e.g. `"X" = 1, Y = X`. That means that we need to detect whether or not a string
|
31 | * literal is identifier-like and emit a variable if so, even if the declaration did not use an
|
32 | * identifier.
|
33 | * - Reserved keywords like `break` are valid enum keys, but are not valid to be referenced later
|
34 | * and would be a syntax error if we emitted a variable, so we need to skip the variable
|
35 | * declaration in those cases.
|
36 | *
|
37 | * The variableName return value captures these nuances: if non-null, we can and must emit a
|
38 | * variable declaration, and if null, we can't and shouldn't.
|
39 | */
|
40 | extractEnumKeyInfo(nameToken: Token): {
|
41 | nameStringCode: string;
|
42 | variableName: string | null;
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | processStringLiteralEnumMember(enumName: string, nameStringCode: string, variableName: string | null): void;
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | processExplicitValueEnumMember(enumName: string, nameStringCode: string, variableName: string | null): void;
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | processImplicitValueEnumMember(enumName: string, nameStringCode: string, variableName: string | null, previousValueCode: string | null): void;
|
104 | }
|