bare-type-stripper 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/strip.h CHANGED
@@ -176,6 +176,51 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
176
176
  static inline size_t
177
177
  bare_type_stripper__scan_declare(const utf8_t *s, size_t n, size_t i);
178
178
 
179
+ // True when a class/object member key can begin at s[p]: an identifier, a
180
+ // computed '[...]' name, a string or numeric literal, or - only in class
181
+ // bodies ('allow_private') - a '#' private name. Used to tell a keyword
182
+ // prefix ('async', 'get', 'set') or modifier apart from a property that
183
+ // merely happens to be named that word. Note a generator '*' is not a key
184
+ // start; callers that accept one ('async *m') test for it separately.
185
+ static inline bool
186
+ bare_type_stripper__starts_member_key(const utf8_t *s, size_t n, size_t p, bool allow_private) {
187
+ if (p >= n) return false;
188
+ return ids(s[p]) || s[p] == '[' || s[p] == '\'' || s[p] == '"' ||
189
+ (s[p] >= '0' && s[p] <= '9') || (allow_private && s[p] == '#');
190
+ }
191
+
192
+ // Consume the pure-JS method-head prefix '[async] [*] [get|set]' starting at
193
+ // i, advancing past each keyword or marker that is followed by a further
194
+ // prefix or a member key. Emits no ranges - these tokens survive stripping.
195
+ // Returns the index at the member key (or i unchanged when none applies).
196
+ // 'allow_private' lets a '#' private name count as a key start, which is
197
+ // valid in class bodies but not object literals.
198
+ static inline size_t
199
+ bare_type_stripper__skip_method_head_prefix(const utf8_t *s, size_t n, size_t i, bool allow_private) {
200
+ // 'async' prefix - only when followed by a generator marker or a key, so a
201
+ // property named 'async' ('async() {}', 'async: 1') lexes as its own key.
202
+ if (bare_type_stripper__at_kw(s, n, i, "async", 5)) {
203
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 5);
204
+ if (peek < n && (s[peek] == '*' || bare_type_stripper__starts_member_key(s, n, peek, allow_private))) {
205
+ i = peek;
206
+ }
207
+ }
208
+
209
+ // Generator marker '*'.
210
+ if (i < n && s[i] == '*') i = bare_type_stripper__skip_trivia(s, n, i + 1);
211
+
212
+ // 'get' / 'set' accessor prefix - only when followed by a key, so a method
213
+ // or property named 'get'/'set' lexes as its own key.
214
+ if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
215
+ size_t peek = bare_type_stripper__skip_trivia(s, n, i + 3);
216
+ if (bare_type_stripper__starts_member_key(s, n, peek, allow_private)) {
217
+ i = peek;
218
+ }
219
+ }
220
+
221
+ return i;
222
+ }
223
+
179
224
  #define BARE_TYPE_STRIPPER__T_COMMA 0x01
180
225
  #define BARE_TYPE_STRIPPER__T_SEMI 0x02
181
226
  #define BARE_TYPE_STRIPPER__T_PAREN 0x04 // ')'
@@ -705,9 +750,13 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
705
750
 
706
751
  // Type-level keywords that expect another type to follow. Marking these
707
752
  // as 'op' keeps multi-line conditional types intact (e.g. 'T extends\n
708
- // U ? A : B').
753
+ // U ? A : B'). 'as' covers both a chained cast ('x as unknown as {...}')
754
+ // and a mapped-type key remap ('{ [K in T as U]: V }'), where the type
755
+ // that follows may be an object literal whose '{' must be scanned as an
756
+ // operand rather than mistaken for the caller's terminating brace.
709
757
  last_was_op =
710
- (kl == 2 && memcmp(&s[ks], "is", 2) == 0) ||
758
+ (kl == 2 && (memcmp(&s[ks], "is", 2) == 0 ||
759
+ memcmp(&s[ks], "as", 2) == 0)) ||
711
760
  (kl == 5 && (memcmp(&s[ks], "infer", 5) == 0 ||
712
761
  memcmp(&s[ks], "keyof", 5) == 0)) ||
