bare-type-stripper 0.1.1 → 0.1.3

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 // ')'
@@ -707,10 +752,12 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
707
752
  // as 'op' keeps multi-line conditional types intact (e.g. 'T extends\n
708
753
  // U ? A : B').
709
754
  last_was_op =
755
+ (kl == 2 && memcmp(&s[ks], "is", 2) == 0) ||
710
756
  (kl == 5 && (memcmp(&s[ks], "infer", 5) == 0 ||
711
757
  memcmp(&s[ks], "keyof", 5) == 0)) ||
712
758
  (kl == 6 && memcmp(&s[ks], "typeof", 6) == 0) ||
713
- (kl == 7 && memcmp(&s[ks], "extends", 7) == 0);
759
+ (kl == 7 && (memcmp(&s[ks], "extends", 7) == 0 ||
760
+ memcmp(&s[ks], "asserts", 7) == 0));
714
761
  seen_token = true;
715
762
  continue;
716
763
  }
@@ -1078,9 +1125,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1078
1125
 
1079
1126
  size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
1080
1127
 
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] == '*')) {
1128
+ // Erase modifier only if followed by another modifier or member name -
1129
+ // any member-key start, or a generator '*' introducing a method.
1130
+ if (bare_type_stripper__starts_member_key(s, n, peek, true) || (peek < n && s[peek] == '*')) {
1084
1131
  uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
1085
1132
 
1086
1133
  err = bare_type_stripper__add_range_flags(ctx, ks, peek, flags);
@@ -1138,21 +1185,16 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1138
1185
  i = bs;
1139
1186
  }
1140
1187
 
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
- }
1188
+ // Method-head prefix '[async] [*] [get|set]'. These tokens survive
1189
+ // stripping, but consume them here rather than letting one lex as the
1190
+ // member name, so the method's real name and signature are processed in
1191
+ // this same iteration - and so a bodyless overload erases from the prefix
1192
+ // onward instead of stranding it. When a bare 'async'/'get'/'set' is
1193
+ // itself the member name ('async() {}', 'get = 1') the prefix scan leaves
1194
+ // i untouched and it lexes as the name below.
1195
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, true);
1150
1196
 
1151
1197
  // Member name.
1152
- if (u(0) == '*') {
1153
- i++;
1154
- i = bare_type_stripper__skip_trivia(s, n, i);
1155
- }
1156
1198
  if (u(0) == '#') i++; // Private name
1157
1199
 
1158
1200
  if (i < n && ids(u(0))) {
@@ -1297,20 +1339,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1297
1339
  // Pure lookahead: Consumes nothing and emits no ranges.
1298
1340
  static inline bool
1299
1341
  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
- }
1342
+ // Method-head prefix '[async] [*] [get|set]' - object literals have no
1343
+ // private names, so '#' is not a key start here.
1344
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, false);
1314
1345
 
1315
1346
  // Key.
1316
1347
  if (i < n && ids(s[i])) {
@@ -1371,26 +1402,9 @@ bare_type_stripper__process_object_literal(bare_type_stripper_t *ctx, size_t *re
1371
1402
  }
1372
1403
 
1373
1404
  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
- }
1405
+ // Method-head prefix '[async] [*] [get|set]' - re-walked here to consume
1406
+ // it now that the lookahead has confirmed a method.
1407
+ i = bare_type_stripper__skip_method_head_prefix(s, n, i, false);
1394
1408
 
1395
1409
  // Key.
1396
1410
  if (i < n && ids(u(0))) {
@@ -1915,6 +1929,10 @@ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result, ui
1915
1929
  p = bare_type_stripper__skip_trivia(s, n, p + 7);
1916
1930
  }
1917
1931
 
1932
+ if (bare_type_stripper__at_kw(s, n, p, "async", 5)) {
1933
+ p = bare_type_stripper__skip_trivia(s, n, p + 5);
1934
+ }
1935
+
1918
1936
  if (bare_type_stripper__at_kw(s, n, p, "function", 8)) {
1919
1937
  err = bare_type_stripper__process_function(ctx, &p, start, depth);
1920
1938
  if (err < 0) return err;
@@ -2330,6 +2348,12 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2330
2348
  int prev = body_is_stmt ? bare_type_stripper__prev_none : bare_type_stripper__prev_op;
2331
2349
  bool prev_dot = false;
2332
2350
 
2351
+ // Start offset of the most recent 'async' keyword. Valid only while
2352
+ // 'prev == prev_async', i.e. when 'async' is the immediately preceding
2353
+ // token. Used so a bodyless 'async function' overload erases from 'async'
2354
+ // onward instead of leaving a dangling 'async ;' behind.
2355
+ size_t async_start = 0;
2356
+
2333
2357
  while (i < n) {
2334
2358
  uint8_t ch = u(0);
2335
2359
 
@@ -2581,7 +2605,11 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2581
2605
  if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
2582
2606
  size_t save = ks;
2583
2607
 
2584
- err = bare_type_stripper__process_function(ctx, &save, ks, depth);
2608
+ // A preceding 'async' is part of the declaration - erase a bodyless
2609
+ // overload from there so the 'async' keyword doesn't survive.
2610
+ size_t fn_start = prev == bare_type_stripper__prev_async ? async_start : ks;
2611
+
2612
+ err = bare_type_stripper__process_function(ctx, &save, fn_start, depth);
2585
2613
  if (err < 0) return err;
2586
2614
 
2587
2615
  i = save;
@@ -2660,6 +2688,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2660
2688
  // Remaining keywords only affect token classification - see the
2661
2689
  // keyword table above the walker. Plain identifiers end an expression.
2662
2690
  prev = bare_type_stripper__classify_kw(s, ks, kl);
2691
+ if (prev == bare_type_stripper__prev_async) async_start = ks;
2663
2692
  continue;
2664
2693
  }
2665
2694
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-type-stripper",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",