bare-type-stripper 0.0.0 → 0.1.1

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.
Files changed (33) hide show
  1. package/CMakeLists.txt +37 -0
  2. package/LICENSE +201 -0
  3. package/README.md +90 -0
  4. package/binding.c +90 -0
  5. package/index.d.ts +19 -0
  6. package/index.js +129 -0
  7. package/lib/binding/bare.js +1 -0
  8. package/lib/binding/node.js +3 -0
  9. package/lib/strip.h +3041 -0
  10. package/package.json +65 -1
  11. package/prebuilds/android-arm/bare-type-stripper.bare +0 -0
  12. package/prebuilds/android-arm/bare-type-stripper.node +0 -0
  13. package/prebuilds/android-arm64/bare-type-stripper.bare +0 -0
  14. package/prebuilds/android-arm64/bare-type-stripper.node +0 -0
  15. package/prebuilds/android-ia32/bare-type-stripper.bare +0 -0
  16. package/prebuilds/android-ia32/bare-type-stripper.node +0 -0
  17. package/prebuilds/android-x64/bare-type-stripper.bare +0 -0
  18. package/prebuilds/android-x64/bare-type-stripper.node +0 -0
  19. package/prebuilds/darwin-arm64/bare-type-stripper.bare +0 -0
  20. package/prebuilds/darwin-arm64/bare-type-stripper.node +0 -0
  21. package/prebuilds/darwin-x64/bare-type-stripper.bare +0 -0
  22. package/prebuilds/darwin-x64/bare-type-stripper.node +0 -0
  23. package/prebuilds/ios-arm64/bare-type-stripper.bare +0 -0
  24. package/prebuilds/ios-arm64-simulator/bare-type-stripper.bare +0 -0
  25. package/prebuilds/ios-x64-simulator/bare-type-stripper.bare +0 -0
  26. package/prebuilds/linux-arm64/bare-type-stripper.bare +0 -0
  27. package/prebuilds/linux-arm64/bare-type-stripper.node +0 -0
  28. package/prebuilds/linux-x64/bare-type-stripper.bare +0 -0
  29. package/prebuilds/linux-x64/bare-type-stripper.node +0 -0
  30. package/prebuilds/win32-arm64/bare-type-stripper.bare +0 -0
  31. package/prebuilds/win32-arm64/bare-type-stripper.node +0 -0
  32. package/prebuilds/win32-x64/bare-type-stripper.bare +0 -0
  33. package/prebuilds/win32-x64/bare-type-stripper.node +0 -0
