bare-type-stripper 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +37 -0
- package/LICENSE +201 -0
- package/README.md +88 -0
- package/binding.c +90 -0
- package/index.d.ts +19 -0
- package/index.js +129 -0
- package/lib/binding/bare.js +1 -0
- package/lib/binding/node.js +3 -0
- package/lib/strip.h +2791 -0
- package/package.json +65 -1
- package/prebuilds/android-arm/bare-type-stripper.bare +0 -0
- package/prebuilds/android-arm/bare-type-stripper.node +0 -0
- package/prebuilds/android-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/android-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/android-ia32/bare-type-stripper.bare +0 -0
- package/prebuilds/android-ia32/bare-type-stripper.node +0 -0
- package/prebuilds/android-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/android-x64/bare-type-stripper.node +0 -0
- package/prebuilds/darwin-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/darwin-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/darwin-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/darwin-x64/bare-type-stripper.node +0 -0
- package/prebuilds/ios-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-type-stripper.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/linux-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-x64/bare-type-stripper.node +0 -0
- package/prebuilds/win32-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/win32-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/win32-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/win32-x64/bare-type-stripper.node +0 -0
package/lib/strip.h
ADDED
|
@@ -0,0 +1,2791 @@
|
|
|
1
|
+
#ifndef BARE_TYPE_STRIPPER_H
|
|
2
|
+
#define BARE_TYPE_STRIPPER_H
|
|
3
|
+
|
|
4
|
+
#include <assert.h>
|
|
5
|
+
#include <stdbool.h>
|
|
6
|
+
#include <stddef.h>
|
|
7
|
+
#include <stdint.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
#include <utf.h>
|
|
11
|
+
|
|
12
|
+
// Range flag bits. When set on a stripped range, they tell the JS apply
|
|
13
|
+
// layer to replace some bytes with something other than a space.
|
|
14
|
+
enum {
|
|
15
|
+
bare_type_stripper_semi = 0x1, // First byte of range becomes ';' instead of space
|
|
16
|
+
bare_type_stripper_paren = 0x2, // First byte of range becomes ')' instead of space
|
|
17
|
+
bare_type_stripper_error = 0x4 // Range marks non-erasable TS syntax that can't be stripped to valid JS
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
#define BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE 512 // Power of two
|
|
21
|
+
#define BARE_TYPE_STRIPPER__PAREN_STACK_SIZE 512
|
|
22
|
+
|
|
23
|
+
// Lexer context. Carries the input, the accumulated strip ranges, and a
|
|
24
|
+
// small cache of paren match positions used by the arrow-function lookahead.
|
|
25
|
+
typedef struct {
|
|
26
|
+
// Input being lexed.
|
|
27
|
+
const utf8_t *s;
|
|
28
|
+
size_t n;
|
|
29
|
+
|
|
30
|
+
// Strip ranges as (start, end, flags) triples, heap-allocated and grown
|
|
31
|
+
// geometrically. The caller owns the buffer and must free() it.
|
|
32
|
+
uint32_t *ranges;
|
|
33
|
+
uint32_t len; // In triples
|
|
34
|
+
uint32_t cap; // In triples
|
|
35
|
+
|
|
36
|
+
// Direct-mapped cache of paren match positions: paren_open[slot] is the
|
|
37
|
+
// position of a '(' and paren_close[slot] the index just past its matching
|
|
38
|
+
// ')'. A zero close marks an empty slot - a '(' at position 0 closes at
|
|
39
|
+
// position 2 or later, so 0 can't be a valid close.
|
|
40
|
+
size_t paren_open[BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE];
|
|
41
|
+
size_t paren_close[BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE];
|
|
42
|
+
} bare_type_stripper_t;
|
|
43
|
+
|
|
44
|
+
// Previous-token classification, used to decide how to interpret the next
|
|
45
|
+
// character. Together with a flag for the previous '.' / '?.' (property
|
|
46
|
+
// access), this is enough to distinguish e.g. division from regex, generic
|
|
47
|
+
// arguments from less-than, postfix '!' from prefix '!', and TS contextual
|
|
48
|
+
// keywords from ordinary identifiers.
|
|
49
|
+
enum {
|
|
50
|
+
bare_type_stripper__prev_none = 0, // Start of input or block - statement start
|
|
51
|
+
bare_type_stripper__prev_stmt = 1, // After ';' or end of statement - statement start
|
|
52
|
+
bare_type_stripper__prev_op = 2, // After an operator, comma, opening bracket - expression start
|
|
53
|
+
bare_type_stripper__prev_expr = 3, // After an identifier, literal, ']' - expression end
|
|
54
|
+
bare_type_stripper__prev_paren = 4, // After ')' - expression end, but a following '{' is a block
|
|
55
|
+
bare_type_stripper__prev_arrow = 5, // After '=>' - expression start, but a following '{' is a function body
|
|
56
|
+
bare_type_stripper__prev_async = 6 // After the identifier 'async' - expression end, but a following '(' may start arrow parameters
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// True when `prev` marks the end of an expression - i.e. the next token
|
|
60
|
+
// continues or follows an expression rather than starting one.
|
|
61
|
+
static inline bool
|
|
62
|
+
bare_type_stripper__prev_is_expr(int prev) {
|
|
63
|
+
return prev == bare_type_stripper__prev_expr || prev == bare_type_stripper__prev_paren || prev == bare_type_stripper__prev_async;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static inline bool
|
|
67
|
+
bare_type_stripper__is_line_terminator(uint8_t c) {
|
|
68
|
+
return c == 0xa || c == 0xd;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static inline bool
|
|
72
|
+
bare_type_stripper__is_whitespace(uint8_t c) {
|
|
73
|
+
// Byte-oriented: Multi-byte Unicode whitespace (NBSP, U+2028/2029) is not
|
|
74
|
+
// recognized and lexes as identifier bytes instead, which is harmless for
|
|
75
|
+
// stripping purposes and vanishingly rare in real source.
|
|
76
|
+
return c == ' ' || c == '\t' || c == 0xb || c == 0xc || bare_type_stripper__is_line_terminator(c);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static inline bool
|
|
80
|
+
bare_type_stripper__is_id_start(uint8_t c) {
|
|
81
|
+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c >= 0x80;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static inline bool
|
|
85
|
+
bare_type_stripper__is_id(uint8_t c) {
|
|
86
|
+
return bare_type_stripper__is_id_start(c) || (c >= '0' && c <= '9');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Current character, unchecked
|
|
90
|
+
#define u(offset) ((uint8_t) s[i + offset])
|
|
91
|
+
|
|
92
|
+
#define lt bare_type_stripper__is_line_terminator
|
|
93
|
+
#define ws bare_type_stripper__is_whitespace
|
|
94
|
+
#define ids bare_type_stripper__is_id_start
|
|
95
|
+
#define id bare_type_stripper__is_id
|
|
96
|
+
|
|
97
|
+
static inline int
|
|
98
|
+
bare_type_stripper__add_range_flags(bare_type_stripper_t *ctx, size_t start, size_t end, uint32_t flags) {
|
|
99
|
+
assert(end > start);
|
|
100
|
+
|
|
101
|
+
if (ctx->len == ctx->cap) {
|
|
102
|
+
uint32_t cap = ctx->cap == 0 ? 64 : ctx->cap * 2;
|
|
103
|
+
|
|
104
|
+
uint32_t *ranges = realloc(ctx->ranges, cap * 3 * sizeof(uint32_t));
|
|
105
|
+
if (ranges == NULL) return -1;
|
|
106
|
+
|
|
107
|
+
ctx->ranges = ranges;
|
|
108
|
+
ctx->cap = cap;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
uint32_t *entry = &ctx->ranges[ctx->len * 3];
|
|
112
|
+
|
|
113
|
+
entry[0] = (uint32_t) start;
|
|
114
|
+
entry[1] = (uint32_t) end;
|
|
115
|
+
entry[2] = flags;
|
|
116
|
+
|
|
117
|
+
ctx->len++;
|
|
118
|
+
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static inline int
|
|
123
|
+
bare_type_stripper__add_range(bare_type_stripper_t *ctx, size_t start, size_t end) {
|
|
124
|
+
return bare_type_stripper__add_range_flags(ctx, start, end, 0);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// True iff s[ks..ks+kl] equals the keyword `kw` of length `kwl`. Use to test
|
|
128
|
+
// an already-lexed identifier span against a known keyword without rescanning.
|
|
129
|
+
static inline bool
|
|
130
|
+
bare_type_stripper__match_kw(const utf8_t *s, size_t ks, size_t kl, const char *kw, size_t kwl) {
|
|
131
|
+
return kl == kwl && memcmp(&s[ks], kw, kwl) == 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// True iff the bytes at i spell the keyword `kw` of length `kwl` with a word
|
|
135
|
+
// boundary after it (end of input or a non-identifier byte).
|
|
136
|
+
static inline bool
|
|
137
|
+
bare_type_stripper__at_kw(const utf8_t *s, size_t n, size_t i, const char *kw, size_t kwl) {
|
|
138
|
+
return i + kwl <= n && memcmp(&s[i], kw, kwl) == 0 && (i + kwl == n || !bare_type_stripper__is_id(s[i + kwl]));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// True iff the identifier span s[ks..ks+kl] is one of the TS modifier
|
|
142
|
+
// keywords that can prefix a class member or parameter property: 'public',
|
|
143
|
+
// 'private', 'protected', 'readonly', 'override' - and 'abstract' when
|
|
144
|
+
// `allow_abstract` is true (class members can be abstract, parameters can't).
|
|
145
|
+
static inline bool
|
|
146
|
+
bare_type_stripper__is_ts_modifier(const utf8_t *s, size_t ks, size_t kl, bool allow_abstract) {
|
|
147
|
+
if (kl == 6) return memcmp(&s[ks], "public", 6) == 0;
|
|
148
|
+
if (kl == 7) return memcmp(&s[ks], "private", 7) == 0;
|
|
149
|
+
if (kl == 8) {
|
|
150
|
+
return memcmp(&s[ks], "readonly", 8) == 0 ||
|
|
151
|
+
memcmp(&s[ks], "override", 8) == 0 ||
|
|
152
|
+
(allow_abstract && memcmp(&s[ks], "abstract", 8) == 0);
|
|
153
|
+
}
|
|
154
|
+
if (kl == 9) return memcmp(&s[ks], "protected", 9) == 0;
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static inline size_t
|
|
159
|
+
bare_type_stripper__skip_string(const utf8_t *s, size_t n, size_t i);
|
|
160
|
+
|
|
161
|
+
static inline size_t
|
|
162
|
+
bare_type_stripper__skip_template(const utf8_t *s, size_t n, size_t i);
|
|
163
|
+
|
|
164
|
+
static inline size_t
|
|
165
|
+
bare_type_stripper__skip_regex(const utf8_t *s, size_t n, size_t i);
|
|
166
|
+
|
|
167
|
+
static inline size_t
|
|
168
|
+
bare_type_stripper__skip_trivia(const utf8_t *s, size_t n, size_t i);
|
|
169
|
+
|
|
170
|
+
static inline size_t
|
|
171
|
+
bare_type_stripper__scan_generic(const utf8_t *s, size_t n, size_t i);
|
|
172
|
+
|
|
173
|
+
static inline size_t
|
|
174
|
+
bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t terms);
|
|
175
|
+
|
|
176
|
+
static inline size_t
|
|
177
|
+
bare_type_stripper__scan_declare(const utf8_t *s, size_t n, size_t i);
|
|
178
|
+
|
|
179
|
+
#define BARE_TYPE_STRIPPER__T_COMMA 0x01
|
|
180
|
+
#define BARE_TYPE_STRIPPER__T_SEMI 0x02
|
|
181
|
+
#define BARE_TYPE_STRIPPER__T_PAREN 0x04 // ')'
|
|
182
|
+
#define BARE_TYPE_STRIPPER__T_BRACE 0x08 // '{'/'}'
|
|
183
|
+
#define BARE_TYPE_STRIPPER__T_BRACK 0x10 // ']'
|
|
184
|
+
#define BARE_TYPE_STRIPPER__T_EQ 0x20 // '='
|
|
185
|
+
#define BARE_TYPE_STRIPPER__T_NL 0x40 // Bare newline
|
|
186
|
+
#define BARE_TYPE_STRIPPER__T_ANGLE 0x80 // '>' at depth 0
|
|
187
|
+
#define BARE_TYPE_STRIPPER__T_ARROW 0x100 // '=>' at depth 0
|
|
188
|
+
#define BARE_TYPE_STRIPPER__T_INOF 0x200 // 'in'/'of' identifier at depth 0
|
|
189
|
+
|
|
190
|
+
static inline size_t
|
|
191
|
+
bare_type_stripper__skip_trivia(const utf8_t *s, size_t n, size_t i) {
|
|
192
|
+
while (i < n) {
|
|
193
|
+
if (ws(u(0))) {
|
|
194
|
+
i++;
|
|
195
|
+
} else if (i + 1 < n && u(0) == '/' && u(1) == '/') {
|
|
196
|
+
i += 2;
|
|
197
|
+
while (i < n && !lt(u(0))) i++;
|
|
198
|
+
} else if (i + 1 < n && u(0) == '/' && u(1) == '*') {
|
|
199
|
+
i += 2;
|
|
200
|
+
while (i + 1 < n && !(u(0) == '*' && u(1) == '/')) i++;
|
|
201
|
+
if (i + 1 < n) i += 2;
|
|
202
|
+
else i = n;
|
|
203
|
+
} else {
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return i;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Skip leading trivia, an identifier if present, and trailing trivia. Used
|
|
212
|
+
// to consume the name of a declaration ('class NAME', 'enum NAME', ...).
|
|
213
|
+
static inline size_t
|
|
214
|
+
bare_type_stripper__skip_name(const utf8_t *s, size_t n, size_t i) {
|
|
215
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
216
|
+
while (i < n && id(u(0))) i++;
|
|
217
|
+
return bare_type_stripper__skip_trivia(s, n, i);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// True when removing a chunk ending at 'pos' would expose an ASI hazard:
|
|
221
|
+
// The next non-trivia byte is across a newline and starts a token that would
|
|
222
|
+
// continue the previous expression (call '(', subscript '[', tagged template
|
|
223
|
+
// '`'). Used by 'as'/'satisfies' strips, where blanking the type otherwise
|
|
224
|
+
// glues 'foo as T\n(1)' into 'foo(1)'.
|
|
225
|
+
static inline bool
|
|
226
|
+
bare_type_stripper__asi_hazard_after(const utf8_t *s, size_t n, size_t i) {
|
|
227
|
+
bool seen_nl = false;
|
|
228
|
+
|
|
229
|
+
while (i < n) {
|
|
230
|
+
uint8_t c = u(0);
|
|
231
|
+
if (lt(c)) {
|
|
232
|
+
seen_nl = true;
|
|
233
|
+
i++;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (ws(c)) {
|
|
237
|
+
i++;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (i + 1 < n && c == '/' && u(1) == '/') {
|
|
241
|
+
i += 2;
|
|
242
|
+
while (i < n && !lt(u(0))) i++;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (i + 1 < n && c == '/' && u(1) == '*') {
|
|
246
|
+
i += 2;
|
|
247
|
+
while (i + 1 < n && !(u(0) == '*' && u(1) == '/')) {
|
|
248
|
+
if (lt(u(0))) seen_nl = true;
|
|
249
|
+
i++;
|
|
250
|
+
}
|
|
251
|
+
if (i + 1 < n) i += 2;
|
|
252
|
+
else i = n;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
return seen_nl && (c == '(' || c == '[' || c == '`');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static inline size_t
|
|
262
|
+
bare_type_stripper__skip_string(const utf8_t *s, size_t n, size_t i) {
|
|
263
|
+
uint8_t e = u(0);
|
|
264
|
+
i++;
|
|
265
|
+
|
|
266
|
+
while (i < n) {
|
|
267
|
+
if (u(0) == '\\') {
|
|
268
|
+
if (i + 1 < n) i += 2;
|
|
269
|
+
else i++;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (u(0) == e) {
|
|
274
|
+
i++;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (lt(u(0))) break;
|
|
279
|
+
|
|
280
|
+
i++;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return i;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
static inline size_t
|
|
287
|
+
bare_type_stripper__skip_template(const utf8_t *s, size_t n, size_t i) {
|
|
288
|
+
i++;
|
|
289
|
+
|
|
290
|
+
while (i < n) {
|
|
291
|
+
if (u(0) == '\\') {
|
|
292
|
+
if (i + 1 < n) i += 2;
|
|
293
|
+
else i++;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (u(0) == '`') {
|
|
298
|
+
i++;
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (u(0) == '$' && i + 1 < n && u(1) == '{') {
|
|
303
|
+
i += 2;
|
|
304
|
+
int depth = 1;
|
|
305
|
+
|
|
306
|
+
while (i < n && depth > 0) {
|
|
307
|
+
if (u(0) == '`') {
|
|
308
|
+
i = bare_type_stripper__skip_template(s, n, i);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (u(0) == '\'' || u(0) == '"') {
|
|
312
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
if (u(0) == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
316
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (u(0) == '{') depth++;
|
|
321
|
+
else if (u(0) == '}') depth--;
|
|
322
|
+
|
|
323
|
+
if (depth == 0) {
|
|
324
|
+
i++;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
i++;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
i++;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return i;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
static inline size_t
|
|
341
|
+
bare_type_stripper__skip_regex(const utf8_t *s, size_t n, size_t i) {
|
|
342
|
+
i++;
|
|
343
|
+
bool in_class = false;
|
|
344
|
+
|
|
345
|
+
while (i < n) {
|
|
346
|
+
if (u(0) == '\\') {
|
|
347
|
+
if (i + 1 < n) i += 2;
|
|
348
|
+
else i++;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (in_class) {
|
|
353
|
+
if (u(0) == ']') in_class = false;
|
|
354
|
+
} else {
|
|
355
|
+
if (u(0) == '[') {
|
|
356
|
+
in_class = true;
|
|
357
|
+
} else if (u(0) == '/') {
|
|
358
|
+
i++;
|
|
359
|
+
while (i < n && u(0) >= 'a' && u(0) <= 'z') i++;
|
|
360
|
+
return i;
|
|
361
|
+
} else if (lt(u(0))) {
|
|
362
|
+
return i;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
i++;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return i;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Advance past a single span of inert content (whitespace, line comment,
|
|
373
|
+
// block comment, string literal, template literal) at *result. Returns true
|
|
374
|
+
// and updates *result if it consumed something; returns false otherwise.
|
|
375
|
+
// Used by balanced-bracket scanners that need to ignore brackets nested
|
|
376
|
+
// inside literals and comments.
|
|
377
|
+
static inline bool
|
|
378
|
+
bare_type_stripper__try_skip_inert(const utf8_t *s, size_t n, size_t *result) {
|
|
379
|
+
size_t i = *result;
|
|
380
|
+
if (i >= n) return false;
|
|
381
|
+
|
|
382
|
+
uint8_t c = u(0);
|
|
383
|
+
if (ws(c)) {
|
|
384
|
+
*result = i + 1;
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
if (c == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
388
|
+
*result = bare_type_stripper__skip_trivia(s, n, i);
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
if (c == '\'' || c == '"') {
|
|
392
|
+
*result = bare_type_stripper__skip_string(s, n, i);
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
if (c == '`') {
|
|
396
|
+
*result = bare_type_stripper__skip_template(s, n, i);
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Scan past a balanced 'open ... close' span. Assumes s[i] == open. Returns
|
|
403
|
+
// the index just past the matching close. Skips over comments, strings, and
|
|
404
|
+
// template literals so brackets inside them don't affect the depth count.
|
|
405
|
+
// If the brackets never close, returns n.
|
|
406
|
+
static inline size_t
|
|
407
|
+
bare_type_stripper__scan_balanced(const utf8_t *s, size_t n, size_t i, uint8_t open, uint8_t close) {
|
|
408
|
+
if (i >= n || u(0) != open) return i + 1;
|
|
409
|
+
i++;
|
|
410
|
+
int depth = 1;
|
|
411
|
+
|
|
412
|
+
while (i < n && depth > 0) {
|
|
413
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
414
|
+
|
|
415
|
+
if (u(0) == open) depth++;
|
|
416
|
+
else if (u(0) == close) depth--;
|
|
417
|
+
|
|
418
|
+
i++;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return i;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Scan past a balanced '(...)' group, like scan_balanced, but backed by the
|
|
425
|
+
// context's paren match cache. The arrow-function lookahead runs this for
|
|
426
|
+
// every candidate '(' - on nested groups the inner scans would otherwise
|
|
427
|
+
// redo the outer scan's work, making the lookahead quadratic in nesting
|
|
428
|
+
// depth. Assumes s[i] == '('.
|
|
429
|
+
static inline size_t
|
|
430
|
+
bare_type_stripper__scan_group(bare_type_stripper_t *ctx, size_t i) {
|
|
431
|
+
const utf8_t *s = ctx->s;
|
|
432
|
+
size_t n = ctx->n;
|
|
433
|
+
|
|
434
|
+
size_t slot = i & (BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE - 1);
|
|
435
|
+
if (ctx->paren_close[slot] != 0 && ctx->paren_open[slot] == i) return ctx->paren_close[slot];
|
|
436
|
+
|
|
437
|
+
// Positions of the unclosed '(' at each depth, so every nested match
|
|
438
|
+
// discovered along the way gets cached as well. Levels deeper than the
|
|
439
|
+
// stack still balance, they just aren't recorded.
|
|
440
|
+
size_t opens[BARE_TYPE_STRIPPER__PAREN_STACK_SIZE];
|
|
441
|
+
int depth = 0;
|
|
442
|
+
|
|
443
|
+
opens[depth++] = i;
|
|
444
|
+
i++;
|
|
445
|
+
|
|
446
|
+
while (i < n && depth > 0) {
|
|
447
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
448
|
+
|
|
449
|
+
if (u(0) == '(') {
|
|
450
|
+
if (depth < BARE_TYPE_STRIPPER__PAREN_STACK_SIZE) opens[depth] = i;
|
|
451
|
+
depth++;
|
|
452
|
+
i++;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (u(0) == ')') {
|
|
457
|
+
i++;
|
|
458
|
+
depth--;
|
|
459
|
+
|
|
460
|
+
if (depth < BARE_TYPE_STRIPPER__PAREN_STACK_SIZE) {
|
|
461
|
+
size_t open = opens[depth];
|
|
462
|
+
size_t slot = open & (BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE - 1);
|
|
463
|
+
|
|
464
|
+
ctx->paren_open[slot] = open;
|
|
465
|
+
ctx->paren_close[slot] = i;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
i++;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return i;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Scan past a balanced '<...>'. Assumes s[i] == '<'. Returns the index after
|
|
478
|
+
// the closing '>'. If the brackets don't balance cleanly (e.g. we ran into a
|
|
479
|
+
// ';' which is almost certainly a comparison, not a generic), returns the
|
|
480
|
+
// original i+1 to signal "not a generic".
|
|
481
|
+
static inline size_t
|
|
482
|
+
bare_type_stripper__scan_generic(const utf8_t *s, size_t n, size_t i) {
|
|
483
|
+
size_t start = i;
|
|
484
|
+
if (u(0) != '<') return i + 1;
|
|
485
|
+
|
|
486
|
+
i++;
|
|
487
|
+
int depth = 1;
|
|
488
|
+
|
|
489
|
+
while (i < n && depth > 0) {
|
|
490
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
491
|
+
|
|
492
|
+
uint8_t ch = u(0);
|
|
493
|
+
|
|
494
|
+
if (ch == '<') {
|
|
495
|
+
depth++;
|
|
496
|
+
i++;
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (ch == '>') {
|
|
500
|
+
depth--;
|
|
501
|
+
i++;
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// '=>' is an arrow-type separator inside the generic arguments (e.g.
|
|
506
|
+
// 'foo<(a: T) => U>()'), not a closing '>'.
|
|
507
|
+
if (ch == '=' && i + 1 < n && u(1) == '>') {
|
|
508
|
+
i += 2;
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (ch == '(' || ch == '[' || ch == '{') {
|
|
513
|
+
uint8_t close = ch == '(' ? ')' : ch == '[' ? ']'
|
|
514
|
+
: '}';
|
|
515
|
+
i = bare_type_stripper__scan_balanced(s, n, i, ch, close);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// ';' or an unmatched closing bracket can't appear inside generic
|
|
520
|
+
// arguments: This is almost certainly a comparison.
|
|
521
|
+
if (ch == ';' || ch == ')' || ch == ']' || ch == '}') return start + 1;
|
|
522
|
+
|
|
523
|
+
i++;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// The brackets never balanced before end of input. Signal "not a generic"
|
|
527
|
+
// rather than returning n, which callers would mistake for a generic
|
|
528
|
+
// spanning the rest of the file.
|
|
529
|
+
if (depth > 0) return start + 1;
|
|
530
|
+
|
|
531
|
+
return i;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Strip a balanced '<...>' generic argument or parameter list at *result,
|
|
535
|
+
// if present. Advances past it on success; leaves the cursor alone when
|
|
536
|
+
// there is no '<' or the brackets don't scan as a generic.
|
|
537
|
+
static inline int
|
|
538
|
+
bare_type_stripper__strip_generic(bare_type_stripper_t *ctx, size_t *result) {
|
|
539
|
+
const utf8_t *s = ctx->s;
|
|
540
|
+
size_t n = ctx->n;
|
|
541
|
+
|
|
542
|
+
size_t i = *result;
|
|
543
|
+
|
|
544
|
+
if (i >= n || s[i] != '<') return 0;
|
|
545
|
+
|
|
546
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, i);
|
|
547
|
+
if (ge <= i + 1) return 0;
|
|
548
|
+
|
|
549
|
+
int err = bare_type_stripper__add_range(ctx, i, ge);
|
|
550
|
+
if (err < 0) return err;
|
|
551
|
+
|
|
552
|
+
*result = ge;
|
|
553
|
+
return 0;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// Scan past a type expression starting at i. Terminates at the first
|
|
557
|
+
// character listed in `terms` at depth 0. Balances (), [], {}, <>.
|
|
558
|
+
//
|
|
559
|
+
// At depth 0 with T_NL, a newline terminates the scan only if the type
|
|
560
|
+
// doesn't appear to continue across it - i.e. the previous token wasn't a
|
|
561
|
+
// type-level operator AND the next non-trivia token isn't one either. This
|
|
562
|
+
// keeps multi-line union/intersection/conditional types intact while still
|
|
563
|
+
// honouring ASI at the end of an actual statement.
|
|
564
|
+
static inline size_t
|
|
565
|
+
bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t terms) {
|
|
566
|
+
int depth = 0;
|
|
567
|
+
bool seen_token = false;
|
|
568
|
+
// True when the most recent significant token was an operator that
|
|
569
|
+
// expects another type expression to follow (e.g. '|', '&', 'extends').
|
|
570
|
+
// Starts true so an immediate leading newline doesn't stop us.
|
|
571
|
+
bool last_was_op = true;
|
|
572
|
+
|
|
573
|
+
while (i < n) {
|
|
574
|
+
uint8_t ch = u(0);
|
|
575
|
+
|
|
576
|
+
if (ws(ch)) {
|
|
577
|
+
if (depth == 0 && (terms & BARE_TYPE_STRIPPER__T_NL) && lt(ch)) {
|
|
578
|
+
bool continues = last_was_op;
|
|
579
|
+
if (!continues) {
|
|
580
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
|
|
581
|
+
if (peek < n) {
|
|
582
|
+
uint8_t nb = s[peek];
|
|
583
|
+
if (nb == '|' || nb == '&' || nb == '?' || nb == ':' || nb == '.') {
|
|
584
|
+
continues = true;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (!continues) return i;
|
|
589
|
+
}
|
|
590
|
+
i++;
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
if (ch == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
594
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
if (ch == '\'' || ch == '"') {
|
|
598
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
599
|
+
seen_token = true;
|
|
600
|
+
last_was_op = false;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
if (ch == '`') {
|
|
604
|
+
i = bare_type_stripper__skip_template(s, n, i);
|
|
605
|
+
seen_token = true;
|
|
606
|
+
last_was_op = false;
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (depth == 0) {
|
|
611
|
+
if ((terms & BARE_TYPE_STRIPPER__T_COMMA) && ch == ',') return i;
|
|
612
|
+
if ((terms & BARE_TYPE_STRIPPER__T_SEMI) && ch == ';') return i;
|
|
613
|
+
if ((terms & BARE_TYPE_STRIPPER__T_PAREN) && ch == ')') return i;
|
|
614
|
+
if ((terms & BARE_TYPE_STRIPPER__T_BRACK) && ch == ']') return i;
|
|
615
|
+
if ((terms & BARE_TYPE_STRIPPER__T_BRACE) && ch == '}') return i;
|
|
616
|
+
if ((terms & BARE_TYPE_STRIPPER__T_ANGLE) && ch == '>') return i;
|
|
617
|
+
|
|
618
|
+
if (ch == '=') {
|
|
619
|
+
// '==' is comparison (rare inside a type, treated as continuation).
|
|
620
|
+
if (i + 1 < n && u(1) == '=') {
|
|
621
|
+
i += 2;
|
|
622
|
+
seen_token = true;
|
|
623
|
+
last_was_op = true;
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// '=>' is either an arrow-function separator (terminator) or part of
|
|
628
|
+
// an arrow-type expression (continuation), depending on terms.
|
|
629
|
+
if (i + 1 < n && u(1) == '>') {
|
|
630
|
+
if (terms & BARE_TYPE_STRIPPER__T_ARROW) return i;
|
|
631
|
+
i += 2;
|
|
632
|
+
seen_token = true;
|
|
633
|
+
last_was_op = true;
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (terms & BARE_TYPE_STRIPPER__T_EQ) return i;
|
|
638
|
+
|
|
639
|
+
i++;
|
|
640
|
+
seen_token = true;
|
|
641
|
+
last_was_op = true;
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if ((terms & BARE_TYPE_STRIPPER__T_BRACE) && ch == '{' && seen_token) return i;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (ch == '(' || ch == '[' || ch == '{') {
|
|
649
|
+
depth++;
|
|
650
|
+
i++;
|
|
651
|
+
seen_token = true;
|
|
652
|
+
last_was_op = true;
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
if (ch == ')' || ch == ']' || ch == '}') {
|
|
656
|
+
if (depth == 0) return i;
|
|
657
|
+
depth--;
|
|
658
|
+
i++;
|
|
659
|
+
last_was_op = false;
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (ch == '<') {
|
|
664
|
+
depth++;
|
|
665
|
+
i++;
|
|
666
|
+
seen_token = true;
|
|
667
|
+
last_was_op = true;
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (ch == '>') {
|
|
671
|
+
if (depth > 0) {
|
|
672
|
+
depth--;
|
|
673
|
+
i++;
|
|
674
|
+
last_was_op = false;
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
// At depth 0, '>' is a comparison operator in surrounding JS code, not
|
|
678
|
+
// a type close - it ends the type. Without this, 'as number > 2' would
|
|
679
|
+
// greedily swallow ' > 2' into the cast.
|
|
680
|
+
return i;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
if (ids(ch)) {
|
|
684
|
+
size_t ks = i;
|
|
685
|
+
while (i < n && id(u(0))) i++;
|
|
686
|
+
size_t kl = i - ks;
|
|
687
|
+
|
|
688
|
+
if (depth == 0 && (terms & BARE_TYPE_STRIPPER__T_INOF)) {
|
|
689
|
+
if (kl == 2 && (memcmp(&s[ks], "in", 2) == 0 || memcmp(&s[ks], "of", 2) == 0)) {
|
|
690
|
+
return ks;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Type-level keywords that expect another type to follow. Marking these
|
|
695
|
+
// as 'op' keeps multi-line conditional types intact (e.g. 'T extends\n
|
|
696
|
+
// U ? A : B').
|
|
697
|
+
last_was_op =
|
|
698
|
+
(kl == 5 && (memcmp(&s[ks], "infer", 5) == 0 ||
|
|
699
|
+
memcmp(&s[ks], "keyof", 5) == 0)) ||
|
|
700
|
+
(kl == 6 && memcmp(&s[ks], "typeof", 6) == 0) ||
|
|
701
|
+
(kl == 7 && memcmp(&s[ks], "extends", 7) == 0);
|
|
702
|
+
seen_token = true;
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// Punctuation that combines two type expressions ('|', '&', '?', ':', ',').
|
|
707
|
+
// We don't terminate on these here (T_COMMA above already covers ',' at
|
|
708
|
+
// depth 0 when set, and ',' inside brackets has depth > 0); they just
|
|
709
|
+
// continue scanning and mark the position as 'after an operator'.
|
|
710
|
+
last_was_op = (ch == '|' || ch == '&' || ch == '?' || ch == ':' || ch == ',' || ch == '.');
|
|
711
|
+
seen_token = true;
|
|
712
|
+
i++;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return i;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// Forward declaration of the master walker - used by helpers that process
|
|
719
|
+
// nested blocks (function bodies, class methods, etc.).
|
|
720
|
+
static inline int
|
|
721
|
+
bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop_at, bool body_is_stmt, bool stop_at_asi, uint32_t depth);
|
|
722
|
+
|
|
723
|
+
// Walk a template literal, recursively stripping type syntax inside each
|
|
724
|
+
// '${...}' expression. Assumes s[*result] == '`'. Leaves the cursor after
|
|
725
|
+
// the closing '`'. Unlike skip_template (which only advances the cursor),
|
|
726
|
+
// this invokes the walker on the substitution expressions so 'x as Foo'
|
|
727
|
+
// inside '${x as Foo}' still gets stripped.
|
|
728
|
+
static inline int
|
|
729
|
+
bare_type_stripper__walk_template(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
730
|
+
const utf8_t *s = ctx->s;
|
|
731
|
+
size_t n = ctx->n;
|
|
732
|
+
|
|
733
|
+
int err;
|
|
734
|
+
size_t i = *result;
|
|
735
|
+
i++;
|
|
736
|
+
|
|
737
|
+
while (i < n) {
|
|
738
|
+
if (u(0) == '\\') {
|
|
739
|
+
if (i + 1 < n) i += 2;
|
|
740
|
+
else i++;
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (u(0) == '`') {
|
|
745
|
+
i++;
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
if (u(0) == '$' && i + 1 < n && u(1) == '{') {
|
|
750
|
+
i += 2;
|
|
751
|
+
|
|
752
|
+
err = bare_type_stripper__walk(ctx, &i, '}', false, false, depth + 1);
|
|
753
|
+
if (err < 0) return err;
|
|
754
|
+
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
i++;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
*result = i;
|
|
762
|
+
return 0;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// Process a decorator '@...'. Expects s[i] == '@'. Handles three forms:
|
|
766
|
+
//
|
|
767
|
+
// @Name.Path bare reference
|
|
768
|
+
// @Name.Path<T> TS 4.7+ generic decorator (strips '<T>')
|
|
769
|
+
// @Name.Path(args) decorator factory call (walks args)
|
|
770
|
+
// @(expr) parenthesized expression (walks expr)
|
|
771
|
+
//
|
|
772
|
+
// In the (args) and (expr) forms we recurse via walk() so 'as'/'satisfies'/
|
|
773
|
+
// '!' inside the args also get stripped. Leaves the cursor just past the
|
|
774
|
+
// closing ')' if there is one, otherwise just past the identifier path.
|
|
775
|
+
static inline int
|
|
776
|
+
bare_type_stripper__process_decorator(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
777
|
+
const utf8_t *s = ctx->s;
|
|
778
|
+
size_t n = ctx->n;
|
|
779
|
+
|
|
780
|
+
int err;
|
|
781
|
+
size_t i = *result;
|
|
782
|
+
|
|
783
|
+
if (i >= n || u(0) != '@') return 0;
|
|
784
|
+
i++;
|
|
785
|
+
|
|
786
|
+
if (i < n && u(0) == '(') {
|
|
787
|
+
i++;
|
|
788
|
+
|
|
789
|
+
err = bare_type_stripper__walk(ctx, &i, ')', false, false, depth + 1);
|
|
790
|
+
if (err < 0) return err;
|
|
791
|
+
|
|
792
|
+
if (i < n && u(0) == ')') i++;
|
|
793
|
+
} else {
|
|
794
|
+
while (i < n && (id(u(0)) || u(0) == '.')) i++;
|
|
795
|
+
|
|
796
|
+
err = bare_type_stripper__strip_generic(ctx, &i);
|
|
797
|
+
if (err < 0) return err;
|
|
798
|
+
|
|
799
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
800
|
+
|
|
801
|
+
if (i < n && u(0) == '(') {
|
|
802
|
+
i++;
|
|
803
|
+
|
|
804
|
+
err = bare_type_stripper__walk(ctx, &i, ')', false, false, depth + 1);
|
|
805
|
+
if (err < 0) return err;
|
|
806
|
+
|
|
807
|
+
if (i < n && u(0) == ')') i++;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
*result = i;
|
|
812
|
+
return 0;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// Process a function's parameter list. Expects s[i] == '('. Strips parameter
|
|
816
|
+
// modifiers (parameter properties), type annotations and optional '?' markers.
|
|
817
|
+
static inline int
|
|
818
|
+
bare_type_stripper__process_params(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
819
|
+
const utf8_t *s = ctx->s;
|
|
820
|
+
size_t n = ctx->n;
|
|
821
|
+
|
|
822
|
+
int err;
|
|
823
|
+
size_t i = *result;
|
|
824
|
+
|
|
825
|
+
if (u(0) != '(') {
|
|
826
|
+
*result = i;
|
|
827
|
+
return 0;
|
|
828
|
+
}
|
|
829
|
+
i++;
|
|
830
|
+
|
|
831
|
+
// 'this: T' parameter. TypeScript allows the first parameter to be named
|
|
832
|
+
// 'this' for typing the call context; it has no runtime presence and must
|
|
833
|
+
// be erased entirely (including its trailing comma if more params follow).
|
|
834
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, i);
|
|
835
|
+
if (bare_type_stripper__at_kw(s, n, after, "this", 4)) {
|
|
836
|
+
size_t this_end = after + 4;
|
|
837
|
+
size_t scan = bare_type_stripper__skip_trivia(s, n, this_end);
|
|
838
|
+
|
|
839
|
+
if (scan < n && (s[scan] == ':' || s[scan] == ',' || s[scan] == ')')) {
|
|
840
|
+
// Walk past the optional ': T' annotation.
|
|
841
|
+
if (s[scan] == ':') {
|
|
842
|
+
scan++;
|
|
843
|
+
scan = bare_type_stripper__scan_type(s, n, scan, BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_PAREN);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// Erase the 'this' parameter and the trailing comma when more
|
|
847
|
+
// parameters follow; if not, leave the closing ')' for the loop.
|
|
848
|
+
size_t strip_end = scan;
|
|
849
|
+
if (scan < n && s[scan] == ',') strip_end = scan + 1;
|
|
850
|
+
|
|
851
|
+
err = bare_type_stripper__add_range(ctx, i, strip_end);
|
|
852
|
+
if (err < 0) return err;
|
|
853
|
+
i = strip_end;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// Each iteration recognizes one feature (a modifier, a name, an optional
|
|
858
|
+
// marker, an annotation, a default, a comma, etc.) and 'continue's. Any
|
|
859
|
+
// unrecognized byte falls through to the bottom 'i++', which guarantees
|
|
860
|
+
// forward progress on malformed input without an explicit safeguard.
|
|
861
|
+
while (i < n) {
|
|
862
|
+
uint8_t ch = u(0);
|
|
863
|
+
|
|
864
|
+
if (ws(ch) || (ch == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*'))) {
|
|
865
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
if (ch == ')') {
|
|
870
|
+
i++;
|
|
871
|
+
*result = i;
|
|
872
|
+
return 0;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (ch == ',') {
|
|
876
|
+
i++;
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// Decorator on a parameter - leave the decorator itself alone (decorators
|
|
881
|
+
// emit runtime code) but walk its args so any TS inside gets stripped.
|
|
882
|
+
if (ch == '@') {
|
|
883
|
+
err = bare_type_stripper__process_decorator(ctx, &i, depth);
|
|
884
|
+
if (err < 0) return err;
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Rest parameter '...'
|
|
889
|
+
if (i + 2 < n && ch == '.' && u(1) == '.' && u(2) == '.') {
|
|
890
|
+
i += 3;
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// Destructuring pattern. Balance brackets but don't recurse - destructuring
|
|
895
|
+
// may contain nested type annotations only in obscure cases.
|
|
896
|
+
if (ch == '[' || ch == '{') {
|
|
897
|
+
uint8_t close = ch == '[' ? ']' : '}';
|
|
898
|
+
i = bare_type_stripper__scan_balanced(s, n, i, ch, close);
|
|
899
|
+
continue;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// Optional marker '?'
|
|
903
|
+
if (ch == '?') {
|
|
904
|
+
err = bare_type_stripper__add_range(ctx, i, i + 1);
|
|
905
|
+
if (err < 0) return err;
|
|
906
|
+
|
|
907
|
+
i++;
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// Type annotation ': T'
|
|
912
|
+
if (ch == ':') {
|
|
913
|
+
size_t ts = i;
|
|
914
|
+
i++;
|
|
915
|
+
i = bare_type_stripper__scan_type(
|
|
916
|
+
s,
|
|
917
|
+
n,
|
|
918
|
+
i,
|
|
919
|
+
BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_PAREN | BARE_TYPE_STRIPPER__T_EQ
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
923
|
+
if (err < 0) return err;
|
|
924
|
+
continue;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// Default value '=' - walk the expression, stopping at ',' or ')' at
|
|
928
|
+
// depth 0 (the loop will see whichever stop char comes next).
|
|
929
|
+
if (ch == '=') {
|
|
930
|
+
i++;
|
|
931
|
+
err = bare_type_stripper__walk(ctx, &i, ',', false, false, depth + 1);
|
|
932
|
+
if (err < 0) return err;
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// Identifier - either a TS modifier (public/private/protected/readonly/
|
|
937
|
+
// override) prefixing a parameter property, or the parameter name.
|
|
938
|
+
if (ids(ch)) {
|
|
939
|
+
size_t ks = i;
|
|
940
|
+
while (i < n && id(u(0))) i++;
|
|
941
|
+
size_t ke = i;
|
|
942
|
+
size_t kl = ke - ks;
|
|
943
|
+
|
|
944
|
+
if (bare_type_stripper__is_ts_modifier(s, ks, kl, false)) {
|
|
945
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
|
|
946
|
+
|
|
947
|
+
// Match the modifier only if followed by another modifier, an
|
|
948
|
+
// identifier, or a destructuring/rest opener - i.e. we're really at
|
|
949
|
+
// a parameter property and not just an identifier that happens to
|
|
950
|
+
// spell a modifier keyword. A parameter property is non-erasable:
|
|
951
|
+
// TypeScript assigns the parameter to a same-named instance member
|
|
952
|
+
// at construction time, which stripping the modifier can't
|
|
953
|
+
// reproduce, so mark it instead of erasing it.
|
|
954
|
+
if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '{' || (s[peek] == '.' && peek + 2 < n && s[peek + 1] == '.' && s[peek + 2] == '.'))) {
|
|
955
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, ke, bare_type_stripper_error);
|
|
956
|
+
if (err < 0) return err;
|
|
957
|
+
i = peek;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// Unrecognized byte - skip it so the loop can't wedge on malformed input.
|
|
964
|
+
i++;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
*result = i;
|
|
968
|
+
return 0;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
// Process the body of a class. Expects s[i] == '{'. Walks until matching '}'.
|
|
972
|
+
static inline int
|
|
973
|
+
bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
974
|
+
const utf8_t *s = ctx->s;
|
|
975
|
+
size_t n = ctx->n;
|
|
976
|
+
|
|
977
|
+
int err;
|
|
978
|
+
size_t i = *result;
|
|
979
|
+
|
|
980
|
+
if (u(0) != '{') return 0;
|
|
981
|
+
i++;
|
|
982
|
+
|
|
983
|
+
while (i < n) {
|
|
984
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
985
|
+
if (i >= n) break;
|
|
986
|
+
|
|
987
|
+
if (u(0) == '}') {
|
|
988
|
+
i++;
|
|
989
|
+
*result = i;
|
|
990
|
+
return 0;
|
|
991
|
+
}
|
|
992
|
+
if (u(0) == ';') {
|
|
993
|
+
i++;
|
|
994
|
+
continue;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
// Decorator on a member.
|
|
998
|
+
if (u(0) == '@') {
|
|
999
|
+
err = bare_type_stripper__process_decorator(ctx, &i, depth);
|
|
1000
|
+
if (err < 0) return err;
|
|
1001
|
+
continue;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
// 'static' is a real JS keyword that prefixes a class member, or starts
|
|
1005
|
+
// a static initialization block. Consume it when followed by another
|
|
1006
|
+
// identifier (or '['/'*') and remember that the line now has a syntactic
|
|
1007
|
+
// anchor: A subsequent TS modifier strip on the same line must NOT inject
|
|
1008
|
+
// ';' since 'static' already breaks any ASI hazard with the preceding
|
|
1009
|
+
// member ('f = 1\nstatic readonly x' parses cleanly, but 'f = 1\nstatic ;
|
|
1010
|
+
// x' would not). When followed by '{', recurse into the block as plain
|
|
1011
|
+
// JS - the contents are statements, not class members.
|
|
1012
|
+
bool kept_jskw = false;
|
|
1013
|
+
if (bare_type_stripper__at_kw(s, n, i, "static", 6)) {
|
|
1014
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i + 6);
|
|
1015
|
+
|
|
1016
|
+
if (peek < n && s[peek] == '{') {
|
|
1017
|
+
i = peek;
|
|
1018
|
+
|
|
1019
|
+
err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
|
|
1020
|
+
if (err < 0) return err;
|
|
1021
|
+
|
|
1022
|
+
continue;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '*')) {
|
|
1026
|
+
i = i + 6;
|
|
1027
|
+
kept_jskw = true;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// Track whether we've already emitted a strip range that starts this
|
|
1032
|
+
// class member. The first such range gets the F_SEMI flag so that ASI
|
|
1033
|
+
// can't merge the preceding field's initializer into this one (e.g.
|
|
1034
|
+
// 'field = 1\npublic ["k"] = 2' would parse as '1["k"]' after stripping
|
|
1035
|
+
// 'public' without an injected ';').
|
|
1036
|
+
bool member_strip_seen = kept_jskw;
|
|
1037
|
+
|
|
1038
|
+
// Strip TS modifier keywords. Loop as long as we keep seeing modifiers
|
|
1039
|
+
// followed by another modifier or a member name.
|
|
1040
|
+
for (;;) {
|
|
1041
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1042
|
+
if (i >= n || !ids(u(0))) break;
|
|
1043
|
+
|
|
1044
|
+
size_t ks = i;
|
|
1045
|
+
while (i < n && id(u(0))) i++;
|
|
1046
|
+
size_t ke = i;
|
|
1047
|
+
size_t kl = ke - ks;
|
|
1048
|
+
|
|
1049
|
+
bool is_mod = bare_type_stripper__is_ts_modifier(s, ks, kl, true);
|
|
1050
|
+
|
|
1051
|
+
if (!is_mod) {
|
|
1052
|
+
i = ks;
|
|
1053
|
+
break;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
|
|
1057
|
+
|
|
1058
|
+
// Erase modifier only if followed by another identifier (member name
|
|
1059
|
+
// or another modifier), '[' (computed name), or '*' (generator).
|
|
1060
|
+
if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '*')) {
|
|
1061
|
+
uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
|
|
1062
|
+
|
|
1063
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, peek, flags);
|
|
1064
|
+
if (err < 0) return err;
|
|
1065
|
+
|
|
1066
|
+
member_strip_seen = true;
|
|
1067
|
+
i = peek;
|
|
1068
|
+
continue;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
i = ks;
|
|
1072
|
+
break;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// 'declare' member - erase to end of statement. scan_declare balances
|
|
1076
|
+
// brackets, so an object type in the annotation can't end the strip at
|
|
1077
|
+
// its inner '}'.
|
|
1078
|
+
if (bare_type_stripper__at_kw(s, n, i, "declare", 7)) {
|
|
1079
|
+
size_t ds = i;
|
|
1080
|
+
i = bare_type_stripper__scan_declare(s, n, i + 7);
|
|
1081
|
+
|
|
1082
|
+
uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
|
|
1083
|
+
|
|
1084
|
+
err = bare_type_stripper__add_range_flags(ctx, ds, i, flags);
|
|
1085
|
+
if (err < 0) return err;
|
|
1086
|
+
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
// Index signature '[k: K]: V'
|
|
1091
|
+
if (u(0) == '[') {
|
|
1092
|
+
size_t bs = i;
|
|
1093
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
|
|
1094
|
+
|
|
1095
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, i);
|
|
1096
|
+
if (after < n && s[after] == ':') {
|
|
1097
|
+
// Index signature - erase entire signature.
|
|
1098
|
+
i = after + 1;
|
|
1099
|
+
i = bare_type_stripper__scan_type(
|
|
1100
|
+
s,
|
|
1101
|
+
n,
|
|
1102
|
+
i,
|
|
1103
|
+
BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_COMMA
|
|
1104
|
+
);
|
|
1105
|
+
if (i < n && (u(0) == ';' || u(0) == ',')) i++;
|
|
1106
|
+
|
|
1107
|
+
err = bare_type_stripper__add_range(ctx, bs, i);
|
|
1108
|
+
if (err < 0) return err;
|
|
1109
|
+
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// Otherwise it's a computed member name; fall through.
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
// Member name.
|
|
1117
|
+
if (u(0) == '*') {
|
|
1118
|
+
i++;
|
|
1119
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1120
|
+
}
|
|
1121
|
+
if (u(0) == '#') i++; // Private name
|
|
1122
|
+
|
|
1123
|
+
if (i < n && ids(u(0))) {
|
|
1124
|
+
while (i < n && id(u(0))) i++;
|
|
1125
|
+
} else if (u(0) == '\'' || u(0) == '"') {
|
|
1126
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1127
|
+
} else if (u(0) >= '0' && u(0) <= '9') {
|
|
1128
|
+
while (i < n && (id(u(0)) || u(0) == '.')) i++;
|
|
1129
|
+
} else {
|
|
1130
|
+
// Unknown - advance one byte to recover.
|
|
1131
|
+
i++;
|
|
1132
|
+
continue;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1136
|
+
|
|
1137
|
+
// Optional '?' or definite assignment '!:' on a property.
|
|
1138
|
+
if (i < n && u(0) == '?') {
|
|
1139
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
|
|
1140
|
+
if (peek < n && (s[peek] == ':' || s[peek] == '(' || s[peek] == ';' || s[peek] == '=' || s[peek] == '}' || lt(s[peek]) || s[peek] == ',')) {
|
|
1141
|
+
err = bare_type_stripper__add_range(ctx, i, i + 1);
|
|
1142
|
+
if (err < 0) return err;
|
|
1143
|
+
|
|
1144
|
+
i++;
|
|
1145
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1146
|
+
}
|
|
1147
|
+
} else if (i < n && u(0) == '!') {
|
|
1148
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
|
|
1149
|
+
if (peek < n && s[peek] == ':') {
|
|
1150
|
+
err = bare_type_stripper__add_range(ctx, i, i + 1);
|
|
1151
|
+
if (err < 0) return err;
|
|
1152
|
+
|
|
1153
|
+
i++;
|
|
1154
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// Method generics '<T>'.
|
|
1159
|
+
if (i < n && u(0) == '<') {
|
|
1160
|
+
size_t gs = i;
|
|
1161
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, i);
|
|
1162
|
+
|
|
1163
|
+
if (ge > i + 1) {
|
|
1164
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
|
|
1165
|
+
if (peek < n && s[peek] == '(') {
|
|
1166
|
+
err = bare_type_stripper__add_range(ctx, gs, ge);
|
|
1167
|
+
if (err < 0) return err;
|
|
1168
|
+
|
|
1169
|
+
i = ge;
|
|
1170
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
if (i >= n) break;
|
|
1176
|
+
|
|
1177
|
+
if (u(0) == '(') {
|
|
1178
|
+
// Method.
|
|
1179
|
+
err = bare_type_stripper__process_params(ctx, &i, depth);
|
|
1180
|
+
if (err < 0) return err;
|
|
1181
|
+
|
|
1182
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1183
|
+
|
|
1184
|
+
// Return type annotation.
|
|
1185
|
+
if (i < n && u(0) == ':') {
|
|
1186
|
+
size_t ts = i;
|
|
1187
|
+
i++;
|
|
1188
|
+
i = bare_type_stripper__scan_type(
|
|
1189
|
+
s,
|
|
1190
|
+
n,
|
|
1191
|
+
i,
|
|
1192
|
+
BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
|
|
1193
|
+
);
|
|
1194
|
+
|
|
1195
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
1196
|
+
if (err < 0) return err;
|
|
1197
|
+
|
|
1198
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// Method body (or just ';' for overload).
|
|
1202
|
+
if (i < n && u(0) == '{') {
|
|
1203
|
+
err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
|
|
1204
|
+
if (err < 0) return err;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
continue;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
if (u(0) == ':') {
|
|
1211
|
+
// Property type annotation.
|
|
1212
|
+
size_t ts = i;
|
|
1213
|
+
i++;
|
|
1214
|
+
i = bare_type_stripper__scan_type(
|
|
1215
|
+
s,
|
|
1216
|
+
n,
|
|
1217
|
+
i,
|
|
1218
|
+
BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_EQ | BARE_TYPE_STRIPPER__T_NL | BARE_TYPE_STRIPPER__T_BRACE
|
|
1219
|
+
);
|
|
1220
|
+
|
|
1221
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
1222
|
+
if (err < 0) return err;
|
|
1223
|
+
|
|
1224
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// Initializer. Pass stop_at_asi so the walker stops at a newline at
|
|
1228
|
+
// depth 0 (after an expression-ending token) - otherwise it would chew
|
|
1229
|
+
// past the next class element when the field has no trailing ';'.
|
|
1230
|
+
if (i < n && u(0) == '=') {
|
|
1231
|
+
i++;
|
|
1232
|
+
|
|
1233
|
+
err = bare_type_stripper__walk(ctx, &i, ';', false, true, depth + 1);
|
|
1234
|
+
if (err < 0) return err;
|
|
1235
|
+
|
|
1236
|
+
if (i < n && u(0) == ';') i++;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
*result = i;
|
|
1241
|
+
return 0;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
// Scan past an 'enum IDENT { ... }' construct. Assumes s[i] points at the
|
|
1245
|
+
// 'enum' keyword. Returns the index after the matching '}'. Used to mark
|
|
1246
|
+
// the whole construct as a non-erasable range.
|
|
1247
|
+
static inline size_t
|
|
1248
|
+
bare_type_stripper__scan_enum(const utf8_t *s, size_t n, size_t i) {
|
|
1249
|
+
i = bare_type_stripper__skip_name(s, n, i + 4);
|
|
1250
|
+
|
|
1251
|
+
if (i < n && u(0) == '{') {
|
|
1252
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
return i;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
// Scan past an 'interface IDENT [<...>] [extends ...] { ... }' construct
|
|
1259
|
+
// without emitting any strip ranges. Assumes s[i] points at 'interface'.
|
|
1260
|
+
// Returns the index after the matching '}'.
|
|
1261
|
+
static inline size_t
|
|
1262
|
+
bare_type_stripper__scan_interface(const utf8_t *s, size_t n, size_t i) {
|
|
1263
|
+
i = bare_type_stripper__skip_name(s, n, i + 9);
|
|
1264
|
+
|
|
1265
|
+
if (i < n && u(0) == '<') {
|
|
1266
|
+
i = bare_type_stripper__scan_generic(s, n, i);
|
|
1267
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
// Optional 'extends X, Y, ...'.
|
|
1271
|
+
if (bare_type_stripper__at_kw(s, n, i, "extends", 7)) {
|
|
1272
|
+
i += 7;
|
|
1273
|
+
int depth = 0;
|
|
1274
|
+
|
|
1275
|
+
while (i < n) {
|
|
1276
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
1277
|
+
|
|
1278
|
+
uint8_t cc = u(0);
|
|
1279
|
+
|
|
1280
|
+
if (cc == '<' || cc == '(' || cc == '[') {
|
|
1281
|
+
depth++;
|
|
1282
|
+
i++;
|
|
1283
|
+
continue;
|
|
1284
|
+
}
|
|
1285
|
+
if (cc == '>' || cc == ')' || cc == ']') {
|
|
1286
|
+
if (depth == 0) break;
|
|
1287
|
+
depth--;
|
|
1288
|
+
i++;
|
|
1289
|
+
continue;
|
|
1290
|
+
}
|
|
1291
|
+
if (cc == '{' && depth == 0) break;
|
|
1292
|
+
|
|
1293
|
+
i++;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
if (i < n && u(0) == '{') {
|
|
1298
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
return i;
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
// Process a top-level 'interface IDENT [...] { ... }' declaration. Emits a
|
|
1305
|
+
// single SEMI-flagged range covering the whole construct.
|
|
1306
|
+
static inline int
|
|
1307
|
+
bare_type_stripper__process_interface(bare_type_stripper_t *ctx, size_t *result) {
|
|
1308
|
+
const utf8_t *s = ctx->s;
|
|
1309
|
+
size_t n = ctx->n;
|
|
1310
|
+
|
|
1311
|
+
int err;
|
|
1312
|
+
size_t start = *result;
|
|
1313
|
+
size_t end = bare_type_stripper__scan_interface(s, n, start);
|
|
1314
|
+
|
|
1315
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1316
|
+
if (err < 0) return err;
|
|
1317
|
+
|
|
1318
|
+
*result = end;
|
|
1319
|
+
return 0;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// Scan past the remainder of a 'type IDENT [<...>] = T;' alias. On entry, i
|
|
1323
|
+
// points just past the 'type' keyword. Returns the index past the alias and
|
|
1324
|
+
// its terminating ';' when present.
|
|
1325
|
+
static inline size_t
|
|
1326
|
+
bare_type_stripper__scan_type_alias(const utf8_t *s, size_t n, size_t i) {
|
|
1327
|
+
i = bare_type_stripper__skip_name(s, n, i);
|
|
1328
|
+
|
|
1329
|
+
if (i < n && u(0) == '<') {
|
|
1330
|
+
i = bare_type_stripper__scan_generic(s, n, i);
|
|
1331
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
if (i < n && u(0) == '=') {
|
|
1335
|
+
i++;
|
|
1336
|
+
i = bare_type_stripper__scan_type(s, n, i, BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL);
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
if (i < n && u(0) == ';') i++;
|
|
1340
|
+
|
|
1341
|
+
return i;
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
// Process 'type IDENT [<...>] = T;'. Emits the whole declaration as a range.
|
|
1345
|
+
static inline int
|
|
1346
|
+
bare_type_stripper__process_type_alias(bare_type_stripper_t *ctx, size_t *result) {
|
|
1347
|
+
int err;
|
|
1348
|
+
size_t start = *result;
|
|
1349
|
+
size_t end = bare_type_stripper__scan_type_alias(ctx->s, ctx->n, start + 4);
|
|
1350
|
+
|
|
1351
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1352
|
+
if (err < 0) return err;
|
|
1353
|
+
|
|
1354
|
+
*result = end;
|
|
1355
|
+
return 0;
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
// Scan past the remainder of a 'declare ...' construct. On entry, i points
|
|
1359
|
+
// just past the 'declare' keyword. Returns the index past the declared body
|
|
1360
|
+
// or the terminating ';' / newline.
|
|
1361
|
+
static inline size_t
|
|
1362
|
+
bare_type_stripper__scan_declare(const utf8_t *s, size_t n, size_t i) {
|
|
1363
|
+
// Balance '()'/'[]'/'{}' uniformly, but only treat a closing '}' at outer
|
|
1364
|
+
// depth as the end of the declared body. This matters for 'declare function
|
|
1365
|
+
// f(): void' (no body): The ')' of params closes at depth 0 but isn't a
|
|
1366
|
+
// body, so we keep scanning past the return type until a ';' or newline.
|
|
1367
|
+
int depth = 0;
|
|
1368
|
+
|
|
1369
|
+
while (i < n) {
|
|
1370
|
+
uint8_t cc = u(0);
|
|
1371
|
+
|
|
1372
|
+
// A newline at depth 0 ends the construct, so check it before the
|
|
1373
|
+
// generic trivia skip (which would consume it).
|
|
1374
|
+
if (lt(cc) && depth == 0) break;
|
|
1375
|
+
|
|
1376
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
1377
|
+
|
|
1378
|
+
if (cc == '{' || cc == '(' || cc == '[') {
|
|
1379
|
+
depth++;
|
|
1380
|
+
i++;
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
if (cc == '}' || cc == ')' || cc == ']') {
|
|
1384
|
+
if (depth == 0) break;
|
|
1385
|
+
depth--;
|
|
1386
|
+
i++;
|
|
1387
|
+
if (depth == 0 && cc == '}') break;
|
|
1388
|
+
continue;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
if (cc == ';' && depth == 0) {
|
|
1392
|
+
i++;
|
|
1393
|
+
break;
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
i++;
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
return i;
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
// Process 'declare ...' at statement position.
|
|
1403
|
+
static inline int
|
|
1404
|
+
bare_type_stripper__process_declare(bare_type_stripper_t *ctx, size_t *result) {
|
|
1405
|
+
const utf8_t *s = ctx->s;
|
|
1406
|
+
size_t n = ctx->n;
|
|
1407
|
+
|
|
1408
|
+
int err;
|
|
1409
|
+
size_t start = *result;
|
|
1410
|
+
size_t end = bare_type_stripper__scan_declare(s, n, start + 7);
|
|
1411
|
+
|
|
1412
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1413
|
+
if (err < 0) return err;
|
|
1414
|
+
|
|
1415
|
+
*result = end;
|
|
1416
|
+
return 0;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// Process the contents of an import or export named specifier list, the
|
|
1420
|
+
// '{ ... }' body. Expects s[i] == '{'. Erases type-only specifiers ('type
|
|
1421
|
+
// Foo', 'type Foo as Bar') entirely, including the separating comma so the
|
|
1422
|
+
// remaining value specifiers stay syntactically valid. Leaves cursor just
|
|
1423
|
+
// past the matching '}'.
|
|
1424
|
+
//
|
|
1425
|
+
// `allow_star` is true for import lists - which can be parsed by callers
|
|
1426
|
+
// that also want to handle 'import * as X from "..."' (the '*' itself is at
|
|
1427
|
+
// outer scope, but a defensive 'type *' check stays consistent there) - and
|
|
1428
|
+
// false for export lists where '*' isn't allowed inside the braces.
|
|
1429
|
+
static inline int
|
|
1430
|
+
bare_type_stripper__process_named_specifiers(bare_type_stripper_t *ctx, size_t *result, bool allow_star) {
|
|
1431
|
+
const utf8_t *s = ctx->s;
|
|
1432
|
+
size_t n = ctx->n;
|
|
1433
|
+
|
|
1434
|
+
int err;
|
|
1435
|
+
size_t i = *result;
|
|
1436
|
+
|
|
1437
|
+
if (i >= n || u(0) != '{') return 0;
|
|
1438
|
+
i++;
|
|
1439
|
+
|
|
1440
|
+
// Position just after the last kept (non-type) specifier's content, before
|
|
1441
|
+
// its trailing comma if any. Used to absorb the preceding comma when the
|
|
1442
|
+
// final specifier in the list is type-only.
|
|
1443
|
+
size_t prev_kept_end = i;
|
|
1444
|
+
|
|
1445
|
+
while (i < n) {
|
|
1446
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1447
|
+
if (i >= n) break;
|
|
1448
|
+
if (u(0) == '}') break;
|
|
1449
|
+
|
|
1450
|
+
size_t spec_start = i;
|
|
1451
|
+
bool is_type = false;
|
|
1452
|
+
|
|
1453
|
+
if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
|
|
1454
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, i + 4);
|
|
1455
|
+
if (after < n && (ids(s[after]) || (allow_star && s[after] == '*'))) {
|
|
1456
|
+
is_type = true;
|
|
1457
|
+
i = after;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
// Specifier name is either an identifier, '*' (import only), or a string
|
|
1462
|
+
// literal (arbitrary module namespace names, e.g. 'import { "x" as C }').
|
|
1463
|
+
if (i < n && (u(0) == '\'' || u(0) == '"')) {
|
|
1464
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1465
|
+
} else if (allow_star && i < n && u(0) == '*') {
|
|
1466
|
+
i++;
|
|
1467
|
+
} else {
|
|
1468
|
+
while (i < n && id(u(0))) i++;
|
|
1469
|
+
}
|
|
1470
|
+
size_t spec_end = i;
|
|
1471
|
+
|
|
1472
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1473
|
+
if (bare_type_stripper__at_kw(s, n, i, "as", 2)) {
|
|
1474
|
+
i += 2;
|
|
1475
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1476
|
+
if (i < n && (u(0) == '\'' || u(0) == '"')) {
|
|
1477
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1478
|
+
} else {
|
|
1479
|
+
while (i < n && id(u(0))) i++;
|
|
1480
|
+
}
|
|
1481
|
+
spec_end = i;
|
|
1482
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
bool has_comma = false;
|
|
1486
|
+
size_t after_comma = spec_end;
|
|
1487
|
+
if (i < n && u(0) == ',') {
|
|
1488
|
+
has_comma = true;
|
|
1489
|
+
i++;
|
|
1490
|
+
after_comma = i;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
if (is_type) {
|
|
1494
|
+
size_t strip_start = has_comma ? spec_start : prev_kept_end;
|
|
1495
|
+
|
|
1496
|
+
err = bare_type_stripper__add_range(ctx, strip_start, after_comma);
|
|
1497
|
+
if (err < 0) return err;
|
|
1498
|
+
} else {
|
|
1499
|
+
prev_kept_end = spec_end;
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
if (i < n && u(0) == '}') i++;
|
|
1504
|
+
|
|
1505
|
+
*result = i;
|
|
1506
|
+
return 0;
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
// Process 'import' at statement position. Handles 'import type ...' and
|
|
1510
|
+
// 'import { type X, ... }'. Leaves dynamic import and import.meta alone.
|
|
1511
|
+
static inline int
|
|
1512
|
+
bare_type_stripper__process_import(bare_type_stripper_t *ctx, size_t *result) {
|
|
1513
|
+
const utf8_t *s = ctx->s;
|
|
1514
|
+
size_t n = ctx->n;
|
|
1515
|
+
|
|
1516
|
+
int err;
|
|
1517
|
+
size_t i = *result;
|
|
1518
|
+
size_t start = i;
|
|
1519
|
+
i += 6;
|
|
1520
|
+
|
|
1521
|
+
size_t after_kw = bare_type_stripper__skip_trivia(s, n, i);
|
|
1522
|
+
if (after_kw < n && (s[after_kw] == '(' || s[after_kw] == '.')) {
|
|
1523
|
+
*result = i;
|
|
1524
|
+
return 0;
|
|
1525
|
+
}
|
|
1526
|
+
i = after_kw;
|
|
1527
|
+
|
|
1528
|
+
// 'import type ...' - erase whole statement.
|
|
1529
|
+
if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
|
|
1530
|
+
i += 4;
|
|
1531
|
+
|
|
1532
|
+
while (i < n) {
|
|
1533
|
+
uint8_t cc = u(0);
|
|
1534
|
+
if (cc == '\'' || cc == '"') {
|
|
1535
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1536
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i);
|
|
1537
|
+
if (peek < n && s[peek] == ';') i = peek + 1;
|
|
1538
|
+
break;
|
|
1539
|
+
}
|
|
1540
|
+
if (cc == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
1541
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1542
|
+
continue;
|
|
1543
|
+
}
|
|
1544
|
+
if (cc == ';') {
|
|
1545
|
+
i++;
|
|
1546
|
+
break;
|
|
1547
|
+
}
|
|
1548
|
+
i++;
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
|
|
1552
|
+
if (err < 0) return err;
|
|
1553
|
+
|
|
1554
|
+
*result = i;
|
|
1555
|
+
return 0;
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
// Walk the import clause looking for '{...}' named list. Type-only
|
|
1559
|
+
// specifiers ('type Foo', 'type Foo as Bar') are erased entirely along
|
|
1560
|
+
// with their separating comma, since the named binding would otherwise be
|
|
1561
|
+
// requested as a value at runtime and fail.
|
|
1562
|
+
while (i < n) {
|
|
1563
|
+
uint8_t cc = u(0);
|
|
1564
|
+
if (ws(cc)) {
|
|
1565
|
+
i++;
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
if (cc == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
1569
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1570
|
+
continue;
|
|
1571
|
+
}
|
|
1572
|
+
if (cc == '\'' || cc == '"') {
|
|
1573
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1574
|
+
break;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
if (cc == '{') {
|
|
1578
|
+
err = bare_type_stripper__process_named_specifiers(ctx, &i, true);
|
|
1579
|
+
if (err < 0) return err;
|
|
1580
|
+
continue;
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
if (cc == ';') {
|
|
1584
|
+
i++;
|
|
1585
|
+
break;
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
i++;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
*result = i;
|
|
1592
|
+
return 0;
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
// Process 'export' at statement position.
|
|
1596
|
+
static inline int
|
|
1597
|
+
bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result) {
|
|
1598
|
+
const utf8_t *s = ctx->s;
|
|
1599
|
+
size_t n = ctx->n;
|
|
1600
|
+
|
|
1601
|
+
int err;
|
|
1602
|
+
size_t i = *result;
|
|
1603
|
+
size_t start = i;
|
|
1604
|
+
i += 6;
|
|
1605
|
+
|
|
1606
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1607
|
+
if (i >= n) {
|
|
1608
|
+
*result = i;
|
|
1609
|
+
return 0;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// 'export type ...' - either an exported alias ('export type Foo = ...')
|
|
1613
|
+
// or a type-only re-export ('export type { ... } from ...', 'export type
|
|
1614
|
+
// * as NS from ...'). Both are erased whole, including 'export'.
|
|
1615
|
+
if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
|
|
1616
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, i + 4);
|
|
1617
|
+
|
|
1618
|
+
// Alias form - delegate to the type-alias scanner so multi-line types
|
|
1619
|
+
// behave exactly like a plain 'type' declaration.
|
|
1620
|
+
if (after < n && ids(s[after])) {
|
|
1621
|
+
size_t end = bare_type_stripper__scan_type_alias(s, n, i + 4);
|
|
1622
|
+
|
|
1623
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1624
|
+
if (err < 0) return err;
|
|
1625
|
+
|
|
1626
|
+
*result = end;
|
|
1627
|
+
return 0;
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
// Re-export form - scan the specifier clause, the optional 'from'
|
|
1631
|
+
// clause, and the terminating ';'.
|
|
1632
|
+
if (after < n && (s[after] == '{' || s[after] == '*')) {
|
|
1633
|
+
i = after;
|
|
1634
|
+
|
|
1635
|
+
if (u(0) == '{') {
|
|
1636
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
|
|
1637
|
+
} else {
|
|
1638
|
+
// '* [as NS]'.
|
|
1639
|
+
i++;
|
|
1640
|
+
|
|
1641
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, i);
|
|
1642
|
+
if (bare_type_stripper__at_kw(s, n, p, "as", 2)) {
|
|
1643
|
+
p = bare_type_stripper__skip_trivia(s, n, p + 2);
|
|
1644
|
+
while (p < n && id(s[p])) p++;
|
|
1645
|
+
i = p;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, i);
|
|
1650
|
+
if (bare_type_stripper__at_kw(s, n, p, "from", 4)) {
|
|
1651
|
+
p = bare_type_stripper__skip_trivia(s, n, p + 4);
|
|
1652
|
+
if (p < n && (s[p] == '\'' || s[p] == '"')) {
|
|
1653
|
+
i = bare_type_stripper__skip_string(s, n, p);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
size_t semi = bare_type_stripper__skip_trivia(s, n, i);
|
|
1658
|
+
if (semi < n && s[semi] == ';') i = semi + 1;
|
|
1659
|
+
|
|
1660
|
+
err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
|
|
1661
|
+
if (err < 0) return err;
|
|
1662
|
+
|
|
1663
|
+
*result = i;
|
|
1664
|
+
return 0;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// 'export declare ...' - the whole statement, including 'export', is
|
|
1669
|
+
// ambient and erased as one range; stripping only from 'declare' onward
|
|
1670
|
+
// would leave an invalid bare 'export ;' behind.
|
|
1671
|
+
if (bare_type_stripper__at_kw(s, n, i, "declare", 7)) {
|
|
1672
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, i + 7);
|
|
1673
|
+
if (after < n && ids(s[after])) {
|
|
1674
|
+
size_t end = bare_type_stripper__scan_declare(s, n, i + 7);
|
|
1675
|
+
|
|
1676
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1677
|
+
if (err < 0) return err;
|
|
1678
|
+
|
|
1679
|
+
*result = end;
|
|
1680
|
+
return 0;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
// 'export interface ...' - erase as one range covering 'export ' too.
|
|
1685
|
+
if (bare_type_stripper__at_kw(s, n, i, "interface", 9)) {
|
|
1686
|
+
size_t end = bare_type_stripper__scan_interface(s, n, i);
|
|
1687
|
+
|
|
1688
|
+
err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
|
|
1689
|
+
if (err < 0) return err;
|
|
1690
|
+
|
|
1691
|
+
*result = end;
|
|
1692
|
+
return 0;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// 'export { ... }' - erase type-only specifiers entirely, including the
|
|
1696
|
+
// comma that separates them from value specifiers.
|
|
1697
|
+
if (u(0) == '{') {
|
|
1698
|
+
err = bare_type_stripper__process_named_specifiers(ctx, &i, false);
|
|
1699
|
+
if (err < 0) return err;
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
*result = i;
|
|
1703
|
+
return 0;
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
// Process a variable declaration sequence (the right-hand side of const/let/var).
|
|
1707
|
+
// On entry, i points just past the keyword.
|
|
1708
|
+
static inline int
|
|
1709
|
+
bare_type_stripper__process_var_decl(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
1710
|
+
const utf8_t *s = ctx->s;
|
|
1711
|
+
size_t n = ctx->n;
|
|
1712
|
+
|
|
1713
|
+
int err;
|
|
1714
|
+
size_t i = *result;
|
|
1715
|
+
|
|
1716
|
+
while (i < n) {
|
|
1717
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1718
|
+
if (i >= n) break;
|
|
1719
|
+
|
|
1720
|
+
// Destructuring pattern or identifier.
|
|
1721
|
+
if (u(0) == '{' || u(0) == '[') {
|
|
1722
|
+
uint8_t open = u(0);
|
|
1723
|
+
uint8_t close = open == '{' ? '}' : ']';
|
|
1724
|
+
i = bare_type_stripper__scan_balanced(s, n, i, open, close);
|
|
1725
|
+
} else if (ids(u(0))) {
|
|
1726
|
+
while (i < n && id(u(0))) i++;
|
|
1727
|
+
} else {
|
|
1728
|
+
break;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1732
|
+
|
|
1733
|
+
// Definite assignment '!:'.
|
|
1734
|
+
if (i < n && u(0) == '!') {
|
|
1735
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
|
|
1736
|
+
if (peek < n && s[peek] == ':') {
|
|
1737
|
+
err = bare_type_stripper__add_range(ctx, i, i + 1);
|
|
1738
|
+
if (err < 0) return err;
|
|
1739
|
+
|
|
1740
|
+
i++;
|
|
1741
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
// Type annotation ': T'. T_INOF stops the scan at a bare 'in'/'of'
|
|
1746
|
+
// identifier - needed for 'for (const x: T of arr)' headers, harmless
|
|
1747
|
+
// elsewhere since neither word appears at depth 0 of a valid type.
|
|
1748
|
+
if (i < n && u(0) == ':') {
|
|
1749
|
+
size_t ts = i;
|
|
1750
|
+
i++;
|
|
1751
|
+
i = bare_type_stripper__scan_type(
|
|
1752
|
+
s,
|
|
1753
|
+
n,
|
|
1754
|
+
i,
|
|
1755
|
+
BARE_TYPE_STRIPPER__T_EQ | BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL | BARE_TYPE_STRIPPER__T_INOF
|
|
1756
|
+
);
|
|
1757
|
+
|
|
1758
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
1759
|
+
if (err < 0) return err;
|
|
1760
|
+
|
|
1761
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
// Initializer. stop_at_asi keeps the walker from chewing past a newline
|
|
1765
|
+
// into the next statement when the declaration has no trailing ';',
|
|
1766
|
+
// which would otherwise miss type annotations on the following decl.
|
|
1767
|
+
if (i < n && u(0) == '=') {
|
|
1768
|
+
i++;
|
|
1769
|
+
|
|
1770
|
+
err = bare_type_stripper__walk(ctx, &i, ';', false, true, depth + 1);
|
|
1771
|
+
if (err < 0) return err;
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1775
|
+
if (i < n && u(0) == ',') {
|
|
1776
|
+
i++;
|
|
1777
|
+
continue;
|
|
1778
|
+
}
|
|
1779
|
+
break;
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
*result = i;
|
|
1783
|
+
return 0;
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
// Process 'function ...' or arrow function whose params we're entering.
|
|
1787
|
+
// On entry, i points at 'function'.
|
|
1788
|
+
static inline int
|
|
1789
|
+
bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
1790
|
+
const utf8_t *s = ctx->s;
|
|
1791
|
+
size_t n = ctx->n;
|
|
1792
|
+
|
|
1793
|
+
int err;
|
|
1794
|
+
size_t start = *result;
|
|
1795
|
+
size_t i = *result;
|
|
1796
|
+
i += 8;
|
|
1797
|
+
|
|
1798
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1799
|
+
if (i < n && u(0) == '*') {
|
|
1800
|
+
i++;
|
|
1801
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
i = bare_type_stripper__skip_name(s, n, i);
|
|
1805
|
+
|
|
1806
|
+
err = bare_type_stripper__strip_generic(ctx, &i);
|
|
1807
|
+
if (err < 0) return err;
|
|
1808
|
+
|
|
1809
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1810
|
+
|
|
1811
|
+
if (i < n && u(0) == '(') {
|
|
1812
|
+
err = bare_type_stripper__process_params(ctx, &i, depth);
|
|
1813
|
+
if (err < 0) return err;
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1817
|
+
|
|
1818
|
+
if (i < n && u(0) == ':') {
|
|
1819
|
+
size_t ts = i;
|
|
1820
|
+
i++;
|
|
1821
|
+
i = bare_type_stripper__scan_type(
|
|
1822
|
+
s,
|
|
1823
|
+
n,
|
|
1824
|
+
i,
|
|
1825
|
+
BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
|
|
1826
|
+
);
|
|
1827
|
+
|
|
1828
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
1829
|
+
if (err < 0) return err;
|
|
1830
|
+
|
|
1831
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
if (i < n && u(0) == '{') {
|
|
1835
|
+
err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
|
|
1836
|
+
if (err < 0) return err;
|
|
1837
|
+
} else {
|
|
1838
|
+
// Overload signature: A function declaration without a body, terminated
|
|
1839
|
+
// by ';'. Strip the whole signature line - leaving 'function f();' would
|
|
1840
|
+
// produce invalid runtime JS.
|
|
1841
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, i);
|
|
1842
|
+
if (peek < n && s[peek] == ';') {
|
|
1843
|
+
i = peek + 1;
|
|
1844
|
+
|
|
1845
|
+
err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
|
|
1846
|
+
if (err < 0) return err;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
*result = i;
|
|
1851
|
+
return 0;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// Process 'class ...'. On entry, i points at 'class'.
|
|
1855
|
+
static inline int
|
|
1856
|
+
bare_type_stripper__process_class(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
1857
|
+
const utf8_t *s = ctx->s;
|
|
1858
|
+
size_t n = ctx->n;
|
|
1859
|
+
|
|
1860
|
+
int err;
|
|
1861
|
+
size_t i = *result;
|
|
1862
|
+
|
|
1863
|
+
i = bare_type_stripper__skip_name(s, n, i + 5);
|
|
1864
|
+
|
|
1865
|
+
err = bare_type_stripper__strip_generic(ctx, &i);
|
|
1866
|
+
if (err < 0) return err;
|
|
1867
|
+
|
|
1868
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1869
|
+
|
|
1870
|
+
// 'extends X[<...>]' - keep but strip generic arguments and 'implements'.
|
|
1871
|
+
for (;;) {
|
|
1872
|
+
if (bare_type_stripper__at_kw(s, n, i, "extends", 7)) {
|
|
1873
|
+
i += 7;
|
|
1874
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1875
|
+
|
|
1876
|
+
// Skip the expression to the next significant boundary, but strip any
|
|
1877
|
+
// '<...>' generic arguments that appear at top level.
|
|
1878
|
+
int depth = 0;
|
|
1879
|
+
|
|
1880
|
+
while (i < n) {
|
|
1881
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
1882
|
+
|
|
1883
|
+
uint8_t cc = u(0);
|
|
1884
|
+
|
|
1885
|
+
if (depth == 0 && cc == '<') {
|
|
1886
|
+
size_t before = i;
|
|
1887
|
+
|
|
1888
|
+
err = bare_type_stripper__strip_generic(ctx, &i);
|
|
1889
|
+
if (err < 0) return err;
|
|
1890
|
+
|
|
1891
|
+
if (i == before) i++;
|
|
1892
|
+
continue;
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
if (cc == '(' || cc == '[' || cc == '{') {
|
|
1896
|
+
if (depth == 0 && cc == '{') break;
|
|
1897
|
+
depth++;
|
|
1898
|
+
i++;
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
if (cc == ')' || cc == ']' || cc == '}') {
|
|
1902
|
+
if (depth == 0) break;
|
|
1903
|
+
depth--;
|
|
1904
|
+
i++;
|
|
1905
|
+
continue;
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
if (depth == 0 && cc == ',') {
|
|
1909
|
+
i++;
|
|
1910
|
+
break;
|
|
1911
|
+
}
|
|
1912
|
+
if (depth == 0 && ids(cc)) {
|
|
1913
|
+
// Could be 'implements' keyword.
|
|
1914
|
+
if (bare_type_stripper__at_kw(s, n, i, "implements", 10)) break;
|
|
1915
|
+
while (i < n && id(u(0))) i++;
|
|
1916
|
+
continue;
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
i++;
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1923
|
+
continue;
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
if (bare_type_stripper__at_kw(s, n, i, "implements", 10)) {
|
|
1927
|
+
size_t ims = i;
|
|
1928
|
+
i += 10;
|
|
1929
|
+
int depth = 0;
|
|
1930
|
+
|
|
1931
|
+
while (i < n) {
|
|
1932
|
+
if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
|
|
1933
|
+
|
|
1934
|
+
uint8_t cc = u(0);
|
|
1935
|
+
|
|
1936
|
+
if (cc == '<' || cc == '(' || cc == '[') {
|
|
1937
|
+
depth++;
|
|
1938
|
+
i++;
|
|
1939
|
+
continue;
|
|
1940
|
+
}
|
|
1941
|
+
if (cc == '>' || cc == ')' || cc == ']') {
|
|
1942
|
+
if (depth == 0) break;
|
|
1943
|
+
depth--;
|
|
1944
|
+
i++;
|
|
1945
|
+
continue;
|
|
1946
|
+
}
|
|
1947
|
+
if (cc == '{' && depth == 0) break;
|
|
1948
|
+
|
|
1949
|
+
i++;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
err = bare_type_stripper__add_range(ctx, ims, i);
|
|
1953
|
+
if (err < 0) return err;
|
|
1954
|
+
|
|
1955
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1956
|
+
continue;
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
break;
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
if (i < n && u(0) == '{') {
|
|
1963
|
+
err = bare_type_stripper__process_class_body(ctx, &i, depth);
|
|
1964
|
+
if (err < 0) return err;
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
*result = i;
|
|
1968
|
+
return 0;
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
// Keyword classification for the walker: Maps a keyword to the `prev` class
|
|
1972
|
+
// it leaves behind. Keywords with side effects (declaration forms, 'as' /
|
|
1973
|
+
// 'satisfies', 'import' / 'export', ...) are dispatched explicitly in the
|
|
1974
|
+
// walker before this table is consulted.
|
|
1975
|
+
typedef struct {
|
|
1976
|
+
const char *kw;
|
|
1977
|
+
uint8_t len;
|
|
1978
|
+
uint8_t prev;
|
|
1979
|
+
} bare_type_stripper__kw_t;
|
|
1980
|
+
|
|
1981
|
+
static const bare_type_stripper__kw_t bare_type_stripper__kws[] = {
|
|
1982
|
+
// Expression-starting keywords: The next token begins a new expression.
|
|
1983
|
+
{"in", 2, bare_type_stripper__prev_op},
|
|
1984
|
+
{"new", 3, bare_type_stripper__prev_op},
|
|
1985
|
+
{"void", 4, bare_type_stripper__prev_op},
|
|
1986
|
+
{"case", 4, bare_type_stripper__prev_op},
|
|
1987
|
+
{"throw", 5, bare_type_stripper__prev_op},
|
|
1988
|
+
{"await", 5, bare_type_stripper__prev_op},
|
|
1989
|
+
{"yield", 5, bare_type_stripper__prev_op},
|
|
1990
|
+
{"return", 6, bare_type_stripper__prev_op},
|
|
1991
|
+
{"typeof", 6, bare_type_stripper__prev_op},
|
|
1992
|
+
{"delete", 6, bare_type_stripper__prev_op},
|
|
1993
|
+
{"instanceof", 10, bare_type_stripper__prev_op},
|
|
1994
|
+
|
|
1995
|
+
// Literal-like keywords: End an expression.
|
|
1996
|
+
{"true", 4, bare_type_stripper__prev_expr},
|
|
1997
|
+
{"this", 4, bare_type_stripper__prev_expr},
|
|
1998
|
+
{"null", 4, bare_type_stripper__prev_expr},
|
|
1999
|
+
{"false", 5, bare_type_stripper__prev_expr},
|
|
2000
|
+
{"super", 5, bare_type_stripper__prev_expr},
|
|
2001
|
+
{"arguments", 9, bare_type_stripper__prev_expr},
|
|
2002
|
+
{"undefined", 9, bare_type_stripper__prev_expr},
|
|
2003
|
+
|
|
2004
|
+
// Statement keywords whose body follows directly: A '{' after these is a
|
|
2005
|
+
// block, not an object literal, and the next token sits at statement
|
|
2006
|
+
// position.
|
|
2007
|
+
{"do", 2, bare_type_stripper__prev_stmt},
|
|
2008
|
+
{"try", 3, bare_type_stripper__prev_stmt},
|
|
2009
|
+
{"else", 4, bare_type_stripper__prev_stmt},
|
|
2010
|
+
{"finally", 7, bare_type_stripper__prev_stmt},
|
|
2011
|
+
|
|
2012
|
+
// 'async' is remembered so a following '(' can still be recognized as
|
|
2013
|
+
// arrow parameters.
|
|
2014
|
+
{"async", 5, bare_type_stripper__prev_async},
|
|
2015
|
+
};
|
|
2016
|
+
|
|
2017
|
+
// Classify an identifier span against the keyword table. Returns the `prev`
|
|
2018
|
+
// class the keyword leaves behind, or prev_expr for plain identifiers.
|
|
2019
|
+
static inline int
|
|
2020
|
+
bare_type_stripper__classify_kw(const utf8_t *s, size_t ks, size_t kl) {
|
|
2021
|
+
// Every entry is 2-10 bytes long; gate on length and first byte before
|
|
2022
|
+
// paying for a memcmp, since plain identifiers vastly outnumber keywords.
|
|
2023
|
+
if (kl < 2 || kl > 10) return bare_type_stripper__prev_expr;
|
|
2024
|
+
|
|
2025
|
+
uint8_t c0 = s[ks];
|
|
2026
|
+
|
|
2027
|
+
for (size_t k = 0; k < sizeof(bare_type_stripper__kws) / sizeof(bare_type_stripper__kws[0]); k++) {
|
|
2028
|
+
const bare_type_stripper__kw_t *kw = &bare_type_stripper__kws[k];
|
|
2029
|
+
|
|
2030
|
+
if (kw->len == kl && (uint8_t) kw->kw[0] == c0 && memcmp(&s[ks], kw->kw, kl) == 0) {
|
|
2031
|
+
return kw->prev;
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
return bare_type_stripper__prev_expr;
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
// Maximum walker recursion depth. The walker can recurse on '{' blocks and
|
|
2039
|
+
// (via the process_* helpers) on function/class bodies; capping the depth
|
|
2040
|
+
// keeps deeply nested input from blowing the C stack. 256 is generous in
|
|
2041
|
+
// practice - real code rarely nests more than a few dozen levels.
|
|
2042
|
+
#define BARE_TYPE_STRIPPER__MAX_DEPTH 256
|
|
2043
|
+
|
|
2044
|
+
// The master walker. Walks bytes from *result and either:
|
|
2045
|
+
// * If `stop_at` is non-zero, stops at the first occurrence of that byte at
|
|
2046
|
+
// depth 0 (relative to our local paren/bracket counters), without
|
|
2047
|
+
// consuming it. Two modes stop at more than one byte: ';' also stops at
|
|
2048
|
+
// ',' (variable declarators), and ',' also stops at ')' (default
|
|
2049
|
+
// parameter values).
|
|
2050
|
+
// * If `stop_at` is '}', expects to be called with i pointing AT the opening
|
|
2051
|
+
// '{' or after it - handled by the `body_is_stmt` flag: When true, this
|
|
2052
|
+
// walker recursively treats nested '{' blocks as statement contexts.
|
|
2053
|
+
//
|
|
2054
|
+
// `body_is_stmt` tells us whether the current block context behaves like a
|
|
2055
|
+
// statement region (so 'type', 'interface', etc. should be matched as
|
|
2056
|
+
// statement-leading keywords).
|
|
2057
|
+
static inline int
|
|
2058
|
+
bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop_at, bool body_is_stmt, bool stop_at_asi, uint32_t depth) {
|
|
2059
|
+
const utf8_t *s = ctx->s;
|
|
2060
|
+
size_t n = ctx->n;
|
|
2061
|
+
|
|
2062
|
+
int err;
|
|
2063
|
+
size_t i = *result;
|
|
2064
|
+
|
|
2065
|
+
// Bail out if we've recursed too deeply. We jump to end-of-input so the
|
|
2066
|
+
// enclosing scan finishes cleanly rather than getting stuck in a partial
|
|
2067
|
+
// state; the (very deeply nested) tail won't be stripped, which is no
|
|
2068
|
+
// worse than the alternative of crashing.
|
|
2069
|
+
if (depth > BARE_TYPE_STRIPPER__MAX_DEPTH) {
|
|
2070
|
+
*result = n;
|
|
2071
|
+
return 0;
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
// If we're entering a block scope, consume the opening '{'.
|
|
2075
|
+
if (stop_at == '}' && i < n && u(0) == '{') i++;
|
|
2076
|
+
|
|
2077
|
+
int paren_depth = 0;
|
|
2078
|
+
int bracket_depth = 0;
|
|
2079
|
+
|
|
2080
|
+
// Unclosed ternary '?' count at this walk level. Used to keep the arrow
|
|
2081
|
+
// return-type lookahead from claiming the ':' of an enclosing ternary
|
|
2082
|
+
// ('c ? (a) : b => c' is a ternary, not an annotated arrow).
|
|
2083
|
+
int ternary_depth = 0;
|
|
2084
|
+
|
|
2085
|
+
// Previous-token classification - see enum at top.
|
|
2086
|
+
int prev = body_is_stmt ? bare_type_stripper__prev_none : bare_type_stripper__prev_op;
|
|
2087
|
+
bool prev_dot = false;
|
|
2088
|
+
|
|
2089
|
+
while (i < n) {
|
|
2090
|
+
uint8_t ch = u(0);
|
|
2091
|
+
|
|
2092
|
+
// Whitespace.
|
|
2093
|
+
if (ws(ch)) {
|
|
2094
|
+
// ASI-aware stop: A newline at depth 0 right after an expression-
|
|
2095
|
+
// ending token is the cheapest lexical approximation of where ASI
|
|
2096
|
+
// would insert. Callers ask for this when scanning a class field
|
|
2097
|
+
// initializer so we don't chew into the next class member.
|
|
2098
|
+
if (stop_at_asi && lt(ch) && paren_depth == 0 && bracket_depth == 0 && bare_type_stripper__prev_is_expr(prev)) {
|
|
2099
|
+
*result = i;
|
|
2100
|
+
return 0;
|
|
2101
|
+
}
|
|
2102
|
+
// In statement contexts, treat a newline at depth 0 following an
|
|
2103
|
+
// expression-ending token as an ASI boundary - i.e. the next
|
|
2104
|
+
// identifier could be a statement-leading keyword ('type', 'declare',
|
|
2105
|
+
// 'interface', 'import', 'export'). Don't reset inside expression
|
|
2106
|
+
// contexts like object literals.
|
|
2107
|
+
if (body_is_stmt && lt(ch) && paren_depth == 0 && bracket_depth == 0 && bare_type_stripper__prev_is_expr(prev)) {
|
|
2108
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2109
|
+
}
|
|
2110
|
+
i++;
|
|
2111
|
+
continue;
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
// Comments.
|
|
2115
|
+
if (ch == '/' && i + 1 < n && u(1) == '/') {
|
|
2116
|
+
while (i < n && !lt(u(0))) i++;
|
|
2117
|
+
continue;
|
|
2118
|
+
}
|
|
2119
|
+
if (ch == '/' && i + 1 < n && u(1) == '*') {
|
|
2120
|
+
i += 2;
|
|
2121
|
+
while (i + 1 < n && !(u(0) == '*' && u(1) == '/')) i++;
|
|
2122
|
+
if (i + 1 < n) i += 2;
|
|
2123
|
+
continue;
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
// Stop condition.
|
|
2127
|
+
if (stop_at != 0 && paren_depth == 0 && bracket_depth == 0) {
|
|
2128
|
+
if (stop_at == '}' && ch == '}') {
|
|
2129
|
+
i++;
|
|
2130
|
+
*result = i;
|
|
2131
|
+
return 0;
|
|
2132
|
+
}
|
|
2133
|
+
if (stop_at == ')' && ch == ')') {
|
|
2134
|
+
*result = i;
|
|
2135
|
+
return 0;
|
|
2136
|
+
}
|
|
2137
|
+
if (stop_at == ';' && (ch == ';' || ch == ',')) {
|
|
2138
|
+
*result = i;
|
|
2139
|
+
return 0;
|
|
2140
|
+
}
|
|
2141
|
+
if (stop_at == ',' && (ch == ',' || ch == ')')) {
|
|
2142
|
+
*result = i;
|
|
2143
|
+
return 0;
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
// A token follows. Latch and clear the pending property-access marker -
|
|
2148
|
+
// only the '.' / '?.' branches re-arm it.
|
|
2149
|
+
bool was_dot = prev_dot;
|
|
2150
|
+
prev_dot = false;
|
|
2151
|
+
|
|
2152
|
+
// String, template, regex.
|
|
2153
|
+
if (ch == '\'' || ch == '"') {
|
|
2154
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
2155
|
+
prev = bare_type_stripper__prev_expr;
|
|
2156
|
+
continue;
|
|
2157
|
+
}
|
|
2158
|
+
if (ch == '`') {
|
|
2159
|
+
err = bare_type_stripper__walk_template(ctx, &i, depth);
|
|
2160
|
+
if (err < 0) return err;
|
|
2161
|
+
|
|
2162
|
+
prev = bare_type_stripper__prev_expr;
|
|
2163
|
+
continue;
|
|
2164
|
+
}
|
|
2165
|
+
if (ch == '/') {
|
|
2166
|
+
if (!bare_type_stripper__prev_is_expr(prev)) {
|
|
2167
|
+
i = bare_type_stripper__skip_regex(s, n, i);
|
|
2168
|
+
prev = bare_type_stripper__prev_expr;
|
|
2169
|
+
continue;
|
|
2170
|
+
}
|
|
2171
|
+
i++;
|
|
2172
|
+
prev = bare_type_stripper__prev_op;
|
|
2173
|
+
continue;
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
// Numeric literal.
|
|
2177
|
+
if (ch >= '0' && ch <= '9') {
|
|
2178
|
+
while (i < n && (id(u(0)) || u(0) == '.')) i++;
|
|
2179
|
+
prev = bare_type_stripper__prev_expr;
|
|
2180
|
+
continue;
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
// Identifier or keyword.
|
|
2184
|
+
if (ids(ch)) {
|
|
2185
|
+
size_t ks = i;
|
|
2186
|
+
while (i < n && id(u(0))) i++;
|
|
2187
|
+
size_t ke = i;
|
|
2188
|
+
size_t kl = ke - ks;
|
|
2189
|
+
|
|
2190
|
+
// Property access: This identifier is just a property name.
|
|
2191
|
+
if (was_dot) {
|
|
2192
|
+
prev = bare_type_stripper__prev_expr;
|
|
2193
|
+
continue;
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
bool at_stmt_start = (prev == bare_type_stripper__prev_stmt || prev == bare_type_stripper__prev_none);
|
|
2197
|
+
|
|
2198
|
+
// Statement-leading TS keywords. The three declaration forms ('type',
|
|
2199
|
+
// 'interface', 'declare') share an identical dispatch pattern: When
|
|
2200
|
+
// followed by an identifier, delegate to a process_* helper. 'abstract'
|
|
2201
|
+
// is a special case - it only erases the keyword and lets the normal
|
|
2202
|
+
// 'class' branch handle the rest.
|
|
2203
|
+
if (at_stmt_start) {
|
|
2204
|
+
int (*handler)(bare_type_stripper_t *, size_t *) = NULL;
|
|
2205
|
+
|
|
2206
|
+
if (bare_type_stripper__match_kw(s, ks, kl, "type", 4)) handler = bare_type_stripper__process_type_alias;
|
|
2207
|
+
else if (bare_type_stripper__match_kw(s, ks, kl, "interface", 9)) handler = bare_type_stripper__process_interface;
|
|
2208
|
+
else if (bare_type_stripper__match_kw(s, ks, kl, "declare", 7)) handler = bare_type_stripper__process_declare;
|
|
2209
|
+
|
|
2210
|
+
if (handler) {
|
|
2211
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2212
|
+
if (after < n && ids(s[after])) {
|
|
2213
|
+
size_t save = ks;
|
|
2214
|
+
|
|
2215
|
+
err = handler(ctx, &save);
|
|
2216
|
+
if (err < 0) return err;
|
|
2217
|
+
|
|
2218
|
+
i = save;
|
|
2219
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2220
|
+
continue;
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
// 'abstract class' - strip the leading 'abstract' keyword. The 'class'
|
|
2225
|
+
// that follows is processed by the normal class branch below.
|
|
2226
|
+
if (bare_type_stripper__match_kw(s, ks, kl, "abstract", 8)) {
|
|
2227
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2228
|
+
if (bare_type_stripper__at_kw(s, n, after, "class", 5)) {
|
|
2229
|
+
err = bare_type_stripper__add_range(ctx, ks, ke);
|
|
2230
|
+
if (err < 0) return err;
|
|
2231
|
+
|
|
2232
|
+
i = ke;
|
|
2233
|
+
continue;
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
// 'enum E { ... }' - non-erasable: An enum declaration emits a
|
|
2238
|
+
// runtime object that stripping can't reproduce. Mark the whole
|
|
2239
|
+
// construct so strip() can throw while lex() consumers still see
|
|
2240
|
+
// its extent. ('declare enum' is ambient and already handled above.)
|
|
2241
|
+
if (bare_type_stripper__match_kw(s, ks, kl, "enum", 4)) {
|
|
2242
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2243
|
+
if (after < n && ids(s[after])) {
|
|
2244
|
+
size_t end = bare_type_stripper__scan_enum(s, n, ks);
|
|
2245
|
+
|
|
2246
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
|
|
2247
|
+
if (err < 0) return err;
|
|
2248
|
+
|
|
2249
|
+
i = end;
|
|
2250
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
// 'namespace N { ... }' / 'module N { ... }' - non-erasable for the
|
|
2256
|
+
// same reason. Requires a '{' body after the (possibly dotted) name
|
|
2257
|
+
// so identifiers that merely spell the keywords ('module.exports')
|
|
2258
|
+
// are left alone.
|
|
2259
|
+
if (bare_type_stripper__match_kw(s, ks, kl, "namespace", 9) || bare_type_stripper__match_kw(s, ks, kl, "module", 6)) {
|
|
2260
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2261
|
+
if (p < n && ids(s[p])) {
|
|
2262
|
+
while (p < n && (id(s[p]) || s[p] == '.')) p++;
|
|
2263
|
+
p = bare_type_stripper__skip_trivia(s, n, p);
|
|
2264
|
+
|
|
2265
|
+
if (p < n && s[p] == '{') {
|
|
2266
|
+
size_t end = bare_type_stripper__scan_balanced(s, n, p, '{', '}');
|
|
2267
|
+
|
|
2268
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
|
|
2269
|
+
if (err < 0) return err;
|
|
2270
|
+
|
|
2271
|
+
i = end;
|
|
2272
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2273
|
+
continue;
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
// 'catch (e: T)' - strip the TS type annotation inside the catch
|
|
2280
|
+
// parameter list. 'catch' is a reserved word so its only valid context
|
|
2281
|
+
// is the catch clause of a try-statement; we don't gate on prev. The
|
|
2282
|
+
// outer walker still processes 'catch (e) { ... }' as usual.
|
|
2283
|
+
if (kl == 5 && memcmp(&s[ks], "catch", 5) == 0) {
|
|
2284
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2285
|
+
if (after < n && s[after] == '(') {
|
|
2286
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, after + 1);
|
|
2287
|
+
if (p < n && ids(s[p])) {
|
|
2288
|
+
while (p < n && id(s[p])) p++;
|
|
2289
|
+
size_t scan = bare_type_stripper__skip_trivia(s, n, p);
|
|
2290
|
+
if (scan < n && s[scan] == ':') {
|
|
2291
|
+
size_t ts = scan;
|
|
2292
|
+
size_t te = bare_type_stripper__scan_type(s, n, scan + 1, BARE_TYPE_STRIPPER__T_PAREN);
|
|
2293
|
+
|
|
2294
|
+
err = bare_type_stripper__add_range(ctx, ts, te);
|
|
2295
|
+
if (err < 0) return err;
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
// 'import' and 'export' - module-level statement keywords. Allow these
|
|
2302
|
+
// even when prev is something other than stmt_start, since they're
|
|
2303
|
+
// reserved words and our prev tracking can be imprecise at module level.
|
|
2304
|
+
if (kl == 6 && memcmp(&s[ks], "import", 6) == 0) {
|
|
2305
|
+
// Dynamic import or import.meta - leave as expression.
|
|
2306
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2307
|
+
if (after < n && (s[after] == '(' || s[after] == '.')) {
|
|
2308
|
+
i = ke;
|
|
2309
|
+
prev = bare_type_stripper__prev_expr;
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
if (at_stmt_start) {
|
|
2314
|
+
size_t save = ks;
|
|
2315
|
+
|
|
2316
|
+
err = bare_type_stripper__process_import(ctx, &save);
|
|
2317
|
+
if (err < 0) return err;
|
|
2318
|
+
|
|
2319
|
+
i = save;
|
|
2320
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2321
|
+
continue;
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
if (kl == 6 && memcmp(&s[ks], "export", 6) == 0 && at_stmt_start) {
|
|
2326
|
+
size_t save = ks;
|
|
2327
|
+
|
|
2328
|
+
err = bare_type_stripper__process_export(ctx, &save);
|
|
2329
|
+
if (err < 0) return err;
|
|
2330
|
+
|
|
2331
|
+
i = save;
|
|
2332
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2333
|
+
continue;
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
// 'function' / 'class' - declaration or expression.
|
|
2337
|
+
if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
|
|
2338
|
+
size_t save = ks;
|
|
2339
|
+
|
|
2340
|
+
err = bare_type_stripper__process_function(ctx, &save, depth);
|
|
2341
|
+
if (err < 0) return err;
|
|
2342
|
+
|
|
2343
|
+
i = save;
|
|
2344
|
+
prev = at_stmt_start ? bare_type_stripper__prev_stmt : bare_type_stripper__prev_expr;
|
|
2345
|
+
continue;
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
if (kl == 5 && memcmp(&s[ks], "class", 5) == 0) {
|
|
2349
|
+
size_t save = ks;
|
|
2350
|
+
|
|
2351
|
+
err = bare_type_stripper__process_class(ctx, &save, depth);
|
|
2352
|
+
if (err < 0) return err;
|
|
2353
|
+
|
|
2354
|
+
i = save;
|
|
2355
|
+
prev = at_stmt_start ? bare_type_stripper__prev_stmt : bare_type_stripper__prev_expr;
|
|
2356
|
+
continue;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
// Variable declarations - const/let/var. These appear at statement
|
|
2360
|
+
// starts and in for-loop headers ('for (const x = ...)'); we accept
|
|
2361
|
+
// any prev that isn't an expression-ending position.
|
|
2362
|
+
if (
|
|
2363
|
+
!bare_type_stripper__prev_is_expr(prev) &&
|
|
2364
|
+
((kl == 3 && (memcmp(&s[ks], "let", 3) == 0 ||
|
|
2365
|
+
memcmp(&s[ks], "var", 3) == 0)) ||
|
|
2366
|
+
(kl == 5 && memcmp(&s[ks], "const", 5) == 0))
|
|
2367
|
+
) {
|
|
2368
|
+
// 'const enum E { ... }' is an enum declaration, not a variable -
|
|
2369
|
+
// non-erasable like a plain 'enum'.
|
|
2370
|
+
if (kl == 5) {
|
|
2371
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, ke);
|
|
2372
|
+
if (bare_type_stripper__at_kw(s, n, after, "enum", 4)) {
|
|
2373
|
+
size_t name = bare_type_stripper__skip_trivia(s, n, after + 4);
|
|
2374
|
+
if (name < n && ids(s[name])) {
|
|
2375
|
+
size_t end = bare_type_stripper__scan_enum(s, n, after);
|
|
2376
|
+
|
|
2377
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
|
|
2378
|
+
if (err < 0) return err;
|
|
2379
|
+
|
|
2380
|
+
i = end;
|
|
2381
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2382
|
+
continue;
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
i = ke;
|
|
2388
|
+
|
|
2389
|
+
err = bare_type_stripper__process_var_decl(ctx, &i, depth);
|
|
2390
|
+
if (err < 0) return err;
|
|
2391
|
+
|
|
2392
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2393
|
+
continue;
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
// Postfix type assertion keywords 'as' and 'satisfies' - both consume a
|
|
2397
|
+
// following type expression with the same terminator set.
|
|
2398
|
+
if (bare_type_stripper__prev_is_expr(prev) && (bare_type_stripper__match_kw(s, ks, kl, "as", 2) || bare_type_stripper__match_kw(s, ks, kl, "satisfies", 9))) {
|
|
2399
|
+
size_t te = bare_type_stripper__scan_type(
|
|
2400
|
+
s,
|
|
2401
|
+
n,
|
|
2402
|
+
ke,
|
|
2403
|
+
BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_PAREN | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_BRACK | BARE_TYPE_STRIPPER__T_EQ | BARE_TYPE_STRIPPER__T_NL
|
|
2404
|
+
);
|
|
2405
|
+
|
|
2406
|
+
uint32_t flags = bare_type_stripper__asi_hazard_after(s, n, te) ? bare_type_stripper_semi : 0;
|
|
2407
|
+
|
|
2408
|
+
err = bare_type_stripper__add_range_flags(ctx, ks, te, flags);
|
|
2409
|
+
if (err < 0) return err;
|
|
2410
|
+
|
|
2411
|
+
i = te;
|
|
2412
|
+
prev = bare_type_stripper__prev_expr;
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
// Remaining keywords only affect token classification - see the
|
|
2417
|
+
// keyword table above the walker. Plain identifiers end an expression.
|
|
2418
|
+
prev = bare_type_stripper__classify_kw(s, ks, kl);
|
|
2419
|
+
continue;
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
// Punctuation.
|
|
2423
|
+
switch (ch) {
|
|
2424
|
+
case '.': {
|
|
2425
|
+
// Spread / rest '...'
|
|
2426
|
+
if (i + 2 < n && u(1) == '.' && u(2) == '.') {
|
|
2427
|
+
i += 3;
|
|
2428
|
+
prev = bare_type_stripper__prev_op;
|
|
2429
|
+
continue;
|
|
2430
|
+
}
|
|
2431
|
+
// Otherwise '.' is a member operator; 'prev' is unchanged.
|
|
2432
|
+
i++;
|
|
2433
|
+
prev_dot = true;
|
|
2434
|
+
continue;
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
case '?': {
|
|
2438
|
+
// Optional chaining '?.'
|
|
2439
|
+
if (i + 1 < n && u(1) == '.') {
|
|
2440
|
+
i += 2;
|
|
2441
|
+
prev_dot = true;
|
|
2442
|
+
continue;
|
|
2443
|
+
}
|
|
2444
|
+
// Nullish coalescing '??' (optionally '??=').
|
|
2445
|
+
if (i + 1 < n && u(1) == '?') {
|
|
2446
|
+
i += 2;
|
|
2447
|
+
if (i < n && u(0) == '=') i++;
|
|
2448
|
+
prev = bare_type_stripper__prev_op;
|
|
2449
|
+
continue;
|
|
2450
|
+
}
|
|
2451
|
+
// Ternary '?'.
|
|
2452
|
+
ternary_depth++;
|
|
2453
|
+
i++;
|
|
2454
|
+
prev = bare_type_stripper__prev_op;
|
|
2455
|
+
continue;
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2458
|
+
case '(': {
|
|
2459
|
+
// Detect arrow function parameter lists: '(...)' immediately followed
|
|
2460
|
+
// by '=>' or by ': TYPE =>'. Without this we miss type annotations
|
|
2461
|
+
// inside arrow params. A '(' after an expression is a call and can't
|
|
2462
|
+
// start arrow parameters, so the lookahead is skipped entirely there -
|
|
2463
|
+
// except after the identifier 'async', which prefixes arrow parameters
|
|
2464
|
+
// while otherwise lexing like a plain expression.
|
|
2465
|
+
bool maybe_arrow = !bare_type_stripper__prev_is_expr(prev) || prev == bare_type_stripper__prev_async;
|
|
2466
|
+
|
|
2467
|
+
bool is_arrow = false;
|
|
2468
|
+
if (maybe_arrow) {
|
|
2469
|
+
size_t pend = bare_type_stripper__scan_group(ctx, i);
|
|
2470
|
+
size_t after = bare_type_stripper__skip_trivia(s, n, pend);
|
|
2471
|
+
|
|
2472
|
+
if (after + 1 < n && s[after] == '=' && s[after + 1] == '>') {
|
|
2473
|
+
is_arrow = true;
|
|
2474
|
+
} else if (after < n && s[after] == ':' && ternary_depth == 0) {
|
|
2475
|
+
// With an open ternary, a ':' after ')' is the ternary's alternate
|
|
2476
|
+
// separator ('c ? (a) : b => c'), not a return-type annotation.
|
|
2477
|
+
// Arrow return types can legitimately span newlines, so we don't
|
|
2478
|
+
// pass T_NL here - only the unambiguous terminators.
|
|
2479
|
+
size_t type_end = bare_type_stripper__scan_type(
|
|
2480
|
+
s,
|
|
2481
|
+
n,
|
|
2482
|
+
after + 1,
|
|
2483
|
+
BARE_TYPE_STRIPPER__T_ARROW | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI
|
|
2484
|
+
);
|
|
2485
|
+
|
|
2486
|
+
size_t after_type = bare_type_stripper__skip_trivia(s, n, type_end);
|
|
2487
|
+
|
|
2488
|
+
if (after_type + 1 < n && s[after_type] == '=' && s[after_type + 1] == '>') {
|
|
2489
|
+
is_arrow = true;
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
if (is_arrow) {
|
|
2495
|
+
err = bare_type_stripper__process_params(ctx, &i, depth);
|
|
2496
|
+
if (err < 0) return err;
|
|
2497
|
+
|
|
2498
|
+
// The original ')' sits one byte before i (process_params consumed it).
|
|
2499
|
+
size_t orig_paren = i - 1;
|
|
2500
|
+
|
|
2501
|
+
size_t aft = bare_type_stripper__skip_trivia(s, n, i);
|
|
2502
|
+
if (aft < n && s[aft] == ':') {
|
|
2503
|
+
size_t ts = aft;
|
|
2504
|
+
i = aft + 1;
|
|
2505
|
+
i = bare_type_stripper__scan_type(
|
|
2506
|
+
s,
|
|
2507
|
+
n,
|
|
2508
|
+
i,
|
|
2509
|
+
BARE_TYPE_STRIPPER__T_ARROW | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI
|
|
2510
|
+
);
|
|
2511
|
+
|
|
2512
|
+
// If anything between the params ')' and '=>' spans a newline -
|
|
2513
|
+
// whether inside the return-type itself OR in the gap between ')'
|
|
2514
|
+
// and ':' - JS would see a LineTerminator before '=>' after the
|
|
2515
|
+
// type is blanked, which is a syntax error. Move the ')' down:
|
|
2516
|
+
// Strip the original one and place a replacement ')' at the last
|
|
2517
|
+
// non-whitespace position before '=>'.
|
|
2518
|
+
bool multiline = false;
|
|
2519
|
+
for (size_t j = orig_paren + 1; j < i; j++) {
|
|
2520
|
+
if (lt(s[j])) {
|
|
2521
|
+
multiline = true;
|
|
2522
|
+
break;
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
|
|
2526
|
+
if (multiline) {
|
|
2527
|
+
// Walk back past whitespace to find a byte to repurpose as ')'.
|
|
2528
|
+
size_t target = i;
|
|
2529
|
+
while (target > orig_paren + 1 && ws(s[target - 1])) target--;
|
|
2530
|
+
|
|
2531
|
+
// For the relocation to help, the target byte must live on the
|
|
2532
|
+
// same line as '=>' (no newlines between them) - otherwise we'd
|
|
2533
|
+
// just put ')' on a different orphaned line, still leaving a
|
|
2534
|
+
// line terminator before '=>'. When that's the case we fall
|
|
2535
|
+
// back to the plain strip; the output won't be a valid arrow
|
|
2536
|
+
// but it's no worse than the naive strip.
|
|
2537
|
+
bool same_line = true;
|
|
2538
|
+
for (size_t j = target; j < i; j++) {
|
|
2539
|
+
if (lt(s[j])) {
|
|
2540
|
+
same_line = false;
|
|
2541
|
+
break;
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
if (target > orig_paren + 1 && same_line) {
|
|
2546
|
+
err = bare_type_stripper__add_range(ctx, orig_paren, i);
|
|
2547
|
+
if (err < 0) return err;
|
|
2548
|
+
|
|
2549
|
+
err = bare_type_stripper__add_range_flags(ctx, target - 1, target, bare_type_stripper_paren);
|
|
2550
|
+
if (err < 0) return err;
|
|
2551
|
+
} else {
|
|
2552
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
2553
|
+
if (err < 0) return err;
|
|
2554
|
+
}
|
|
2555
|
+
} else {
|
|
2556
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
2557
|
+
if (err < 0) return err;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
prev = bare_type_stripper__prev_op;
|
|
2562
|
+
continue;
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
paren_depth++;
|
|
2566
|
+
i++;
|
|
2567
|
+
prev = bare_type_stripper__prev_op;
|
|
2568
|
+
continue;
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
case ')': {
|
|
2572
|
+
if (paren_depth > 0) paren_depth--;
|
|
2573
|
+
i++;
|
|
2574
|
+
prev = bare_type_stripper__prev_paren;
|
|
2575
|
+
continue;
|
|
2576
|
+
}
|
|
2577
|
+
|
|
2578
|
+
case '[': {
|
|
2579
|
+
bracket_depth++;
|
|
2580
|
+
i++;
|
|
2581
|
+
prev = bare_type_stripper__prev_op;
|
|
2582
|
+
continue;
|
|
2583
|
+
}
|
|
2584
|
+
|
|
2585
|
+
case ']': {
|
|
2586
|
+
if (bracket_depth > 0) bracket_depth--;
|
|
2587
|
+
i++;
|
|
2588
|
+
prev = bare_type_stripper__prev_expr;
|
|
2589
|
+
continue;
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2592
|
+
case '{': {
|
|
2593
|
+
// Recurse. Decide whether this '{' starts a block (statement context)
|
|
2594
|
+
// or an object literal (expression context). A '{' right after ')'
|
|
2595
|
+
// (if/for/while/switch headers, function parameter lists) or after
|
|
2596
|
+
// '=>' (arrow function body) is always a block in valid code - an
|
|
2597
|
+
// object literal can't directly follow either token.
|
|
2598
|
+
bool is_arrow_body = prev == bare_type_stripper__prev_arrow;
|
|
2599
|
+
bool is_block_ctx =
|
|
2600
|
+
is_arrow_body ||
|
|
2601
|
+
prev == bare_type_stripper__prev_paren ||
|
|
2602
|
+
prev == bare_type_stripper__prev_stmt ||
|
|
2603
|
+
prev == bare_type_stripper__prev_none;
|
|
2604
|
+
|
|
2605
|
+
size_t save = i;
|
|
2606
|
+
|
|
2607
|
+
err = bare_type_stripper__walk(ctx, &save, '}', is_block_ctx, false, depth + 1);
|
|
2608
|
+
if (err < 0) return err;
|
|
2609
|
+
|
|
2610
|
+
i = save;
|
|
2611
|
+
// An arrow body ends the arrow expression; any other block ends a
|
|
2612
|
+
// statement.
|
|
2613
|
+
prev = is_block_ctx && !is_arrow_body
|
|
2614
|
+
? bare_type_stripper__prev_stmt
|
|
2615
|
+
: bare_type_stripper__prev_expr;
|
|
2616
|
+
continue;
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
case '}': {
|
|
2620
|
+
// Unmatched '}' at our depth - treat as end of containing scope.
|
|
2621
|
+
if (stop_at == '}' && paren_depth == 0 && bracket_depth == 0) {
|
|
2622
|
+
i++;
|
|
2623
|
+
*result = i;
|
|
2624
|
+
return 0;
|
|
2625
|
+
}
|
|
2626
|
+
i++;
|
|
2627
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2628
|
+
continue;
|
|
2629
|
+
}
|
|
2630
|
+
|
|
2631
|
+
case ';': {
|
|
2632
|
+
ternary_depth = 0;
|
|
2633
|
+
i++;
|
|
2634
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2635
|
+
continue;
|
|
2636
|
+
}
|
|
2637
|
+
|
|
2638
|
+
case ',': {
|
|
2639
|
+
i++;
|
|
2640
|
+
prev = bare_type_stripper__prev_op;
|
|
2641
|
+
continue;
|
|
2642
|
+
}
|
|
2643
|
+
|
|
2644
|
+
case '!': {
|
|
2645
|
+
// Postfix non-null assertion: Prev is an expression and not followed by '='.
|
|
2646
|
+
if (bare_type_stripper__prev_is_expr(prev) && (i + 1 >= n || u(1) != '=')) {
|
|
2647
|
+
err = bare_type_stripper__add_range(ctx, i, i + 1);
|
|
2648
|
+
if (err < 0) return err;
|
|
2649
|
+
|
|
2650
|
+
i++;
|
|
2651
|
+
continue;
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
// '!' (logical not), '!=' or '!=='.
|
|
2655
|
+
i++;
|
|
2656
|
+
if (i < n && u(0) == '=') i++;
|
|
2657
|
+
if (i < n && u(0) == '=') i++;
|
|
2658
|
+
prev = bare_type_stripper__prev_op;
|
|
2659
|
+
continue;
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
case '<': {
|
|
2663
|
+
// Generic at call site or instantiation: Prev is an expression and a
|
|
2664
|
+
// balanced '<...>' is followed by a token that can't continue a JS
|
|
2665
|
+
// comparison expression. '(' / '`' mean a generic call or tagged
|
|
2666
|
+
// template. ')' / ',' / ';' / ']' / '}' / end-of-input mean a TS-only
|
|
2667
|
+
// generic instantiation expression (TS 4.7+) - 'foo<T>' returning a
|
|
2668
|
+
// typed reference - which would otherwise mis-parse as chained
|
|
2669
|
+
// comparisons in stripped JS.
|
|
2670
|
+
if (bare_type_stripper__prev_is_expr(prev)) {
|
|
2671
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, i);
|
|
2672
|
+
if (ge > i + 1) {
|
|
2673
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
|
|
2674
|
+
bool is_generic = false;
|
|
2675
|
+
if (peek >= n) {
|
|
2676
|
+
is_generic = true;
|
|
2677
|
+
} else {
|
|
2678
|
+
uint8_t pc = s[peek];
|
|
2679
|
+
if (pc == '(' || pc == '`' || pc == ')' || pc == ',' || pc == ';' || pc == ']' || pc == '}') {
|
|
2680
|
+
is_generic = true;
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
if (is_generic) {
|
|
2684
|
+
err = bare_type_stripper__add_range(ctx, i, ge);
|
|
2685
|
+
if (err < 0) return err;
|
|
2686
|
+
|
|
2687
|
+
i = ge;
|
|
2688
|
+
continue;
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
} else {
|
|
2692
|
+
// '<' at the start of an expression isn't valid JS - it's TS-only
|
|
2693
|
+
// syntax: Generic arrow type parameters ('<T,>(x) => x') when a '('
|
|
2694
|
+
// follows the balanced '<...>', otherwise an old-style angle cast
|
|
2695
|
+
// ('<T>expr') or JSX, which we can't tell apart. Strip the former;
|
|
2696
|
+
// mark the latter as non-erasable.
|
|
2697
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, i);
|
|
2698
|
+
if (ge > i + 1) {
|
|
2699
|
+
size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
|
|
2700
|
+
|
|
2701
|
+
if (peek < n && s[peek] == '(') {
|
|
2702
|
+
err = bare_type_stripper__add_range(ctx, i, ge);
|
|
2703
|
+
if (err < 0) return err;
|
|
2704
|
+
} else {
|
|
2705
|
+
err = bare_type_stripper__add_range_flags(ctx, i, ge, bare_type_stripper_error);
|
|
2706
|
+
if (err < 0) return err;
|
|
2707
|
+
}
|
|
2708
|
+
|
|
2709
|
+
i = ge;
|
|
2710
|
+
prev = bare_type_stripper__prev_op;
|
|
2711
|
+
continue;
|
|
2712
|
+
}
|
|
2713
|
+
}
|
|
2714
|
+
|
|
2715
|
+
// '<', '<=', '<<' or '<<='.
|
|
2716
|
+
i++;
|
|
2717
|
+
if (i < n && u(0) == '=') i++;
|
|
2718
|
+
if (i < n && u(0) == '<') i++;
|
|
2719
|
+
prev = bare_type_stripper__prev_op;
|
|
2720
|
+
continue;
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
case '=': {
|
|
2724
|
+
i++;
|
|
2725
|
+
|
|
2726
|
+
// Arrow '=>'.
|
|
2727
|
+
if (i < n && u(0) == '>') {
|
|
2728
|
+
i++;
|
|
2729
|
+
prev = bare_type_stripper__prev_arrow;
|
|
2730
|
+
continue;
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
// '==', '==='.
|
|
2734
|
+
if (i < n && u(0) == '=') i++;
|
|
2735
|
+
if (i < n && u(0) == '=') i++;
|
|
2736
|
+
prev = bare_type_stripper__prev_op;
|
|
2737
|
+
continue;
|
|
2738
|
+
}
|
|
2739
|
+
|
|
2740
|
+
case ':': {
|
|
2741
|
+
if (ternary_depth > 0) ternary_depth--;
|
|
2742
|
+
i++;
|
|
2743
|
+
prev = bare_type_stripper__prev_op;
|
|
2744
|
+
continue;
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
case '@': {
|
|
2748
|
+
// Decorator. The decorated thing (a class, class member or top-level
|
|
2749
|
+
// declaration) sits at a statement-leading position.
|
|
2750
|
+
err = bare_type_stripper__process_decorator(ctx, &i, depth);
|
|
2751
|
+
if (err < 0) return err;
|
|
2752
|
+
|
|
2753
|
+
prev = bare_type_stripper__prev_stmt;
|
|
2754
|
+
continue;
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
default: {
|
|
2758
|
+
i++;
|
|
2759
|
+
prev = bare_type_stripper__prev_op;
|
|
2760
|
+
continue;
|
|
2761
|
+
}
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
*result = i;
|
|
2766
|
+
return 0;
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
static inline int
|
|
2770
|
+
bare_type_stripper__lex(bare_type_stripper_t *ctx, const utf8_t *s, size_t n) {
|
|
2771
|
+
size_t i = 0;
|
|
2772
|
+
|
|
2773
|
+
ctx->s = s;
|
|
2774
|
+
ctx->n = n;
|
|
2775
|
+
|
|
2776
|
+
ctx->ranges = NULL;
|
|
2777
|
+
ctx->len = 0;
|
|
2778
|
+
ctx->cap = 0;
|
|
2779
|
+
|
|
2780
|
+
memset(ctx->paren_close, 0, sizeof(ctx->paren_close));
|
|
2781
|
+
|
|
2782
|
+
return bare_type_stripper__walk(ctx, &i, 0, true, false, 0);
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
#undef u
|
|
2786
|
+
#undef lt
|
|
2787
|
+
#undef ws
|
|
2788
|
+
#undef ids
|
|
2789
|
+
#undef id
|
|
2790
|
+
|
|
2791
|
+
#endif // BARE_TYPE_STRIPPER_H
|