713
762
  (kl == 6 && memcmp(&s[ks], "typeof", 6) == 0) ||
@@ -1080,10 +1129,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1080
1129
 
1081
1130
  size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
1082
1131
 
1083
- // Erase modifier only if followed by another identifier (member name
1084
- // or another modifier), '#' (private name), '[' (computed name), '*'
1085
- // (generator), or a string or numeric member name.
1086
- if (peek < n && (ids(s[peek]) || s[peek] == '#' || s[peek] == '[' || s[peek] == '*' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1132
+ // Erase modifier only if followed by another modifier or member name -
1133
+ // any member-key start, or a generator '*' introducing a method.
1134
+ if (bare_type_stripper__starts_member_key(s, n, peek, true) || (peek < n && s[peek] == '*')) {
1087
1135
  uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
1088
1136
 
1089
1137
  err = bare_type_stripper__add_range_flags(ctx, ks, peek, flags);
@@ -1141,21 +1189,16 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1141
1189
  i = bs;
1142
1190
  }
1143
1191
 
1144
- // 'get' / 'set' accessor prefix. Keep it, but consume it here rather
1145
- // than letting it lex as the member name, so that the member's real
1146
- // name and signature are processed in this same iteration.
1147
- if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1148
- size_t peek = bare_type_stripper__skip_trivia(s, n, i + 3);
1149
- if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '#' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1150
- i = peek;
1151
- }
1152
- }
1192
+ // Method-head prefix '[async] [*] [get|set]'. These tokens survive
1193
+ // stripping, but consume them here rather than letting one lex as the
1194
+ // member name, so the method's real name and signature are processed in
1195
+ // this same iteration - and so a bodyless overload erases from the prefix
1196
+ // onward instead of stranding it. When a bare 'async'/'get'/'set' is
1197
+ // itself the member name ('async() {}', 'get = 1') the prefix scan leaves
1198
+ // i untouched and it lexes as the name below.
1199
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, true);
1153
1200
 
1154
1201
  // Member name.
1155
- if (u(0) == '*') {
1156
- i++;
1157
- i = bare_type_stripper__skip_trivia(s, n, i);
1158
- }
1159
1202
  if (u(0) == '#') i++; // Private name
1160
1203
 
1161
1204
  if (i < n && ids(u(0))) {
@@ -1300,20 +1343,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1300
1343
  // Pure lookahead: Consumes nothing and emits no ranges.
1301
1344
  static inline bool
1302
1345
  bare_type_stripper__at_method_head(const utf8_t *s, size_t n, size_t i) {
1303
- // 'async' prefix.
1304
- if (bare_type_stripper__at_kw(s, n, i, "async", 5)) {
1305
- size_t p = bare_type_stripper__skip_trivia(s, n, i + 5);
1306
- if (p < n && (ids(s[p]) || s[p] == '*' || s[p] == '[' || s[p] == '\'' || s[p] == '"' || (s[p] >= '0' && s[p] <= '9'))) i = p;
1307
- }
1308
-
1309
- // Generator marker.
1310
- if (i < n && s[i] == '*') i = bare_type_stripper__skip_trivia(s, n, i + 1);
1311
-
1312
- // 'get' / 'set' accessor prefix.
1313
- if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1314
- size_t p = bare_type_stripper__skip_trivia(s, n, i + 3);
1315
- if (p < n && (ids(s[p]) || s[p] == '[' || s[p] == '\'' || s[p] == '"' || (s[p] >= '0' && s[p] <= '9'))) i = p;
1316
- }
1346
+ // Method-head prefix '[async] [*] [get|set]' - object literals have no
1347
+ // private names, so '#' is not a key start here.
1348
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, false);
1317
1349
 
1318
1350
  // Key.