package/lib/strip.h ADDED
@@ -0,0 +1,3041 @@
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
+ // '=>' inside brackets is part of an arrow type - consume it whole so
611
+ // its '>' can't be mistaken for the close of an enclosing '<'.
612
+ if (ch == '=' && depth > 0 && i + 1 < n && u(1) == '>') {
613
+ i += 2;
614
+ seen_token = true;
615
+ last_was_op = true;
616
+ continue;
617
+ }
618
+
619
+ if (depth == 0) {
620
+ if ((terms & BARE_TYPE_STRIPPER__T_COMMA) && ch == ',') return i;
621
+ if ((terms & BARE_TYPE_STRIPPER__T_SEMI) && ch == ';') return i;
622
+ if ((terms & BARE_TYPE_STRIPPER__T_PAREN) && ch == ')') return i;
623
+ if ((terms & BARE_TYPE_STRIPPER__T_BRACK) && ch == ']') return i;
624
+ if ((terms & BARE_TYPE_STRIPPER__T_BRACE) && ch == '}') return i;
625
+ if ((terms & BARE_TYPE_STRIPPER__T_ANGLE) && ch == '>') return i;
626
+
627
+ if (ch == '=') {
628
+ // '==' is comparison (rare inside a type, treated as continuation).
629
+ if (i + 1 < n && u(1) == '=') {
630
+ i += 2;
631
+ seen_token = true;
632
+ last_was_op = true;
633
+ continue;
634
+ }
635
+
636
+ // '=>' is either an arrow-function separator (terminator) or part of
637
+ // an arrow-type expression (continuation), depending on terms.
638
+ if (i + 1 < n && u(1) == '>') {
639
+ if (terms & BARE_TYPE_STRIPPER__T_ARROW) return i;
640
+ i += 2;
641
+ seen_token = true;
642
+ last_was_op = true;
643
+ continue;
644
+ }
645
+
646
+ if (terms & BARE_TYPE_STRIPPER__T_EQ) return i;
647
+
648
+ i++;
649
+ seen_token = true;
650
+ last_was_op = true;
651
+ continue;
652
+ }
653
+
654
+ // A '{' directly after a type-level operator ('&', '|', 'extends',
655
+ // ...) is an object type literal operand, not the '{' the caller is
656
+ // waiting for - only stop on it after a complete type.
657
+ if ((terms & BARE_TYPE_STRIPPER__T_BRACE) && ch == '{' && seen_token && !last_was_op) return i;
658
+ }
659
+
660
+ if (ch == '(' || ch == '[' || ch == '{') {
661
+ depth++;
662
+ i++;
663
+ seen_token = true;
664
+ last_was_op = true;
665
+ continue;
666
+ }
667
+ if (ch == ')' || ch == ']' || ch == '}') {
668
+ if (depth == 0) return i;
669
+ depth--;
670
+ i++;
671
+ last_was_op = false;
672
+ continue;
673
+ }
674
+
675
+ if (ch == '<') {
676
+ depth++;
677
+ i++;
678
+ seen_token = true;
679
+ last_was_op = true;
680
+ continue;
681
+ }
682
+ if (ch == '>') {
683
+ if (depth > 0) {
684
+ depth--;
685
+ i++;
686
+ last_was_op = false;
687
+ continue;
688
+ }
689
+ // At depth 0, '>' is a comparison operator in surrounding JS code, not
690
+ // a type close - it ends the type. Without this, 'as number > 2' would
691
+ // greedily swallow ' > 2' into the cast.
692
+ return i;
693
+ }
694
+
695
+ if (ids(ch)) {
696
+ size_t ks = i;
697
+ while (i < n && id(u(0))) i++;
698
+ size_t kl = i - ks;
699
+
700
+ if (depth == 0 && (terms & BARE_TYPE_STRIPPER__T_INOF)) {
701
+ if (kl == 2 && (memcmp(&s[ks], "in", 2) == 0 || memcmp(&s[ks], "of", 2) == 0)) {
702
+ return ks;
703
+ }
704
+ }
705
+
706
+ // Type-level keywords that expect another type to follow. Marking these
707
+ // as 'op' keeps multi-line conditional types intact (e.g. 'T extends\n
708
+ // U ? A : B').
709
+ last_was_op =
710
+ (kl == 5 && (memcmp(&s[ks], "infer", 5) == 0 ||
711
+ memcmp(&s[ks], "keyof", 5) == 0)) ||
712
+ (kl == 6 && memcmp(&s[ks], "typeof", 6) == 0) ||
713
+ (kl == 7 && memcmp(&s[ks], "extends", 7) == 0);
714
+ seen_token = true;
715
+ continue;
716
+ }
717
+
718
+ // Punctuation that combines two type expressions ('|', '&', '?', ':', ',').
719
+ // We don't terminate on these here (T_COMMA above already covers ',' at
720
+ // depth 0 when set, and ',' inside brackets has depth > 0); they just
721
+ // continue scanning and mark the position as 'after an operator'.
722
+ last_was_op = (ch == '|' || ch == '&' || ch == '?' || ch == ':' || ch == ',' || ch == '.');
723
+ seen_token = true;
724
+ i++;
725
+ }
726
+
727
+ return i;
728
+ }
729
+
730
+ // Forward declaration of the master walker - used by helpers that process
731
+ // nested blocks (function bodies, class methods, etc.).
732
+ static inline int
733
+ 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);
734
+
735
+ // Forward declaration of the function processor - used by the export
736
+ // processor to delegate 'export [default] function ...' statements.
737
+ static inline int
738
+ bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, size_t stmt_start, uint32_t depth);
739
+
740
+ // Walk a template literal, recursively stripping type syntax inside each
741
+ // '${...}' expression. Assumes s[*result] == '`'. Leaves the cursor after
742
+ // the closing '`'. Unlike skip_template (which only advances the cursor),
743
+ // this invokes the walker on the substitution expressions so 'x as Foo'
744
+ // inside '${x as Foo}' still gets stripped.
745
+ static inline int
746
+ bare_type_stripper__walk_template(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
747
+ const utf8_t *s = ctx->s;
748
+ size_t n = ctx->n;
749
+
750
+ int err;
751
+ size_t i = *result;
752
+ i++;
753
+
754
+ while (i < n) {
755
+ if (u(0) == '\\') {
756
+ if (i + 1 < n) i += 2;
757
+ else i++;
758
+ continue;
759
+ }
760
+
761
+ if (u(0) == '`') {
762
+ i++;
763
+ break;
764
+ }
765
+
766
+ if (u(0) == '$' && i + 1 < n && u(1) == '{') {
767
+ i += 2;
768
+
769
+ err = bare_type_stripper__walk(ctx, &i, '}', false, false, depth + 1);
770
+ if (err < 0) return err;
771
+
772
+ continue;
773
+ }
774
+
775
+ i++;
776
+ }
777
+
778
+ *result = i;
779
+ return 0;
780
+ }
781
+
782
+ // Process a decorator '@...'. Expects s[i] == '@'. Handles three forms:
783
+ //
784
+ // @Name.Path bare reference
785
+ // @Name.Path<T> TS 4.7+ generic decorator (strips '<T>')
786
+ // @Name.Path(args) decorator factory call (walks args)
787
+ // @(expr) parenthesized expression (walks expr)
788
+ //
789
+ // In the (args) and (expr) forms we recurse via walk() so 'as'/'satisfies'/
790
+ // '!' inside the args also get stripped. Leaves the cursor just past the
791
+ // closing ')' if there is one, otherwise just past the identifier path.
792
+ static inline int
793
+ bare_type_stripper__process_decorator(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
794
+ const utf8_t *s = ctx->s;
795
+ size_t n = ctx->n;
796
+
797
+ int err;
798
+ size_t i = *result;
799
+
800
+ if (i >= n || u(0) != '@') return 0;
801
+ i++;
802
+
803
+ if (i < n && u(0) == '(') {
804
+ i++;
805
+
806
+ err = bare_type_stripper__walk(ctx, &i, ')', false, false, depth + 1);
807
+ if (err < 0) return err;
808
+
809
+ if (i < n && u(0) == ')') i++;
810
+ } else {
811
+ while (i < n && (id(u(0)) || u(0) == '.')) i++;
812
+
813
+ err = bare_type_stripper__strip_generic(ctx, &i);
814
+ if (err < 0) return err;
815
+
816
+ i = bare_type_stripper__skip_trivia(s, n, i);
817
+
818
+ if (i < n && u(0) == '(') {
819
+ i++;
820
+
821
+ err = bare_type_stripper__walk(ctx, &i, ')', false, false, depth + 1);
822
+ if (err < 0) return err;
823
+
824
+ if (i < n && u(0) == ')') i++;
825
+ }
826
+ }
827
+
828
+ *result = i;
829
+ return 0;
830
+ }
831
+
832
+ // Process a function's parameter list. Expects s[i] == '('. Strips parameter
833
+ // modifiers (parameter properties), type annotations and optional '?' markers.
834
+ static inline int
835
+ bare_type_stripper__process_params(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
836
+ const utf8_t *s = ctx->s;
837
+ size_t n = ctx->n;
838
+
839
+ int err;
840
+ size_t i = *result;
841
+
842
+ if (u(0) != '(') {
843
+ *result = i;
844
+ return 0;
845
+ }
846
+ i++;
847
+
848
+ // 'this: T' parameter. TypeScript allows the first parameter to be named
849
+ // 'this' for typing the call context; it has no runtime presence and must
850
+ // be erased entirely (including its trailing comma if more params follow).
851
+ size_t after = bare_type_stripper__skip_trivia(s, n, i);
852
+ if (bare_type_stripper__at_kw(s, n, after, "this", 4)) {
853
+ size_t this_end = after + 4;
854
+ size_t scan = bare_type_stripper__skip_trivia(s, n, this_end);
855
+
856
+ if (scan < n && (s[scan] == ':' || s[scan] == ',' || s[scan] == ')')) {
857
+ // Walk past the optional ': T' annotation.
858
+ if (s[scan] == ':') {
859
+ scan++;
860
+ scan = bare_type_stripper__scan_type(s, n, scan, BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_PAREN);
861
+ }
862
+
863
+ // Erase the 'this' parameter and the trailing comma when more
864
+ // parameters follow; if not, leave the closing ')' for the loop.
865
+ size_t strip_end = scan;
866
+ if (scan < n && s[scan] == ',') strip_end = scan + 1;
867
+
868
+ err = bare_type_stripper__add_range(ctx, i, strip_end);
869
+ if (err < 0) return err;
870
+ i = strip_end;
871
+ }
872
+ }
873
+
874
+ // Each iteration recognizes one feature (a modifier, a name, an optional
875
+ // marker, an annotation, a default, a comma, etc.) and 'continue's. Any
876
+ // unrecognized byte falls through to the bottom 'i++', which guarantees
877
+ // forward progress on malformed input without an explicit safeguard.
878
+ while (i < n) {
879
+ uint8_t ch = u(0);
880
+
881
+ if (ws(ch) || (ch == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*'))) {
882
+ i = bare_type_stripper__skip_trivia(s, n, i);
883
+ continue;
884
+ }
885
+
886
+ if (ch == ')') {
887
+ i++;
888
+ *result = i;
889
+ return 0;
890
+ }
891
+
892
+ if (ch == ',') {
893
+ i++;
894
+ continue;
895
+ }
896
+
897
+ // Decorator on a parameter - leave the decorator itself alone (decorators
898
+ // emit runtime code) but walk its args so any TS inside gets stripped.
899
+ if (ch == '@') {
900
+ err = bare_type_stripper__process_decorator(ctx, &i, depth);
901
+ if (err < 0) return err;
902
+ continue;
903
+ }
904
+
905
+ // Rest parameter '...'
906
+ if (i + 2 < n && ch == '.' && u(1) == '.' && u(2) == '.') {
907
+ i += 3;
908
+ continue;
909
+ }
910
+
911
+ // Destructuring pattern. Balance brackets but don't recurse - destructuring
912
+ // may contain nested type annotations only in obscure cases.
913
+ if (ch == '[' || ch == '{') {
914
+ uint8_t close = ch == '[' ? ']' : '}';
915
+ i = bare_type_stripper__scan_balanced(s, n, i, ch, close);
916
+ continue;
917
+ }
918
+
919
+ // Optional marker '?'
920
+ if (ch == '?') {
921
+ err = bare_type_stripper__add_range(ctx, i, i + 1);
922
+ if (err < 0) return err;
923
+
924
+ i++;
925
+ continue;
926
+ }
927
+
928
+ // Type annotation ': T'
929
+ if (ch == ':') {
930
+ size_t ts = i;
931
+ i++;
932
+ i = bare_type_stripper__scan_type(
933
+ s,
934
+ n,
935
+ i,
936
+ BARE_TYPE_STRIPPER__T_COMMA | BARE_TYPE_STRIPPER__T_PAREN | BARE_TYPE_STRIPPER__T_EQ
937
+ );
938
+
939
+ err = bare_type_stripper__add_range(ctx, ts, i);
940
+ if (err < 0) return err;
941
+ continue;
942
+ }
943
+
944
+ // Default value '=' - walk the expression, stopping at ',' or ')' at
945
+ // depth 0 (the loop will see whichever stop char comes next).
946
+ if (ch == '=') {
947
+ i++;
948
+ err = bare_type_stripper__walk(ctx, &i, ',', false, false, depth + 1);
949
+ if (err < 0) return err;
950
+ continue;
951
+ }
952
+
953
+ // Identifier - either a TS modifier (public/private/protected/readonly/
954
+ // override) prefixing a parameter property, or the parameter name.
955
+ if (ids(ch)) {
956
+ size_t ks = i;
957
+ while (i < n && id(u(0))) i++;
958
+ size_t ke = i;
959
+ size_t kl = ke - ks;
960
+
961
+ if (bare_type_stripper__is_ts_modifier(s, ks, kl, false)) {
962
+ size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
963
+
964
+ // Match the modifier only if followed by another modifier, an
965
+ // identifier, or a destructuring/rest opener - i.e. we're really at
966
+ // a parameter property and not just an identifier that happens to
967
+ // spell a modifier keyword. A parameter property is non-erasable:
968
+ // TypeScript assigns the parameter to a same-named instance member
969
+ // at construction time, which stripping the modifier can't
970
+ // reproduce, so mark it instead of erasing it.
971
+ if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '{' || (s[peek] == '.' && peek + 2 < n && s[peek + 1] == '.' && s[peek + 2] == '.'))) {
972
+ err = bare_type_stripper__add_range_flags(ctx, ks, ke, bare_type_stripper_error);
973
+ if (err < 0) return err;
974
+ i = peek;
975
+ }
976
+ }
977
+ continue;
978
+ }
979
+
980
+ // Unrecognized byte - skip it so the loop can't wedge on malformed input.
981
+ i++;
982
+ }
983
+
984
+ *result = i;
985
+ return 0;
986
+ }
987
+
988
+ // Process the body of a class. Expects s[i] == '{'. Walks until matching '}'.
989
+ static inline int
990
+ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
991
+ const utf8_t *s = ctx->s;
992
+ size_t n = ctx->n;
993
+
994
+ int err;
995
+ size_t i = *result;
996
+
997
+ if (u(0) != '{') return 0;
998
+ i++;
999
+
1000
+ while (i < n) {
1001
+ i = bare_type_stripper__skip_trivia(s, n, i);
1002
+ if (i >= n) break;
1003
+
1004
+ if (u(0) == '}') {
1005
+ i++;
1006
+ *result = i;
1007
+ return 0;
1008
+ }
1009
+ if (u(0) == ';') {
1010
+ i++;
1011
+ continue;
1012
+ }
1013
+
1014
+ // Decorator on a member.
1015
+ if (u(0) == '@') {
1016
+ err = bare_type_stripper__process_decorator(ctx, &i, depth);
1017
+ if (err < 0) return err;
1018
+ continue;
1019
+ }
1020
+
1021
+ // Remember where this member starts and how many ranges were emitted
1022
+ // before it. A member that turns out to be a bodyless overload or
1023
+ // abstract signature is rolled back to this point and erased whole.
1024
+ size_t member_start = i;
1025
+ uint32_t member_mark = ctx->len;
1026
+
1027
+ // 'static' is a real JS keyword that prefixes a class member, or starts
1028
+ // a static initialization block. Consume it when followed by another
1029
+ // identifier (or '['/'*') and remember that the line now has a syntactic
1030
+ // anchor: A subsequent TS modifier strip on the same line must NOT inject
1031
+ // ';' since 'static' already breaks any ASI hazard with the preceding
1032
+ // member ('f = 1\nstatic readonly x' parses cleanly, but 'f = 1\nstatic ;
1033
+ // x' would not). When followed by '{', recurse into the block as plain
1034
+ // JS - the contents are statements, not class members.
1035
+ bool kept_jskw = false;
1036
+ if (bare_type_stripper__at_kw(s, n, i, "static", 6)) {
1037
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 6);
1038
+
1039
+ if (peek < n && s[peek] == '{') {
1040
+ i = peek;
1041
+
1042
+ err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
1043
+ if (err < 0) return err;
1044
+
1045
+ continue;
1046
+ }
1047
+
1048
+ if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '*')) {
1049
+ i = i + 6;
1050
+ kept_jskw = true;
1051
+ }
1052
+ }
1053
+
1054
+ // Track whether we've already emitted a strip range that starts this
1055
+ // class member. The first such range gets the F_SEMI flag so that ASI
1056
+ // can't merge the preceding field's initializer into this one (e.g.
1057
+ // 'field = 1\npublic ["k"] = 2' would parse as '1["k"]' after stripping
1058
+ // 'public' without an injected ';').
1059
+ bool member_strip_seen = kept_jskw;
1060
+
1061
+ // Strip TS modifier keywords. Loop as long as we keep seeing modifiers
1062
+ // followed by another modifier or a member name.
1063
+ for (;;) {
1064
+ i = bare_type_stripper__skip_trivia(s, n, i);
1065
+ if (i >= n || !ids(u(0))) break;
1066
+
1067
+ size_t ks = i;
1068
+ while (i < n && id(u(0))) i++;
1069
+ size_t ke = i;
1070
+ size_t kl = ke - ks;
1071
+
1072
+ bool is_mod = bare_type_stripper__is_ts_modifier(s, ks, kl, true);
1073
+
1074
+ if (!is_mod) {
1075
+ i = ks;
1076
+ break;
1077
+ }
1078
+
1079
+ size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
1080
+
1081
+ // Erase modifier only if followed by another identifier (member name
1082
+ // or another modifier), '[' (computed name), or '*' (generator).
1083
+ if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '*')) {
1084
+ uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
1085
+
1086
+ err = bare_type_stripper__add_range_flags(ctx, ks, peek, flags);
1087
+ if (err < 0) return err;
1088
+
1089
+ member_strip_seen = true;
1090
+ i = peek;
1091
+ continue;
1092
+ }
1093
+
1094
+ i = ks;
1095
+ break;
1096
+ }
1097
+
1098
+ // 'declare' member - erase to end of statement. scan_declare balances
1099
+ // brackets, so an object type in the annotation can't end the strip at
1100
+ // its inner '}'.
1101
+ if (bare_type_stripper__at_kw(s, n, i, "declare", 7)) {
1102
+ size_t ds = i;
1103
+ i = bare_type_stripper__scan_declare(s, n, i + 7);
1104
+
1105
+ uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
1106
+
1107
+ err = bare_type_stripper__add_range_flags(ctx, ds, i, flags);
1108
+ if (err < 0) return err;
1109
+
1110
+ continue;
1111
+ }
1112
+
1113
+ // Index signature '[k: K]: V'
1114
+ if (u(0) == '[') {
1115
+ size_t bs = i;
1116
+ i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
1117
+
1118
+ size_t after = bare_type_stripper__skip_trivia(s, n, i);
1119
+ if (after < n && s[after] == ':') {
1120
+ // Index signature - erase entire signature.
1121
+ i = after + 1;
1122
+ i = bare_type_stripper__scan_type(
1123
+ s,
1124
+ n,
1125
+ i,
1126
+ BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_COMMA
1127
+ );
1128
+ if (i < n && (u(0) == ';' || u(0) == ',')) i++;
1129
+
1130
+ err = bare_type_stripper__add_range(ctx, bs, i);
1131
+ if (err < 0) return err;
1132
+
1133
+ continue;
1134
+ }
1135
+
1136
+ // Otherwise it's a computed member name - rewind and let the member
1137
+ // name parsing below consume it.
1138
+ i = bs;
1139
+ }
1140
+
1141
+ // 'get' / 'set' accessor prefix. Keep it, but consume it here rather
1142
+ // than letting it lex as the member name, so that the member's real
1143
+ // name and signature are processed in this same iteration.
1144
+ if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1145
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 3);
1146
+ if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '#' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1147
+ i = peek;
1148
+ }
1149
+ }
1150
+
1151
+ // Member name.
1152
+ if (u(0) == '*') {
1153
+ i++;
1154
+ i = bare_type_stripper__skip_trivia(s, n, i);
1155
+ }
1156
+ if (u(0) == '#') i++; // Private name
1157
+
1158
+ if (i < n && ids(u(0))) {
1159
+ while (i < n && id(u(0))) i++;
1160
+ } else if (u(0) == '\'' || u(0) == '"') {
1161
+ i = bare_type_stripper__skip_string(s, n, i);
1162
+ } else if (u(0) == '[') {
1163
+ // Computed member name.
1164
+ i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
1165
+ } else if (u(0) >= '0' && u(0) <= '9') {
1166
+ while (i < n && (id(u(0)) || u(0) == '.')) i++;
1167
+ } else {
1168
+ // Unknown - advance one byte to recover.
1169
+ i++;
1170
+ continue;
1171
+ }
1172
+
1173
+ i = bare_type_stripper__skip_trivia(s, n, i);
1174
+
1175
+ // Optional '?' or definite assignment '!:' on a property.
1176
+ if (i < n && u(0) == '?') {
1177
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
1178
+ if (peek < n && (s[peek] == ':' || s[peek] == '(' || s[peek] == ';' || s[peek] == '=' || s[peek] == '}' || lt(s[peek]) || s[peek] == ',')) {
1179
+ err = bare_type_stripper__add_range(ctx, i, i + 1);
1180
+ if (err < 0) return err;
1181
+
1182
+ i++;
1183
+ i = bare_type_stripper__skip_trivia(s, n, i);
1184
+ }
1185
+ } else if (i < n && u(0) == '!') {
1186
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
1187
+ if (peek < n && s[peek] == ':') {
1188
+ err = bare_type_stripper__add_range(ctx, i, i + 1);
1189
+ if (err < 0) return err;
1190
+
1191
+ i++;
1192
+ i = bare_type_stripper__skip_trivia(s, n, i);
1193
+ }
1194
+ }
1195
+
1196
+ // Method generics '<T>'.
1197
+ if (i < n && u(0) == '<') {
1198
+ size_t gs = i;
1199
+ size_t ge = bare_type_stripper__scan_generic(s, n, i);
1200
+
1201
+ if (ge > i + 1) {
1202
+ size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
1203
+ if (peek < n && s[peek] == '(') {
1204
+ err = bare_type_stripper__add_range(ctx, gs, ge);
1205
+ if (err < 0) return err;
1206
+
1207
+ i = ge;
1208
+ i = bare_type_stripper__skip_trivia(s, n, i);
1209
+ }
1210
+ }
1211
+ }
1212
+
1213
+ if (i >= n) break;
1214
+
1215
+ if (u(0) == '(') {
1216
+ // Method.
1217
+ err = bare_type_stripper__process_params(ctx, &i, depth);
1218
+ if (err < 0) return err;
1219
+
1220
+ i = bare_type_stripper__skip_trivia(s, n, i);
1221
+
1222
+ // Return type annotation.
1223
+ if (i < n && u(0) == ':') {
1224
+ size_t ts = i;
1225
+ i++;
1226
+ i = bare_type_stripper__scan_type(
1227
+ s,
1228
+ n,
1229
+ i,
1230
+ BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
1231
+ );
1232
+
1233
+ err = bare_type_stripper__add_range(ctx, ts, i);
1234
+ if (err < 0) return err;
1235
+
1236
+ i = bare_type_stripper__skip_trivia(s, n, i);
1237
+ }
1238
+
1239
+ // Method body. Without one this is an overload or abstract method
1240
+ // signature, which has no runtime semantics: Drop the ranges emitted
1241
+ // for its pieces and erase the whole member as one range instead -
1242
+ // leaving a bodyless 'f()' behind would produce invalid runtime JS.
1243
+ // The injected ';' keeps ASI from folding a preceding field
1244
+ // initializer into whatever follows the erased member.
1245
+ if (i < n && u(0) == '{') {
1246
+ err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
1247
+ if (err < 0) return err;
1248
+ } else {
1249
+ if (i < n && u(0) == ';') i++;
1250
+
1251
+ ctx->len = member_mark;
1252
+
1253
+ err = bare_type_stripper__add_range_flags(ctx, member_start, i, bare_type_stripper_semi);
1254
+ if (err < 0) return err;
1255
+ }
1256
+
1257
+ continue;
1258
+ }
1259
+
1260
+ if (u(0) == ':') {
1261
+ // Property type annotation.
1262
+ size_t ts = i;
1263
+ i++;
1264
+ i = bare_type_stripper__scan_type(
1265
+ s,
1266
+ n,
1267
+ i,
1268
+ BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_EQ | BARE_TYPE_STRIPPER__T_NL | BARE_TYPE_STRIPPER__T_BRACE
1269
+ );
1270
+
1271
+ err = bare_type_stripper__add_range(ctx, ts, i);
1272
+ if (err < 0) return err;
1273
+
1274
+ i = bare_type_stripper__skip_trivia(s, n, i);
1275
+ }
1276
+
1277
+ // Initializer. Pass stop_at_asi so the walker stops at a newline at
1278
+ // depth 0 (after an expression-ending token) - otherwise it would chew
1279
+ // past the next class element when the field has no trailing ';'.
1280
+ if (i < n && u(0) == '=') {
1281
+ i++;
1282
+
1283
+ err = bare_type_stripper__walk(ctx, &i, ';', false, true, depth + 1);
1284
+ if (err < 0) return err;
1285
+
1286
+ if (i < n && u(0) == ';') i++;
1287
+ }
1288
+ }
1289
+
1290
+ *result = i;
1291
+ return 0;
1292
+ }
1293
+
1294
+ // True when the property starting at i parses as a method head - i.e.
1295
+ // '[async] [*] [get|set] KEY [<...>]' directly followed by '(' - meaning
1296
+ // the property is a shorthand method whose signature needs type stripping.
1297
+ // Pure lookahead: Consumes nothing and emits no ranges.
1298
+ static inline bool
1299
+ bare_type_stripper__at_method_head(const utf8_t *s, size_t n, size_t i) {
1300
+ // 'async' prefix.
1301
+ if (bare_type_stripper__at_kw(s, n, i, "async", 5)) {
1302
+ size_t p = bare_type_stripper__skip_trivia(s, n, i + 5);
1303
+ if (p < n && (ids(s[p]) || s[p] == '*' || s[p] == '[' || s[p] == '\'' || s[p] == '"' || (s[p] >= '0' && s[p] <= '9'))) i = p;
1304
+ }
1305
+
1306
+ // Generator marker.
1307
+ if (i < n && s[i] == '*') i = bare_type_stripper__skip_trivia(s, n, i + 1);
1308
+
1309
+ // 'get' / 'set' accessor prefix.
1310
+ if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1311
+ size_t p = bare_type_stripper__skip_trivia(s, n, i + 3);
1312
+ if (p < n && (ids(s[p]) || s[p] == '[' || s[p] == '\'' || s[p] == '"' || (s[p] >= '0' && s[p] <= '9'))) i = p;
1313
+ }
1314
+
1315
+ // Key.
1316
+ if (i < n && ids(s[i])) {
1317
+ while (i < n && id(s[i])) i++;
1318
+ } else if (i < n && (s[i] == '\'' || s[i] == '"')) {
1319
+ i = bare_type_stripper__skip_string(s, n, i);
1320
+ } else if (i < n && s[i] == '[') {
1321
+ i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
1322
+ } else if (i < n && s[i] >= '0' && s[i] <= '9') {
1323
+ while (i < n && (id(s[i]) || s[i] == '.')) i++;
1324
+ } else {
1325
+ return false;
1326
+ }
1327
+
1328
+ i = bare_type_stripper__skip_trivia(s, n, i);
1329
+
1330
+ // Method generics '<T>'.
1331
+ if (i < n && s[i] == '<') {
1332
+ size_t ge = bare_type_stripper__scan_generic(s, n, i);
1333
+ if (ge <= i + 1) return false;
1334
+ i = bare_type_stripper__skip_trivia(s, n, ge);
1335
+ }
1336
+
1337
+ return i < n && s[i] == '(';
1338
+ }
1339
+
1340
+ // Process the body of an object literal. Expects s[i] == '{'. Walks until
1341
+ // the matching '}'. Properties are walked as plain expression code, except
1342
+ // shorthand methods, whose signatures (generics, parameter annotations,
1343
+ // return types) are stripped like class methods.
1344
+ static inline int
1345
+ bare_type_stripper__process_object_literal(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
1346
+ const utf8_t *s = ctx->s;
1347
+ size_t n = ctx->n;
1348
+
1349
+ int err;
1350
+ size_t i = *result;
1351
+
1352
+ if (u(0) != '{') return 0;
1353
+ i++;
1354
+
1355
+ while (i < n) {
1356
+ i = bare_type_stripper__skip_trivia(s, n, i);
1357
+ if (i >= n) break;
1358
+
1359
+ if (u(0) == '}') {
1360
+ i++;
1361
+ break;
1362
+ }
1363
+ if (u(0) == ',') {
1364
+ i++;
1365
+ continue;
1366
+ }
1367
+ if (u(0) == ')') {
1368
+ // Unmatched ')' - skip it so the loop can't wedge on malformed input.
1369
+ i++;
1370
+ continue;
1371
+ }
1372
+
1373
+ if (bare_type_stripper__at_method_head(s, n, i)) {
1374
+ // 'async' prefix.
1375
+ if (bare_type_stripper__at_kw(s, n, i, "async", 5)) {
1376
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 5);
1377
+ if (peek < n && (ids(s[peek]) || s[peek] == '*' || s[peek] == '[' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1378
+ i = peek;
1379
+ }
1380
+ }
1381
+
1382
+ // Generator marker.
1383
+ if (u(0) == '*') {
1384
+ i = bare_type_stripper__skip_trivia(s, n, i + 1);
1385
+ }
1386
+
1387
+ // 'get' / 'set' accessor prefix.
1388
+ if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1389
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 3);
1390
+ if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1391
+ i = peek;
1392
+ }
1393
+ }
1394
+
1395
+ // Key.
1396
+ if (i < n && ids(u(0))) {
1397
+ while (i < n && id(u(0))) i++;
1398
+ } else if (i < n && (u(0) == '\'' || u(0) == '"')) {
1399
+ i = bare_type_stripper__skip_string(s, n, i);
1400
+ } else if (i < n && u(0) == '[') {
1401
+ i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
1402
+ } else if (i < n && u(0) >= '0' && u(0) <= '9') {
1403
+ while (i < n && (id(u(0)) || u(0) == '.')) i++;
1404
+ }
1405
+
1406
+ i = bare_type_stripper__skip_trivia(s, n, i);
1407
+
1408
+ // Method generics '<T>'.
1409
+ err = bare_type_stripper__strip_generic(ctx, &i);
1410
+ if (err < 0) return err;
1411
+
1412
+ i = bare_type_stripper__skip_trivia(s, n, i);
1413
+
1414
+ // Parameter list.
1415
+ if (i < n && u(0) == '(') {
1416
+ err = bare_type_stripper__process_params(ctx, &i, depth);
1417
+ if (err < 0) return err;
1418
+ }
1419
+
1420
+ i = bare_type_stripper__skip_trivia(s, n, i);
1421
+
1422
+ // Return type annotation.
1423
+ if (i < n && u(0) == ':') {
1424
+ size_t ts = i;
1425
+ i++;
1426
+ i = bare_type_stripper__scan_type(
1427
+ s,
1428
+ n,
1429
+ i,
1430
+ BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
1431
+ );
1432
+
1433
+ err = bare_type_stripper__add_range(ctx, ts, i);
1434
+ if (err < 0) return err;
1435
+
1436
+ i = bare_type_stripper__skip_trivia(s, n, i);
1437
+ }
1438
+
1439
+ // Method body.
1440
+ if (i < n && u(0) == '{') {
1441
+ err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
1442
+ if (err < 0) return err;
1443
+ }
1444
+
1445
+ continue;
1446
+ }
1447
+
1448
+ // Anything else - spread, shorthand, or 'key: value' - is plain
1449
+ // expression code up to the next depth-0 ',' or the closing '}'.
1450
+ err = bare_type_stripper__walk(ctx, &i, ',', false, false, depth + 1);
1451
+ if (err < 0) return err;
1452
+ }
1453
+
1454
+ *result = i;
1455
+ return 0;
1456
+ }
1457
+
1458
+ // Scan past an 'enum IDENT { ... }' construct. Assumes s[i] points at the
1459
+ // 'enum' keyword. Returns the index after the matching '}'. Used to mark
1460
+ // the whole construct as a non-erasable range.
1461
+ static inline size_t
1462
+ bare_type_stripper__scan_enum(const utf8_t *s, size_t n, size_t i) {
1463
+ i = bare_type_stripper__skip_name(s, n, i + 4);
1464
+
1465
+ if (i < n && u(0) == '{') {
1466
+ i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
1467
+ }
1468
+
1469
+ return i;
1470
+ }
1471
+
1472
+ // Scan past an 'interface IDENT [<...>] [extends ...] { ... }' construct
1473
+ // without emitting any strip ranges. Assumes s[i] points at 'interface'.
1474
+ // Returns the index after the matching '}'.
1475
+ static inline size_t
1476
+ bare_type_stripper__scan_interface(const utf8_t *s, size_t n, size_t i) {
1477
+ i = bare_type_stripper__skip_name(s, n, i + 9);
1478
+
1479
+ if (i < n && u(0) == '<') {
1480
+ i = bare_type_stripper__scan_generic(s, n, i);
1481
+ i = bare_type_stripper__skip_trivia(s, n, i);
1482
+ }
1483
+
1484
+ // Optional 'extends X, Y, ...'.
1485
+ if (bare_type_stripper__at_kw(s, n, i, "extends", 7)) {
1486
+ i += 7;
1487
+ int depth = 0;
1488
+
1489
+ while (i < n) {
1490
+ if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
1491
+
1492
+ uint8_t cc = u(0);
1493
+
1494
+ if (cc == '<' || cc == '(' || cc == '[') {
1495
+ depth++;
1496
+ i++;
1497
+ continue;
1498
+ }
1499
+ if (cc == '>' || cc == ')' || cc == ']') {
1500
+ if (depth == 0) break;
1501
+ depth--;
1502
+ i++;
1503
+ continue;
1504
+ }
1505
+ if (cc == '{' && depth == 0) break;
1506
+
1507
+ i++;
1508
+ }
1509
+ }
1510
+
1511
+ if (i < n && u(0) == '{') {
1512
+ i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
1513
+ }
1514
+
1515
+ return i;
1516
+ }
1517
+
1518
+ // Process a top-level 'interface IDENT [...] { ... }' declaration. Emits a
1519
+ // single SEMI-flagged range covering the whole construct.
1520
+ static inline int
1521
+ bare_type_stripper__process_interface(bare_type_stripper_t *ctx, size_t *result) {
1522
+ const utf8_t *s = ctx->s;
1523
+ size_t n = ctx->n;
1524
+
1525
+ int err;
1526
+ size_t start = *result;
1527
+ size_t end = bare_type_stripper__scan_interface(s, n, start);
1528
+
1529
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1530
+ if (err < 0) return err;
1531
+
1532
+ *result = end;
1533
+ return 0;
1534
+ }
1535
+
1536
+ // Scan past the remainder of a 'type IDENT [<...>] = T;' alias. On entry, i
1537
+ // points just past the 'type' keyword. Returns the index past the alias and
1538
+ // its terminating ';' when present.
1539
+ static inline size_t
1540
+ bare_type_stripper__scan_type_alias(const utf8_t *s, size_t n, size_t i) {
1541
+ i = bare_type_stripper__skip_name(s, n, i);
1542
+
1543
+ if (i < n && u(0) == '<') {
1544
+ i = bare_type_stripper__scan_generic(s, n, i);
1545
+ i = bare_type_stripper__skip_trivia(s, n, i);
1546
+ }
1547
+
1548
+ if (i < n && u(0) == '=') {
1549
+ i++;
1550
+ i = bare_type_stripper__scan_type(s, n, i, BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL);
1551
+ }
1552
+
1553
+ if (i < n && u(0) == ';') i++;
1554
+
1555
+ return i;
1556
+ }
1557
+
1558
+ // Process 'type IDENT [<...>] = T;'. Emits the whole declaration as a range.
1559
+ static inline int
1560
+ bare_type_stripper__process_type_alias(bare_type_stripper_t *ctx, size_t *result) {
1561
+ int err;
1562
+ size_t start = *result;
1563
+ size_t end = bare_type_stripper__scan_type_alias(ctx->s, ctx->n, start + 4);
1564
+
1565
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1566
+ if (err < 0) return err;
1567
+
1568
+ *result = end;
1569
+ return 0;
1570
+ }
1571
+
1572
+ // Scan past the remainder of a 'declare ...' construct. On entry, i points
1573
+ // just past the 'declare' keyword. Returns the index past the declared body
1574
+ // or the terminating ';' / newline.
1575
+ static inline size_t
1576
+ bare_type_stripper__scan_declare(const utf8_t *s, size_t n, size_t i) {
1577
+ // Balance '()'/'[]'/'{}' uniformly, but only treat a closing '}' at outer
1578
+ // depth as the end of the declared body. This matters for 'declare function
1579
+ // f(): void' (no body): The ')' of params closes at depth 0 but isn't a
1580
+ // body, so we keep scanning past the return type until a ';' or newline.
1581
+ int depth = 0;
1582
+
1583
+ while (i < n) {
1584
+ uint8_t cc = u(0);
1585
+
1586
+ // A newline at depth 0 ends the construct, so check it before the
1587
+ // generic trivia skip (which would consume it).
1588
+ if (lt(cc) && depth == 0) break;
1589
+
1590
+ if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
1591
+
1592
+ if (cc == '{' || cc == '(' || cc == '[') {
1593
+ depth++;
1594
+ i++;
1595
+ continue;
1596
+ }
1597
+ if (cc == '}' || cc == ')' || cc == ']') {
1598
+ if (depth == 0) break;
1599
+ depth--;
1600
+ i++;
1601
+ if (depth == 0 && cc == '}') break;
1602
+ continue;
1603
+ }
1604
+
1605
+ if (cc == ';' && depth == 0) {
1606
+ i++;
1607
+ break;
1608
+ }
1609
+
1610
+ i++;
1611
+ }
1612
+
1613
+ return i;
1614
+ }
1615
+
1616
+ // Process 'declare ...' at statement position.
1617
+ static inline int
1618
+ bare_type_stripper__process_declare(bare_type_stripper_t *ctx, size_t *result) {
1619
+ const utf8_t *s = ctx->s;
1620
+ size_t n = ctx->n;
1621
+
1622
+ int err;
1623
+ size_t start = *result;
1624
+ size_t end = bare_type_stripper__scan_declare(s, n, start + 7);
1625
+
1626
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1627
+ if (err < 0) return err;
1628
+
1629
+ *result = end;
1630
+ return 0;
1631
+ }
1632
+
1633
+ // Process the contents of an import or export named specifier list, the
1634
+ // '{ ... }' body. Expects s[i] == '{'. Erases type-only specifiers ('type
1635
+ // Foo', 'type Foo as Bar') entirely, including the separating comma so the
1636
+ // remaining value specifiers stay syntactically valid. Leaves cursor just
1637
+ // past the matching '}'.
1638
+ //
1639
+ // `allow_star` is true for import lists - which can be parsed by callers
1640
+ // that also want to handle 'import * as X from "..."' (the '*' itself is at
1641
+ // outer scope, but a defensive 'type *' check stays consistent there) - and
1642
+ // false for export lists where '*' isn't allowed inside the braces.
1643
+ static inline int
1644
+ bare_type_stripper__process_named_specifiers(bare_type_stripper_t *ctx, size_t *result, bool allow_star) {
1645
+ const utf8_t *s = ctx->s;
1646
+ size_t n = ctx->n;
1647
+
1648
+ int err;
1649
+ size_t i = *result;
1650
+
1651
+ if (i >= n || u(0) != '{') return 0;
1652
+ i++;
1653
+
1654
+ // Position just after the last kept (non-type) specifier's content, before
1655
+ // its trailing comma if any. Used to absorb the preceding comma when the
1656
+ // final specifier in the list is type-only.
1657
+ size_t prev_kept_end = i;
1658
+
1659
+ while (i < n) {
1660
+ i = bare_type_stripper__skip_trivia(s, n, i);
1661
+ if (i >= n) break;
1662
+ if (u(0) == '}') break;
1663
+
1664
+ size_t spec_start = i;
1665
+ bool is_type = false;
1666
+
1667
+ if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
1668
+ size_t after = bare_type_stripper__skip_trivia(s, n, i + 4);
1669
+ if (after < n && (ids(s[after]) || (allow_star && s[after] == '*'))) {
1670
+ is_type = true;
1671
+ i = after;
1672
+ }
1673
+ }
1674
+
1675
+ // Specifier name is either an identifier, '*' (import only), or a string
1676
+ // literal (arbitrary module namespace names, e.g. 'import { "x" as C }').
1677
+ if (i < n && (u(0) == '\'' || u(0) == '"')) {
1678
+ i = bare_type_stripper__skip_string(s, n, i);
1679
+ } else if (allow_star && i < n && u(0) == '*') {
1680
+ i++;
1681
+ } else {
1682
+ while (i < n && id(u(0))) i++;
1683
+ }
1684
+ size_t spec_end = i;
1685
+
1686
+ i = bare_type_stripper__skip_trivia(s, n, i);
1687
+ if (bare_type_stripper__at_kw(s, n, i, "as", 2)) {
1688
+ i += 2;
1689
+ i = bare_type_stripper__skip_trivia(s, n, i);
1690
+ if (i < n && (u(0) == '\'' || u(0) == '"')) {
1691
+ i = bare_type_stripper__skip_string(s, n, i);
1692
+ } else {
1693
+ while (i < n && id(u(0))) i++;
1694
+ }
1695
+ spec_end = i;
1696
+ i = bare_type_stripper__skip_trivia(s, n, i);
1697
+ }
1698
+
1699
+ bool has_comma = false;
1700
+ size_t after_comma = spec_end;
1701
+ if (i < n && u(0) == ',') {
1702
+ has_comma = true;
1703
+ i++;
1704
+ after_comma = i;
1705
+ }
1706
+
1707
+ if (is_type) {
1708
+ size_t strip_start = has_comma ? spec_start : prev_kept_end;
1709
+
1710
+ err = bare_type_stripper__add_range(ctx, strip_start, after_comma);
1711
+ if (err < 0) return err;
1712
+ } else {
1713
+ prev_kept_end = spec_end;
1714
+ }
1715
+ }
1716
+
1717
+ if (i < n && u(0) == '}') i++;
1718
+
1719
+ *result = i;
1720
+ return 0;
1721
+ }
1722
+
1723
+ // Process 'import' at statement position. Handles 'import type ...' and
1724
+ // 'import { type X, ... }'. Leaves dynamic import and import.meta alone.
1725
+ static inline int
1726
+ bare_type_stripper__process_import(bare_type_stripper_t *ctx, size_t *result) {
1727
+ const utf8_t *s = ctx->s;
1728
+ size_t n = ctx->n;
1729
+
1730
+ int err;
1731
+ size_t i = *result;
1732
+ size_t start = i;
1733
+ i += 6;
1734
+
1735
+ size_t after_kw = bare_type_stripper__skip_trivia(s, n, i);
1736
+ if (after_kw < n && (s[after_kw] == '(' || s[after_kw] == '.')) {
1737
+ *result = i;
1738
+ return 0;
1739
+ }
1740
+ i = after_kw;
1741
+
1742
+ // 'import type ...' - erase whole statement.
1743
+ if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
1744
+ i += 4;
1745
+
1746
+ while (i < n) {
1747
+ uint8_t cc = u(0);
1748
+ if (cc == '\'' || cc == '"') {
1749
+ i = bare_type_stripper__skip_string(s, n, i);
1750
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i);
1751
+ if (peek < n && s[peek] == ';') i = peek + 1;
1752
+ break;
1753
+ }
1754
+ if (cc == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
1755
+ i = bare_type_stripper__skip_trivia(s, n, i);
1756
+ continue;
1757
+ }
1758
+ if (cc == ';') {
1759
+ i++;
1760
+ break;
1761
+ }
1762
+ i++;
1763
+ }
1764
+
1765
+ err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
1766
+ if (err < 0) return err;
1767
+
1768
+ *result = i;
1769
+ return 0;
1770
+ }
1771
+
1772
+ // Walk the import clause looking for '{...}' named list. Type-only
1773
+ // specifiers ('type Foo', 'type Foo as Bar') are erased entirely along
1774
+ // with their separating comma, since the named binding would otherwise be
1775
+ // requested as a value at runtime and fail.
1776
+ while (i < n) {
1777
+ uint8_t cc = u(0);
1778
+ if (ws(cc)) {
1779
+ i++;
1780
+ continue;
1781
+ }
1782
+ if (cc == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
1783
+ i = bare_type_stripper__skip_trivia(s, n, i);
1784
+ continue;
1785
+ }
1786
+ if (cc == '\'' || cc == '"') {
1787
+ i = bare_type_stripper__skip_string(s, n, i);
1788
+ break;
1789
+ }
1790
+
1791
+ if (cc == '{') {
1792
+ err = bare_type_stripper__process_named_specifiers(ctx, &i, true);
1793
+ if (err < 0) return err;
1794
+ continue;
1795
+ }
1796
+
1797
+ if (cc == ';') {
1798
+ i++;
1799
+ break;
1800
+ }
1801
+
1802
+ i++;
1803
+ }
1804
+
1805
+ *result = i;
1806
+ return 0;
1807
+ }
1808
+
1809
+ // Process 'export' at statement position.
1810
+ static inline int
1811
+ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
1812
+ const utf8_t *s = ctx->s;
1813
+ size_t n = ctx->n;
1814
+
1815
+ int err;
1816
+ size_t i = *result;
1817
+ size_t start = i;
1818
+ i += 6;
1819
+
1820
+ i = bare_type_stripper__skip_trivia(s, n, i);
1821
+ if (i >= n) {
1822
+ *result = i;
1823
+ return 0;
1824
+ }
1825
+
1826
+ // 'export type ...' - either an exported alias ('export type Foo = ...')
1827
+ // or a type-only re-export ('export type { ... } from ...', 'export type
1828
+ // * as NS from ...'). Both are erased whole, including 'export'.
1829
+ if (bare_type_stripper__at_kw(s, n, i, "type", 4)) {
1830
+ size_t after = bare_type_stripper__skip_trivia(s, n, i + 4);
1831
+
1832
+ // Alias form - delegate to the type-alias scanner so multi-line types
1833
+ // behave exactly like a plain 'type' declaration.
1834
+ if (after < n && ids(s[after])) {
1835
+ size_t end = bare_type_stripper__scan_type_alias(s, n, i + 4);
1836
+
1837
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1838
+ if (err < 0) return err;
1839
+
1840
+ *result = end;
1841
+ return 0;
1842
+ }
1843
+
1844
+ // Re-export form - scan the specifier clause, the optional 'from'
1845
+ // clause, and the terminating ';'.
1846
+ if (after < n && (s[after] == '{' || s[after] == '*')) {
1847
+ i = after;
1848
+
1849
+ if (u(0) == '{') {
1850
+ i = bare_type_stripper__scan_balanced(s, n, i, '{', '}');
1851
+ } else {
1852
+ // '* [as NS]'.
1853
+ i++;
1854
+
1855
+ size_t p = bare_type_stripper__skip_trivia(s, n, i);
1856
+ if (bare_type_stripper__at_kw(s, n, p, "as", 2)) {
1857
+ p = bare_type_stripper__skip_trivia(s, n, p + 2);
1858
+ while (p < n && id(s[p])) p++;
1859
+ i = p;
1860
+ }
1861
+ }
1862
+
1863
+ size_t p = bare_type_stripper__skip_trivia(s, n, i);
1864
+ if (bare_type_stripper__at_kw(s, n, p, "from", 4)) {
1865
+ p = bare_type_stripper__skip_trivia(s, n, p + 4);
1866
+ if (p < n && (s[p] == '\'' || s[p] == '"')) {
1867
+ i = bare_type_stripper__skip_string(s, n, p);
1868
+ }
1869
+ }
1870
+
1871
+ size_t semi = bare_type_stripper__skip_trivia(s, n, i);
1872
+ if (semi < n && s[semi] == ';') i = semi + 1;
1873
+
1874
+ err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
1875
+ if (err < 0) return err;
1876
+
1877
+ *result = i;
1878
+ return 0;
1879
+ }
1880
+ }
1881
+
1882
+ // 'export declare ...' - the whole statement, including 'export', is
1883
+ // ambient and erased as one range; stripping only from 'declare' onward
1884
+ // would leave an invalid bare 'export ;' behind.
1885
+ if (bare_type_stripper__at_kw(s, n, i, "declare", 7)) {
1886
+ size_t after = bare_type_stripper__skip_trivia(s, n, i + 7);
1887
+ if (after < n && ids(s[after])) {
1888
+ size_t end = bare_type_stripper__scan_declare(s, n, i + 7);
1889
+
1890
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1891
+ if (err < 0) return err;
1892
+
1893
+ *result = end;
1894
+ return 0;
1895
+ }
1896
+ }
1897
+
1898
+ // 'export interface ...' - erase as one range covering 'export ' too.
1899
+ if (bare_type_stripper__at_kw(s, n, i, "interface", 9)) {
1900
+ size_t end = bare_type_stripper__scan_interface(s, n, i);
1901
+
1902
+ err = bare_type_stripper__add_range_flags(ctx, start, end, bare_type_stripper_semi);
1903
+ if (err < 0) return err;
1904
+
1905
+ *result = end;
1906
+ return 0;
1907
+ }
1908
+
1909
+ // 'export [default] function ...' - delegate, threading through the
1910
+ // statement start so a bodyless overload signature erases the 'export'
1911
+ // prefix too instead of leaving a dangling 'export ;' behind.
1912
+ size_t p = i;
1913
+
1914
+ if (bare_type_stripper__at_kw(s, n, p, "default", 7)) {
1915
+ p = bare_type_stripper__skip_trivia(s, n, p + 7);
1916
+ }
1917
+
1918
+ if (bare_type_stripper__at_kw(s, n, p, "function", 8)) {
1919
+ err = bare_type_stripper__process_function(ctx, &p, start, depth);
1920
+ if (err < 0) return err;
1921
+
1922
+ *result = p;
1923
+ return 0;
1924
+ }
1925
+
1926
+ // 'export { ... }' - erase type-only specifiers entirely, including the
1927
+ // comma that separates them from value specifiers.
1928
+ if (u(0) == '{') {
1929
+ err = bare_type_stripper__process_named_specifiers(ctx, &i, false);
1930
+ if (err < 0) return err;
1931
+ }
1932
+
1933
+ *result = i;
1934
+ return 0;
1935
+ }
1936
+
1937
+ // Process a variable declaration sequence (the right-hand side of const/let/var).
1938
+ // On entry, i points just past the keyword.
1939
+ static inline int
1940
+ bare_type_stripper__process_var_decl(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
1941
+ const utf8_t *s = ctx->s;
1942
+ size_t n = ctx->n;
1943
+
1944
+ int err;
1945
+ size_t i = *result;
1946
+
1947
+ while (i < n) {
1948
+ i = bare_type_stripper__skip_trivia(s, n, i);
1949
+ if (i >= n) break;
1950
+
1951
+ // Destructuring pattern or identifier.
1952
+ if (u(0) == '{' || u(0) == '[') {
1953
+ uint8_t open = u(0);
1954
+ uint8_t close = open == '{' ? '}' : ']';
1955
+ i = bare_type_stripper__scan_balanced(s, n, i, open, close);
1956
+ } else if (ids(u(0))) {
1957
+ while (i < n && id(u(0))) i++;
1958
+ } else {
1959
+ break;
1960
+ }
1961
+
1962
+ i = bare_type_stripper__skip_trivia(s, n, i);
1963
+
1964
+ // Definite assignment '!:'.
1965
+ if (i < n && u(0) == '!') {
1966
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 1);
1967
+ if (peek < n && s[peek] == ':') {
1968
+ err = bare_type_stripper__add_range(ctx, i, i + 1);
1969
+ if (err < 0) return err;
1970
+
1971
+ i++;
1972
+ i = bare_type_stripper__skip_trivia(s, n, i);
1973
+ }
1974
+ }
1975
+
1976
+ // Type annotation ': T'. T_INOF stops the scan at a bare 'in'/'of'
1977
+ // identifier - needed for 'for (const x: T of arr)' headers, harmless
1978
+ // elsewhere since neither word appears at depth 0 of a valid type.
1979
+ if (i < n && u(0) == ':') {
1980
+ size_t ts = i;
1981
+ i++;
1982
+ i = bare_type_stripper__scan_type(
1983
+ s,
1984
+ n,
1985
+ i,
1986
+ 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
1987
+ );
1988
+
1989
+ err = bare_type_stripper__add_range(ctx, ts, i);
1990
+ if (err < 0) return err;
1991
+
1992
+ i = bare_type_stripper__skip_trivia(s, n, i);
1993
+ }
1994
+
1995
+ // Initializer. stop_at_asi keeps the walker from chewing past a newline
1996
+ // into the next statement when the declaration has no trailing ';',
1997
+ // which would otherwise miss type annotations on the following decl.
1998
+ if (i < n && u(0) == '=') {
1999
+ i++;
2000
+
2001
+ err = bare_type_stripper__walk(ctx, &i, ';', false, true, depth + 1);
2002
+ if (err < 0) return err;
2003
+ }
2004
+
2005
+ i = bare_type_stripper__skip_trivia(s, n, i);
2006
+ if (i < n && u(0) == ',') {
2007
+ i++;
2008
+ continue;
2009
+ }
2010
+ break;
2011
+ }
2012
+
2013
+ *result = i;
2014
+ return 0;
2015
+ }
2016
+
2017
+ // Process 'function ...'. On entry, *result points at 'function' and
2018
+ // stmt_start at the start of the enclosing statement - usually the same
2019
+ // position, but earlier when the declaration is prefixed by
2020
+ // 'export [default]'. A bodyless signature (an overload) is erased whole,
2021
+ // starting from stmt_start so no dangling 'export' survives.
2022
+ static inline int
2023
+ bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, size_t stmt_start, uint32_t depth) {
2024
+ const utf8_t *s = ctx->s;
2025
+ size_t n = ctx->n;
2026
+
2027
+ int err;
2028
+ uint32_t mark = ctx->len;
2029
+ size_t i = *result;
2030
+ i += 8;
2031
+
2032
+ i = bare_type_stripper__skip_trivia(s, n, i);
2033
+
2034
+ // 'function' used as a property key ('{ function: 1 }') rather than a
2035
+ // declaration or expression - leave it alone. A real function never has
2036
+ // ':' directly after the keyword.
2037
+ if (i < n && u(0) == ':') {
2038
+ *result += 8;
2039
+ return 0;
2040
+ }
2041
+ if (i < n && u(0) == '*') {
2042
+ i++;
2043
+ i = bare_type_stripper__skip_trivia(s, n, i);
2044
+ }
2045
+
2046
+ i = bare_type_stripper__skip_name(s, n, i);
2047
+
2048
+ err = bare_type_stripper__strip_generic(ctx, &i);
2049
+ if (err < 0) return err;
2050
+
2051
+ i = bare_type_stripper__skip_trivia(s, n, i);
2052
+
2053
+ if (i < n && u(0) == '(') {
2054
+ err = bare_type_stripper__process_params(ctx, &i, depth);
2055
+ if (err < 0) return err;
2056
+ }
2057
+
2058
+ i = bare_type_stripper__skip_trivia(s, n, i);
2059
+
2060
+ if (i < n && u(0) == ':') {
2061
+ size_t ts = i;
2062
+ i++;
2063
+ i = bare_type_stripper__scan_type(
2064
+ s,
2065
+ n,
2066
+ i,
2067
+ BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
2068
+ );
2069
+
2070
+ err = bare_type_stripper__add_range(ctx, ts, i);
2071
+ if (err < 0) return err;
2072
+
2073
+ i = bare_type_stripper__skip_trivia(s, n, i);
2074
+ }
2075
+
2076
+ if (i < n && u(0) == '{') {
2077
+ err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
2078
+ if (err < 0) return err;
2079
+ } else {
2080
+ // Overload signature: A function declaration without a body, terminated
2081
+ // by ';' or a line break. Drop the ranges emitted for its pieces and
2082
+ // erase the whole signature as one range instead - leaving a bodyless
2083
+ // 'function f()' behind would produce invalid runtime JS. The injected
2084
+ // ';' keeps ASI from folding the signature line into its neighbors.
2085
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i);
2086
+ if (peek < n && s[peek] == ';') i = peek + 1;
2087
+
2088
+ ctx->len = mark;
2089
+
2090
+ err = bare_type_stripper__add_range_flags(ctx, stmt_start, i, bare_type_stripper_semi);
2091
+ if (err < 0) return err;
2092
+ }
2093
+
2094
+ *result = i;
2095
+ return 0;
2096
+ }
2097
+
2098
+ // Process 'class ...'. On entry, i points at 'class'.
2099
+ static inline int
2100
+ bare_type_stripper__process_class(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
2101
+ const utf8_t *s = ctx->s;
2102
+ size_t n = ctx->n;
2103
+
2104
+ int err;
2105
+ size_t i = *result;
2106
+
2107
+ i = bare_type_stripper__skip_name(s, n, i + 5);
2108
+
2109
+ err = bare_type_stripper__strip_generic(ctx, &i);
2110
+ if (err < 0) return err;
2111
+
2112
+ i = bare_type_stripper__skip_trivia(s, n, i);
2113
+
2114
+ // 'extends X[<...>]' - keep but strip generic arguments and 'implements'.
2115
+ for (;;) {
2116
+ if (bare_type_stripper__at_kw(s, n, i, "extends", 7)) {
2117
+ i += 7;
2118
+ i = bare_type_stripper__skip_trivia(s, n, i);
2119
+
2120
+ // Skip the expression to the next significant boundary, but strip any
2121
+ // '<...>' generic arguments that appear at top level.
2122
+ int depth = 0;
2123
+
2124
+ while (i < n) {
2125
+ if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
2126
+
2127
+ uint8_t cc = u(0);
2128
+
2129
+ if (depth == 0 && cc == '<') {
2130
+ size_t before = i;
2131
+
2132
+ err = bare_type_stripper__strip_generic(ctx, &i);
2133
+ if (err < 0) return err;
2134
+
2135
+ if (i == before) i++;
2136
+ continue;
2137
+ }
2138
+
2139
+ if (cc == '(' || cc == '[' || cc == '{') {
2140
+ if (depth == 0 && cc == '{') break;
2141
+ depth++;
2142
+ i++;
2143
+ continue;
2144
+ }
2145
+ if (cc == ')' || cc == ']' || cc == '}') {
2146
+ if (depth == 0) break;
2147
+ depth--;
2148
+ i++;
2149
+ continue;
2150
+ }
2151
+
2152
+ if (depth == 0 && cc == ',') {
2153
+ i++;
2154
+ break;
2155
+ }
2156
+ if (depth == 0 && ids(cc)) {
2157
+ // Could be 'implements' keyword.
2158
+ if (bare_type_stripper__at_kw(s, n, i, "implements", 10)) break;
2159
+ while (i < n && id(u(0))) i++;
2160
+ continue;
2161
+ }
2162
+
2163
+ i++;
2164
+ }
2165
+
2166
+ i = bare_type_stripper__skip_trivia(s, n, i);
2167
+ continue;
2168
+ }
2169
+
2170
+ if (bare_type_stripper__at_kw(s, n, i, "implements", 10)) {
2171
+ size_t ims = i;
2172
+ i += 10;
2173
+ int depth = 0;
2174
+
2175
+ while (i < n) {
2176
+ if (bare_type_stripper__try_skip_inert(s, n, &i)) continue;
2177
+
2178
+ uint8_t cc = u(0);
2179
+
2180
+ if (cc == '<' || cc == '(' || cc == '[') {
2181
+ depth++;
2182
+ i++;
2183
+ continue;
2184
+ }
2185
+ if (cc == '>' || cc == ')' || cc == ']') {
2186
+ if (depth == 0) break;
2187
+ depth--;
2188
+ i++;
2189
+ continue;
2190
+ }
2191
+ if (cc == '{' && depth == 0) break;
2192
+
2193
+ i++;
2194
+ }
2195
+
2196
+ err = bare_type_stripper__add_range(ctx, ims, i);
2197
+ if (err < 0) return err;
2198
+
2199
+ i = bare_type_stripper__skip_trivia(s, n, i);
2200
+ continue;
2201
+ }
2202
+
2203
+ break;
2204
+ }
2205
+
2206
+ if (i < n && u(0) == '{') {
2207
+ err = bare_type_stripper__process_class_body(ctx, &i, depth);
2208
+ if (err < 0) return err;
2209
+ }
2210
+
2211
+ *result = i;
2212
+ return 0;
2213
+ }
2214
+
2215
+ // Keyword classification for the walker: Maps a keyword to the `prev` class
2216
+ // it leaves behind. Keywords with side effects (declaration forms, 'as' /
2217
+ // 'satisfies', 'import' / 'export', ...) are dispatched explicitly in the
2218
+ // walker before this table is consulted.
2219
+ typedef struct {
2220
+ const char *kw;
2221
+ uint8_t len;
2222
+ uint8_t prev;
2223
+ } bare_type_stripper__kw_t;
2224
+
2225
+ static const bare_type_stripper__kw_t bare_type_stripper__kws[] = {
2226
+ // Expression-starting keywords: The next token begins a new expression.
2227
+ {"in", 2, bare_type_stripper__prev_op},
2228
+ {"new", 3, bare_type_stripper__prev_op},
2229
+ {"void", 4, bare_type_stripper__prev_op},
2230
+ {"case", 4, bare_type_stripper__prev_op},
2231
+ {"throw", 5, bare_type_stripper__prev_op},
2232
+ {"await", 5, bare_type_stripper__prev_op},
2233
+ {"yield", 5, bare_type_stripper__prev_op},
2234
+ {"return", 6, bare_type_stripper__prev_op},
2235
+ {"typeof", 6, bare_type_stripper__prev_op},
2236
+ {"delete", 6, bare_type_stripper__prev_op},
2237
+ {"instanceof", 10, bare_type_stripper__prev_op},
2238
+
2239
+ // Literal-like keywords: End an expression.
2240
+ {"true", 4, bare_type_stripper__prev_expr},
2241
+ {"this", 4, bare_type_stripper__prev_expr},
2242
+ {"null", 4, bare_type_stripper__prev_expr},
2243
+ {"false", 5, bare_type_stripper__prev_expr},
2244
+ {"super", 5, bare_type_stripper__prev_expr},
2245
+ {"arguments", 9, bare_type_stripper__prev_expr},
2246
+ {"undefined", 9, bare_type_stripper__prev_expr},
2247
+
2248
+ // Statement keywords whose body follows directly: A '{' after these is a
2249
+ // block, not an object literal, and the next token sits at statement
2250
+ // position.
2251
+ {"do", 2, bare_type_stripper__prev_stmt},
2252
+ {"try", 3, bare_type_stripper__prev_stmt},
2253
+ {"else", 4, bare_type_stripper__prev_stmt},
2254
+ {"finally", 7, bare_type_stripper__prev_stmt},
2255
+
2256
+ // 'async' is remembered so a following '(' can still be recognized as
2257
+ // arrow parameters.
2258
+ {"async", 5, bare_type_stripper__prev_async},
2259
+ };
2260
+
2261
+ // Classify an identifier span against the keyword table. Returns the `prev`
2262
+ // class the keyword leaves behind, or prev_expr for plain identifiers.
2263
+ static inline int
2264
+ bare_type_stripper__classify_kw(const utf8_t *s, size_t ks, size_t kl) {
2265
+ // Every entry is 2-10 bytes long; gate on length and first byte before
2266
+ // paying for a memcmp, since plain identifiers vastly outnumber keywords.
2267
+ if (kl < 2 || kl > 10) return bare_type_stripper__prev_expr;
2268
+
2269
+ uint8_t c0 = s[ks];
2270
+
2271
+ for (size_t k = 0; k < sizeof(bare_type_stripper__kws) / sizeof(bare_type_stripper__kws[0]); k++) {
2272
+ const bare_type_stripper__kw_t *kw = &bare_type_stripper__kws[k];
2273
+
2274
+ if (kw->len == kl && (uint8_t) kw->kw[0] == c0 && memcmp(&s[ks], kw->kw, kl) == 0) {
2275
+ return kw->prev;
2276
+ }
2277
+ }
2278
+
2279
+ return bare_type_stripper__prev_expr;
2280
+ }
2281
+
2282
+ // Maximum walker recursion depth. The walker can recurse on '{' blocks and
2283
+ // (via the process_* helpers) on function/class bodies; capping the depth
2284
+ // keeps deeply nested input from blowing the C stack. 256 is generous in
2285
+ // practice - real code rarely nests more than a few dozen levels.
2286
+ #define BARE_TYPE_STRIPPER__MAX_DEPTH 256
2287
+
2288
+ // The master walker. Walks bytes from *result and either:
2289
+ // * If `stop_at` is non-zero, stops at the first occurrence of that byte at
2290
+ // depth 0 (relative to our local paren/bracket counters), without
2291
+ // consuming it. Two modes stop at more than one byte: ';' also stops at
2292
+ // ',' (variable declarators), and ',' also stops at ')' (default
2293
+ // parameter values).
2294
+ // * If `stop_at` is '}', expects to be called with i pointing AT the opening
2295
+ // '{' or after it - handled by the `body_is_stmt` flag: When true, this
2296
+ // walker recursively treats nested '{' blocks as statement contexts.
2297
+ //
2298
+ // `body_is_stmt` tells us whether the current block context behaves like a
2299
+ // statement region (so 'type', 'interface', etc. should be matched as
2300
+ // statement-leading keywords).
2301
+ static inline int
2302
+ 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) {
2303
+ const utf8_t *s = ctx->s;
2304
+ size_t n = ctx->n;
2305
+
2306
+ int err;
2307
+ size_t i = *result;
2308
+
2309
+ // Bail out if we've recursed too deeply. We jump to end-of-input so the
2310
+ // enclosing scan finishes cleanly rather than getting stuck in a partial
2311
+ // state; the (very deeply nested) tail won't be stripped, which is no
2312
+ // worse than the alternative of crashing.
2313
+ if (depth > BARE_TYPE_STRIPPER__MAX_DEPTH) {
2314
+ *result = n;
2315
+ return 0;
2316
+ }
2317
+
2318
+ // If we're entering a block scope, consume the opening '{'.
2319
+ if (stop_at == '}' && i < n && u(0) == '{') i++;
2320
+
2321
+ int paren_depth = 0;
2322
+ int bracket_depth = 0;
2323
+
2324
+ // Unclosed ternary '?' count at this walk level. Used to keep the arrow
2325
+ // return-type lookahead from claiming the ':' of an enclosing ternary
2326
+ // ('c ? (a) : b => c' is a ternary, not an annotated arrow).
2327
+ int ternary_depth = 0;
2328
+
2329
+ // Previous-token classification - see enum at top.
2330
+ int prev = body_is_stmt ? bare_type_stripper__prev_none : bare_type_stripper__prev_op;
2331
+ bool prev_dot = false;
2332
+
2333
+ while (i < n) {
2334
+ uint8_t ch = u(0);
2335
+
2336
+ // Whitespace.
2337
+ if (ws(ch)) {
2338
+ // ASI-aware stop: A newline at depth 0 right after an expression-
2339
+ // ending token is the cheapest lexical approximation of where ASI
2340
+ // would insert. Callers ask for this when scanning a class field
2341
+ // initializer so we don't chew into the next class member.
2342
+ if (stop_at_asi && lt(ch) && paren_depth == 0 && bracket_depth == 0 && bare_type_stripper__prev_is_expr(prev)) {
2343
+ *result = i;
2344
+ return 0;
2345
+ }
2346
+ // In statement contexts, treat a newline at depth 0 following an
2347
+ // expression-ending token as an ASI boundary - i.e. the next
2348
+ // identifier could be a statement-leading keyword ('type', 'declare',
2349
+ // 'interface', 'import', 'export'). Don't reset inside expression
2350
+ // contexts like object literals.
2351
+ if (body_is_stmt && lt(ch) && paren_depth == 0 && bracket_depth == 0 && bare_type_stripper__prev_is_expr(prev)) {
2352
+ prev = bare_type_stripper__prev_stmt;
2353
+ }
2354
+ i++;
2355
+ continue;
2356
+ }
2357
+
2358
+ // Comments.
2359
+ if (ch == '/' && i + 1 < n && u(1) == '/') {
2360
+ while (i < n && !lt(u(0))) i++;
2361
+ continue;
2362
+ }
2363
+ if (ch == '/' && i + 1 < n && u(1) == '*') {
2364
+ i += 2;
2365
+ while (i + 1 < n && !(u(0) == '*' && u(1) == '/')) i++;
2366
+ if (i + 1 < n) i += 2;
2367
+ continue;
2368
+ }
2369
+
2370
+ // Stop condition.
2371
+ if (stop_at != 0 && paren_depth == 0 && bracket_depth == 0) {
2372
+ if (stop_at == '}' && ch == '}') {
2373
+ i++;
2374
+ *result = i;
2375
+ return 0;
2376
+ }
2377
+ if (stop_at == ')' && ch == ')') {
2378
+ *result = i;
2379
+ return 0;
2380
+ }
2381
+ if (stop_at == ';' && (ch == ';' || ch == ',')) {
2382
+ *result = i;
2383
+ return 0;
2384
+ }
2385
+ if (stop_at == ',' && (ch == ',' || ch == ')' || ch == '}')) {
2386
+ *result = i;
2387
+ return 0;
2388
+ }
2389
+ }
2390
+
2391
+ // A token follows. Latch and clear the pending property-access marker -
2392
+ // only the '.' / '?.' branches re-arm it.
2393
+ bool was_dot = prev_dot;
2394
+ prev_dot = false;
2395
+
2396
+ // String, template, regex.
2397
+ if (ch == '\'' || ch == '"') {
2398
+ i = bare_type_stripper__skip_string(s, n, i);
2399
+ prev = bare_type_stripper__prev_expr;
2400
+ continue;
2401
+ }
2402
+ if (ch == '`') {
2403
+ err = bare_type_stripper__walk_template(ctx, &i, depth);
2404
+ if (err < 0) return err;
2405
+
2406
+ prev = bare_type_stripper__prev_expr;
2407
+ continue;
2408
+ }
2409
+ if (ch == '/') {
2410
+ if (!bare_type_stripper__prev_is_expr(prev)) {
2411
+ i = bare_type_stripper__skip_regex(s, n, i);
2412
+ prev = bare_type_stripper__prev_expr;
2413
+ continue;
2414
+ }
2415
+ i++;
2416
+ prev = bare_type_stripper__prev_op;
2417
+ continue;
2418
+ }
2419
+
2420
+ // Numeric literal.
2421
+ if (ch >= '0' && ch <= '9') {
2422
+ while (i < n && (id(u(0)) || u(0) == '.')) i++;
2423
+ prev = bare_type_stripper__prev_expr;
2424
+ continue;
2425
+ }
2426
+
2427
+ // Identifier or keyword.
2428
+ if (ids(ch)) {
2429
+ size_t ks = i;
2430
+ while (i < n && id(u(0))) i++;
2431
+ size_t ke = i;
2432
+ size_t kl = ke - ks;
2433
+
2434
+ // Property access: This identifier is just a property name.
2435
+ if (was_dot) {
2436
+ prev = bare_type_stripper__prev_expr;
2437
+ continue;
2438
+ }
2439
+
2440
+ bool at_stmt_start = (prev == bare_type_stripper__prev_stmt || prev == bare_type_stripper__prev_none);
2441
+
2442
+ // Statement-leading TS keywords. The three declaration forms ('type',
2443
+ // 'interface', 'declare') share an identical dispatch pattern: When
2444
+ // followed by an identifier, delegate to a process_* helper. 'abstract'
2445
+ // is a special case - it only erases the keyword and lets the normal
2446
+ // 'class' branch handle the rest.
2447
+ if (at_stmt_start) {
2448
+ int (*handler)(bare_type_stripper_t *, size_t *) = NULL;
2449
+
2450
+ if (bare_type_stripper__match_kw(s, ks, kl, "type", 4)) handler = bare_type_stripper__process_type_alias;
2451
+ else if (bare_type_stripper__match_kw(s, ks, kl, "interface", 9)) handler = bare_type_stripper__process_interface;
2452
+ else if (bare_type_stripper__match_kw(s, ks, kl, "declare", 7)) handler = bare_type_stripper__process_declare;
2453
+
2454
+ if (handler) {
2455
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2456
+ if (after < n && ids(s[after])) {
2457
+ size_t save = ks;
2458
+
2459
+ err = handler(ctx, &save);
2460
+ if (err < 0) return err;
2461
+
2462
+ i = save;
2463
+ prev = bare_type_stripper__prev_stmt;
2464
+ continue;
2465
+ }
2466
+ }
2467
+
2468
+ // 'abstract class' - strip the leading 'abstract' keyword. The 'class'
2469
+ // that follows is processed by the normal class branch below.
2470
+ if (bare_type_stripper__match_kw(s, ks, kl, "abstract", 8)) {
2471
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2472
+ if (bare_type_stripper__at_kw(s, n, after, "class", 5)) {
2473
+ err = bare_type_stripper__add_range(ctx, ks, ke);
2474
+ if (err < 0) return err;
2475
+
2476
+ i = ke;
2477
+ continue;
2478
+ }
2479
+ }
2480
+
2481
+ // 'enum E { ... }' - non-erasable: An enum declaration emits a
2482
+ // runtime object that stripping can't reproduce. Mark the whole
2483
+ // construct so strip() can throw while lex() consumers still see
2484
+ // its extent. ('declare enum' is ambient and already handled above.)
2485
+ if (bare_type_stripper__match_kw(s, ks, kl, "enum", 4)) {
2486
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2487
+ if (after < n && ids(s[after])) {
2488
+ size_t end = bare_type_stripper__scan_enum(s, n, ks);
2489
+
2490
+ err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
2491
+ if (err < 0) return err;
2492
+
2493
+ i = end;
2494
+ prev = bare_type_stripper__prev_stmt;
2495
+ continue;
2496
+ }
2497
+ }
2498
+
2499
+ // 'namespace N { ... }' / 'module N { ... }' - non-erasable for the
2500
+ // same reason. Requires a '{' body after the (possibly dotted) name
2501
+ // so identifiers that merely spell the keywords ('module.exports')
2502
+ // are left alone.
2503
+ if (bare_type_stripper__match_kw(s, ks, kl, "namespace", 9) || bare_type_stripper__match_kw(s, ks, kl, "module", 6)) {
2504
+ size_t p = bare_type_stripper__skip_trivia(s, n, ke);
2505
+ if (p < n && ids(s[p])) {
2506
+ while (p < n && (id(s[p]) || s[p] == '.')) p++;
2507
+ p = bare_type_stripper__skip_trivia(s, n, p);
2508
+
2509
+ if (p < n && s[p] == '{') {
2510
+ size_t end = bare_type_stripper__scan_balanced(s, n, p, '{', '}');
2511
+
2512
+ err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
2513
+ if (err < 0) return err;
2514
+
2515
+ i = end;
2516
+ prev = bare_type_stripper__prev_stmt;
2517
+ continue;
2518
+ }
2519
+ }
2520
+ }
2521
+ }
2522
+
2523
+ // 'catch (e: T)' - strip the TS type annotation inside the catch
2524
+ // parameter list. 'catch' is a reserved word so its only valid context
2525
+ // is the catch clause of a try-statement; we don't gate on prev. The
2526
+ // outer walker still processes 'catch (e) { ... }' as usual.
2527
+ if (kl == 5 && memcmp(&s[ks], "catch", 5) == 0) {
2528
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2529
+ if (after < n && s[after] == '(') {
2530
+ size_t p = bare_type_stripper__skip_trivia(s, n, after + 1);
2531
+ if (p < n && ids(s[p])) {
2532
+ while (p < n && id(s[p])) p++;
2533
+ size_t scan = bare_type_stripper__skip_trivia(s, n, p);
2534
+ if (scan < n && s[scan] == ':') {
2535
+ size_t ts = scan;
2536
+ size_t te = bare_type_stripper__scan_type(s, n, scan + 1, BARE_TYPE_STRIPPER__T_PAREN);
2537
+
2538
+ err = bare_type_stripper__add_range(ctx, ts, te);
2539
+ if (err < 0) return err;
2540
+ }
2541
+ }
2542
+ }
2543
+ }
2544
+
2545
+ // 'import' and 'export' - module-level statement keywords. Allow these
2546
+ // even when prev is something other than stmt_start, since they're
2547
+ // reserved words and our prev tracking can be imprecise at module level.
2548
+ if (kl == 6 && memcmp(&s[ks], "import", 6) == 0) {
2549
+ // Dynamic import or import.meta - leave as expression.
2550
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2551
+ if (after < n && (s[after] == '(' || s[after] == '.')) {
2552
+ i = ke;
2553
+ prev = bare_type_stripper__prev_expr;
2554
+ continue;
2555
+ }
2556
+
2557
+ if (at_stmt_start) {
2558
+ size_t save = ks;
2559
+
2560
+ err = bare_type_stripper__process_import(ctx, &save);
2561
+ if (err < 0) return err;
2562
+
2563
+ i = save;
2564
+ prev = bare_type_stripper__prev_stmt;
2565
+ continue;
2566
+ }
2567
+ }
2568
+
2569
+ if (kl == 6 && memcmp(&s[ks], "export", 6) == 0 && at_stmt_start) {
2570
+ size_t save = ks;
2571
+
2572
+ err = bare_type_stripper__process_export(ctx, &save, depth);
2573
+ if (err < 0) return err;
2574
+
2575
+ i = save;
2576
+ prev = bare_type_stripper__prev_stmt;
2577
+ continue;
2578
+ }
2579
+
2580
+ // 'function' / 'class' - declaration or expression.
2581
+ if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
2582
+ size_t save = ks;
2583
+
2584
+ err = bare_type_stripper__process_function(ctx, &save, ks, depth);
2585
+ if (err < 0) return err;
2586
+
2587
+ i = save;
2588
+ prev = at_stmt_start ? bare_type_stripper__prev_stmt : bare_type_stripper__prev_expr;
2589
+ continue;
2590
+ }
2591
+
2592
+ if (kl == 5 && memcmp(&s[ks], "class", 5) == 0) {
2593
+ size_t save = ks;
2594
+
2595
+ err = bare_type_stripper__process_class(ctx, &save, depth);
2596
+ if (err < 0) return err;
2597
+
2598
+ i = save;
2599
+ prev = at_stmt_start ? bare_type_stripper__prev_stmt : bare_type_stripper__prev_expr;
2600
+ continue;
2601
+ }
2602
+
2603
+ // Variable declarations - const/let/var. These appear at statement
2604
+ // starts and in for-loop headers ('for (const x = ...)'); we accept
2605
+ // any prev that isn't an expression-ending position.
2606
+ if (
2607
+ !bare_type_stripper__prev_is_expr(prev) &&
2608
+ ((kl == 3 && (memcmp(&s[ks], "let", 3) == 0 ||
2609
+ memcmp(&s[ks], "var", 3) == 0)) ||
2610
+ (kl == 5 && memcmp(&s[ks], "const", 5) == 0))
2611
+ ) {
2612
+ // 'const enum E { ... }' is an enum declaration, not a variable -
2613
+ // non-erasable like a plain 'enum'.
2614
+ if (kl == 5) {
2615
+ size_t after = bare_type_stripper__skip_trivia(s, n, ke);
2616
+ if (bare_type_stripper__at_kw(s, n, after, "enum", 4)) {
2617
+ size_t name = bare_type_stripper__skip_trivia(s, n, after + 4);
2618
+ if (name < n && ids(s[name])) {
2619
+ size_t end = bare_type_stripper__scan_enum(s, n, after);
2620
+
2621
+ err = bare_type_stripper__add_range_flags(ctx, ks, end, bare_type_stripper_error);
2622
+ if (err < 0) return err;
2623
+
2624
+ i = end;
2625
+ prev = bare_type_stripper__prev_stmt;
2626
+ continue;
2627
+ }
2628
+ }
2629
+ }
2630
+
2631
+ i = ke;
2632
+
2633
+ err = bare_type_stripper__process_var_decl(ctx, &i, depth);
2634
+ if (err < 0) return err;
2635
+
2636
+ prev = bare_type_stripper__prev_stmt;
2637
+ continue;
2638
+ }
2639
+
2640
+ // Postfix type assertion keywords 'as' and 'satisfies' - both consume a
2641
+ // following type expression with the same terminator set.
2642
+ 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))) {
2643
+ size_t te = bare_type_stripper__scan_type(
2644
+ s,
2645
+ n,
2646
+ ke,
2647
+ 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
2648
+ );
2649
+
2650
+ uint32_t flags = bare_type_stripper__asi_hazard_after(s, n, te) ? bare_type_stripper_semi : 0;
2651
+
2652
+ err = bare_type_stripper__add_range_flags(ctx, ks, te, flags);
2653
+ if (err < 0) return err;
2654
+
2655
+ i = te;
2656
+ prev = bare_type_stripper__prev_expr;
2657
+ continue;
2658
+ }
2659
+
2660
+ // Remaining keywords only affect token classification - see the
2661
+ // keyword table above the walker. Plain identifiers end an expression.
2662
+ prev = bare_type_stripper__classify_kw(s, ks, kl);
2663
+ continue;
2664
+ }
2665
+
2666
+ // Punctuation.
2667
+ switch (ch) {
2668
+ case '.': {
2669
+ // Spread / rest '...'
2670
+ if (i + 2 < n && u(1) == '.' && u(2) == '.') {
2671
+ i += 3;
2672
+ prev = bare_type_stripper__prev_op;
2673
+ continue;
2674
+ }
2675
+ // Otherwise '.' is a member operator; 'prev' is unchanged.
2676
+ i++;
2677
+ prev_dot = true;
2678
+ continue;
2679
+ }
2680
+
2681
+ case '?': {
2682
+ // Optional chaining '?.'
2683
+ if (i + 1 < n && u(1) == '.') {
2684
+ i += 2;
2685
+ prev_dot = true;
2686
+ continue;
2687
+ }
2688
+ // Nullish coalescing '??' (optionally '??=').
2689
+ if (i + 1 < n && u(1) == '?') {
2690
+ i += 2;
2691
+ if (i < n && u(0) == '=') i++;
2692
+ prev = bare_type_stripper__prev_op;
2693
+ continue;
2694
+ }
2695
+ // Ternary '?'.
2696
+ ternary_depth++;
2697
+ i++;
2698
+ prev = bare_type_stripper__prev_op;
2699
+ continue;
2700
+ }
2701
+
2702
+ case '(': {
2703
+ // Detect arrow function parameter lists: '(...)' immediately followed
2704
+ // by '=>' or by ': TYPE =>'. Without this we miss type annotations
2705
+ // inside arrow params. A '(' after an expression is a call and can't
2706
+ // start arrow parameters, so the lookahead is skipped entirely there -
2707
+ // except after the identifier 'async', which prefixes arrow parameters
2708
+ // while otherwise lexing like a plain expression.
2709
+ bool maybe_arrow = !bare_type_stripper__prev_is_expr(prev) || prev == bare_type_stripper__prev_async;
2710
+
2711
+ bool is_arrow = false;
2712
+ if (maybe_arrow) {
2713
+ size_t pend = bare_type_stripper__scan_group(ctx, i);
2714
+ size_t after = bare_type_stripper__skip_trivia(s, n, pend);
2715
+
2716
+ if (after + 1 < n && s[after] == '=' && s[after + 1] == '>') {
2717
+ is_arrow = true;
2718
+ } else if (after < n && s[after] == ':' && ternary_depth == 0) {
2719
+ // With an open ternary, a ':' after ')' is the ternary's alternate
2720
+ // separator ('c ? (a) : b => c'), not a return-type annotation.
2721
+ // Arrow return types can legitimately span newlines, so we don't
2722
+ // pass T_NL here - only the unambiguous terminators.
2723
+ size_t type_end = bare_type_stripper__scan_type(
2724
+ s,
2725
+ n,
2726
+ after + 1,
2727
+ BARE_TYPE_STRIPPER__T_ARROW | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI
2728
+ );
2729
+
2730
+ size_t after_type = bare_type_stripper__skip_trivia(s, n, type_end);
2731
+
2732
+ if (after_type + 1 < n && s[after_type] == '=' && s[after_type + 1] == '>') {
2733
+ is_arrow = true;
2734
+ }
2735
+ }
2736
+ }
2737
+
2738
+ if (is_arrow) {
2739
+ err = bare_type_stripper__process_params(ctx, &i, depth);
2740
+ if (err < 0) return err;
2741
+
2742
+ // The original ')' sits one byte before i (process_params consumed it).
2743
+ size_t orig_paren = i - 1;
2744
+
2745
+ size_t aft = bare_type_stripper__skip_trivia(s, n, i);
2746
+ if (aft < n && s[aft] == ':') {
2747
+ size_t ts = aft;
2748
+ i = aft + 1;
2749
+ i = bare_type_stripper__scan_type(
2750
+ s,
2751
+ n,
2752
+ i,
2753
+ BARE_TYPE_STRIPPER__T_ARROW | BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI
2754
+ );
2755
+
2756
+ // If anything between the params ')' and '=>' spans a newline -
2757
+ // whether inside the return-type itself OR in the gap between ')'
2758
+ // and ':' - JS would see a LineTerminator before '=>' after the
2759
+ // type is blanked, which is a syntax error. Move the ')' down:
2760
+ // Strip the original one and place a replacement ')' at the last
2761
+ // non-whitespace position before '=>'.
2762
+ bool multiline = false;
2763
+ for (size_t j = orig_paren + 1; j < i; j++) {
2764
+ if (lt(s[j])) {
2765
+ multiline = true;
2766
+ break;
2767
+ }
2768
+ }
2769
+
2770
+ if (multiline) {
2771
+ // Walk back past whitespace to find a byte to repurpose as ')'.
2772
+ size_t target = i;
2773
+ while (target > orig_paren + 1 && ws(s[target - 1])) target--;
2774
+
2775
+ // For the relocation to help, the target byte must live on the
2776
+ // same line as '=>' (no newlines between them) - otherwise we'd
2777
+ // just put ')' on a different orphaned line, still leaving a
2778
+ // line terminator before '=>'. When that's the case we fall
2779
+ // back to the plain strip; the output won't be a valid arrow
2780
+ // but it's no worse than the naive strip.
2781
+ bool same_line = true;
2782
+ for (size_t j = target; j < i; j++) {
2783
+ if (lt(s[j])) {
2784
+ same_line = false;
2785
+ break;
2786
+ }
2787
+ }
2788
+
2789
+ if (target > orig_paren + 1 && same_line) {
2790
+ err = bare_type_stripper__add_range(ctx, orig_paren, i);
2791
+ if (err < 0) return err;
2792
+
2793
+ err = bare_type_stripper__add_range_flags(ctx, target - 1, target, bare_type_stripper_paren);
2794
+ if (err < 0) return err;
2795
+ } else {
2796
+ err = bare_type_stripper__add_range(ctx, ts, i);
2797
+ if (err < 0) return err;
2798
+ }
2799
+ } else {
2800
+ err = bare_type_stripper__add_range(ctx, ts, i);
2801
+ if (err < 0) return err;
2802
+ }
2803
+ }
2804
+
2805
+ prev = bare_type_stripper__prev_op;
2806
+ continue;
2807
+ }
2808
+
2809
+ paren_depth++;
2810
+ i++;
2811
+ prev = bare_type_stripper__prev_op;
2812
+ continue;
2813
+ }
2814
+
2815
+ case ')': {
2816
+ if (paren_depth > 0) paren_depth--;
2817
+ i++;
2818
+ prev = bare_type_stripper__prev_paren;
2819
+ continue;
2820
+ }
2821
+
2822
+ case '[': {
2823
+ bracket_depth++;
2824
+ i++;
2825
+ prev = bare_type_stripper__prev_op;
2826
+ continue;
2827
+ }
2828
+
2829
+ case ']': {
2830
+ if (bracket_depth > 0) bracket_depth--;
2831
+ i++;
2832
+ prev = bare_type_stripper__prev_expr;
2833
+ continue;
2834
+ }
2835
+
2836
+ case '{': {
2837
+ // Recurse. Decide whether this '{' starts a block (statement context)
2838
+ // or an object literal (expression context). A '{' right after ')'
2839
+ // (if/for/while/switch headers, function parameter lists) or after
2840
+ // '=>' (arrow function body) is always a block in valid code - an
2841
+ // object literal can't directly follow either token.
2842
+ bool is_arrow_body = prev == bare_type_stripper__prev_arrow;
2843
+ bool is_block_ctx =
2844
+ is_arrow_body ||
2845
+ prev == bare_type_stripper__prev_paren ||
2846
+ prev == bare_type_stripper__prev_stmt ||
2847
+ prev == bare_type_stripper__prev_none;
2848
+
2849
+ size_t save = i;
2850
+
2851
+ if (is_block_ctx) {
2852
+ err = bare_type_stripper__walk(ctx, &save, '}', true, false, depth + 1);
2853
+ } else {
2854
+ // Object literal - structured processing so shorthand method
2855
+ // signatures get their annotations stripped.
2856
+ err = bare_type_stripper__process_object_literal(ctx, &save, depth + 1);
2857
+ }
2858
+ if (err < 0) return err;
2859
+
2860
+ i = save;
2861
+ // An arrow body ends the arrow expression; any other block ends a
2862
+ // statement.
2863
+ prev = is_block_ctx && !is_arrow_body
2864
+ ? bare_type_stripper__prev_stmt
2865
+ : bare_type_stripper__prev_expr;
2866
+ continue;
2867
+ }
2868
+
2869
+ case '}': {
2870
+ // Unmatched '}' at our depth - treat as end of containing scope.
2871
+ if (stop_at == '}' && paren_depth == 0 && bracket_depth == 0) {
2872
+ i++;
2873
+ *result = i;
2874
+ return 0;
2875
+ }
2876
+ i++;
2877
+ prev = bare_type_stripper__prev_stmt;
2878
+ continue;
2879
+ }
2880
+
2881
+ case ';': {
2882
+ ternary_depth = 0;
2883
+ i++;
2884
+ prev = bare_type_stripper__prev_stmt;
2885
+ continue;
2886
+ }
2887
+
2888
+ case ',': {
2889
+ i++;
2890
+ prev = bare_type_stripper__prev_op;
2891
+ continue;
2892
+ }
2893
+
2894
+ case '!': {
2895
+ // Postfix non-null assertion: Prev is an expression and not followed by '='.
2896
+ if (bare_type_stripper__prev_is_expr(prev) && (i + 1 >= n || u(1) != '=')) {
2897
+ err = bare_type_stripper__add_range(ctx, i, i + 1);
2898
+ if (err < 0) return err;
2899
+
2900
+ i++;
2901
+ continue;
2902
+ }
2903
+
2904
+ // '!' (logical not), '!=' or '!=='.
2905
+ i++;
2906
+ if (i < n && u(0) == '=') i++;
2907
+ if (i < n && u(0) == '=') i++;
2908
+ prev = bare_type_stripper__prev_op;
2909
+ continue;
2910
+ }
2911
+
2912
+ case '<': {
2913
+ // Generic at call site or instantiation: Prev is an expression and a
2914
+ // balanced '<...>' is followed by a token that can't continue a JS
2915
+ // comparison expression. '(' / '`' mean a generic call or tagged
2916
+ // template. ')' / ',' / ';' / ']' / '}' / end-of-input mean a TS-only
2917
+ // generic instantiation expression (TS 4.7+) - 'foo<T>' returning a
2918
+ // typed reference - which would otherwise mis-parse as chained
2919
+ // comparisons in stripped JS.
2920
+ if (bare_type_stripper__prev_is_expr(prev)) {
2921
+ size_t ge = bare_type_stripper__scan_generic(s, n, i);
2922
+ if (ge > i + 1) {
2923
+ size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
2924
+ bool is_generic = false;
2925
+ if (peek >= n) {
2926
+ is_generic = true;
2927
+ } else {
2928
+ uint8_t pc = s[peek];
2929
+ if (pc == '(' || pc == '`' || pc == ')' || pc == ',' || pc == ';' || pc == ']' || pc == '}') {
2930
+ is_generic = true;
2931
+ }
2932
+ }
2933
+ if (is_generic) {
2934
+ err = bare_type_stripper__add_range(ctx, i, ge);
2935
+ if (err < 0) return err;
2936
+
2937
+ i = ge;
2938
+ continue;
2939
+ }
2940
+ }
2941
+ } else {
2942
+ // '<' at the start of an expression isn't valid JS - it's TS-only
2943
+ // syntax: Generic arrow type parameters ('<T,>(x) => x') when a '('
2944
+ // follows the balanced '<...>', otherwise an old-style angle cast
2945
+ // ('<T>expr') or JSX, which we can't tell apart. Strip the former;
2946
+ // mark the latter as non-erasable.
2947
+ size_t ge = bare_type_stripper__scan_generic(s, n, i);
2948
+ if (ge > i + 1) {
2949
+ size_t peek = bare_type_stripper__skip_trivia(s, n, ge);
2950
+
2951
+ if (peek < n && s[peek] == '(') {
2952
+ err = bare_type_stripper__add_range(ctx, i, ge);
2953
+ if (err < 0) return err;
2954
+ } else {
2955
+ err = bare_type_stripper__add_range_flags(ctx, i, ge, bare_type_stripper_error);
2956
+ if (err < 0) return err;
2957
+ }
2958
+
2959
+ i = ge;
2960
+ prev = bare_type_stripper__prev_op;
2961
+ continue;
2962
+ }
2963
+ }
2964
+
2965
+ // '<', '<=', '<<' or '<<='.
2966
+ i++;
2967
+ if (i < n && u(0) == '=') i++;
2968
+ if (i < n && u(0) == '<') i++;
2969
+ prev = bare_type_stripper__prev_op;
2970
+ continue;
2971
+ }
2972
+
2973
+ case '=': {
2974
+ i++;
2975
+
2976
+ // Arrow '=>'.
2977
+ if (i < n && u(0) == '>') {
2978
+ i++;
2979
+ prev = bare_type_stripper__prev_arrow;
2980
+ continue;
2981
+ }
2982
+
2983
+ // '==', '==='.
2984
+ if (i < n && u(0) == '=') i++;
2985
+ if (i < n && u(0) == '=') i++;
2986
+ prev = bare_type_stripper__prev_op;
2987
+ continue;
2988
+ }
2989
+
2990
+ case ':': {
2991
+ if (ternary_depth > 0) ternary_depth--;
2992
+ i++;
2993
+ prev = bare_type_stripper__prev_op;
2994
+ continue;
2995
+ }
2996
+
2997
+ case '@': {
2998
+ // Decorator. The decorated thing (a class, class member or top-level
2999
+ // declaration) sits at a statement-leading position.
3000
+ err = bare_type_stripper__process_decorator(ctx, &i, depth);
3001
+ if (err < 0) return err;
3002
+
3003
+ prev = bare_type_stripper__prev_stmt;
3004
+ continue;
3005
+ }
3006
+
3007
+ default: {
3008
+ i++;
3009
+ prev = bare_type_stripper__prev_op;
3010
+ continue;
3011
+ }
3012
+ }
3013
+ }
3014
+
3015
+ *result = i;
3016
+ return 0;
3017
+ }
3018
+
3019
+ static inline int
3020
+ bare_type_stripper__lex(bare_type_stripper_t *ctx, const utf8_t *s, size_t n) {
3021
+ size_t i = 0;
3022
+
3023
+ ctx->s = s;
3024
+ ctx->n = n;
3025
+
3026
+ ctx->ranges = NULL;
3027
+ ctx->len = 0;
3028
+ ctx->cap = 0;
3029
+
3030
+ memset(ctx->paren_close, 0, sizeof(ctx->paren_close));
3031
+
3032
+ return bare_type_stripper__walk(ctx, &i, 0, true, false, 0);
3033
+ }
3034
+
3035
+ #undef u
3036
+ #undef lt
3037
+ #undef ws
3038
+ #undef ids
3039
+ #undef id
3040
+
3041
+ #endif // BARE_TYPE_STRIPPER_H