1319
1351
  if (i < n && ids(s[i])) {
@@ -1374,26 +1406,9 @@ bare_type_stripper__process_object_literal(bare_type_stripper_t *ctx, size_t *re
1374
1406
  }
1375
1407
 
1376
1408
  if (bare_type_stripper__at_method_head(s, n, i)) {
1377
- // 'async' prefix.
1378
- if (bare_type_stripper__at_kw(s, n, i, "async", 5)) {
1379
- size_t peek = bare_type_stripper__skip_trivia(s, n, i + 5);
1380
- if (peek < n && (ids(s[peek]) || s[peek] == '*' || s[peek] == '[' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1381
- i = peek;
1382
- }
1383
- }
1384
-
1385
- // Generator marker.
1386
- if (u(0) == '*') {
1387
- i = bare_type_stripper__skip_trivia(s, n, i + 1);
1388
- }
1389
-
1390
- // 'get' / 'set' accessor prefix.
1391
- if (bare_type_stripper__at_kw(s, n, i, "get", 3) || bare_type_stripper__at_kw(s, n, i, "set", 3)) {
1392
- size_t peek = bare_type_stripper__skip_trivia(s, n, i + 3);
1393
- if (peek < n && (ids(s[peek]) || s[peek] == '[' || s[peek] == '\'' || s[peek] == '"' || (s[peek] >= '0' && s[peek] <= '9'))) {
1394
- i = peek;
1395
- }
1396
- }
1409
+ // Method-head prefix '[async] [*] [get|set]' - re-walked here to consume
1410
+ // it now that the lookahead has confirmed a method.
1411
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, false);
1397
1412
 
1398
1413
  // Key.
1399
1414
  if (i < n && ids(u(0))) {
@@ -1918,6 +1933,10 @@ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result, ui
1918
1933
  p = bare_type_stripper__skip_trivia(s, n, p + 7);
1919
1934
  }
1920
1935
 
1936
+ if (bare_type_stripper__at_kw(s, n, p, "async", 5)) {
1937
+ p = bare_type_stripper__skip_trivia(s, n, p + 5);
1938
+ }
1939
+
1921
1940
  if (bare_type_stripper__at_kw(s, n, p, "function", 8)) {
1922
1941
  err = bare_type_stripper__process_function(ctx, &p, start, depth);
1923
1942
  if (err < 0) return err;
@@ -2333,6 +2352,12 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2333
2352
  int prev = body_is_stmt ? bare_type_stripper__prev_none : bare_type_stripper__prev_op;
2334
2353
  bool prev_dot = false;
2335
2354
 
2355
+ // Start offset of the most recent 'async' keyword. Valid only while
2356
+ // 'prev == prev_async', i.e. when 'async' is the immediately preceding
2357
+ // token. Used so a bodyless 'async function' overload erases from 'async'
2358
+ // onward instead of leaving a dangling 'async ;' behind.
2359
+ size_t async_start = 0;
2360
+
2336
2361
  while (i < n) {
2337
2362
  uint8_t ch = u(0);
2338
2363
 
@@ -2584,7 +2609,11 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2584
2609
  if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
2585
2610
  size_t save = ks;
2586
2611
 
2587
- err = bare_type_stripper__process_function(ctx, &save, ks, depth);
2612
+ // A preceding 'async' is part of the declaration - erase a bodyless
2613
+ // overload from there so the 'async' keyword doesn't survive.
2614
+ size_t fn_start = prev == bare_type_stripper__prev_async ? async_start : ks;
2615
+
2616
+ err = bare_type_stripper__process_function(ctx, &save, fn_start, depth);
2588
2617
  if (err < 0) return err;
2589
2618
 
2590
2619
  i = save;
@@ -2663,6 +2692,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2663
2692
  // Remaining keywords only affect token classification - see the
2664
2693
  // keyword table above the walker. Plain identifiers end an expression.
2665
2694
  prev = bare_type_stripper__classify_kw(s, ks, kl);
2695
+ if (prev == bare_type_stripper__prev_async) async_start = ks;
2666
2696
  continue;
2667
2697
  }
2668
2698
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-type-stripper",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",