bare-type-stripper 0.1.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.
package/README.md CHANGED
@@ -65,6 +65,8 @@ strip.lex('const x: number = 1')
65
65
  | Class member modifiers | `public`, `private`, `readonly`, etc. |
66
66
  | `implements` clauses | `class C implements I` |
67
67
  | `declare` statements | `declare const x: number` |
68
+ | Overload signatures | `function f(x: string): void` |
69
+ | Abstract members | `abstract foo(): void` |
68
70
 
69
71
  ## What is left alone
70
72
 
package/lib/strip.h CHANGED
@@ -607,6 +607,15 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
607
607
  continue;
608
608
  }
609
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
+
610
619
  if (depth == 0) {
611
620
  if ((terms & BARE_TYPE_STRIPPER__T_COMMA) && ch == ',') return i;
612
621
  if ((terms & BARE_TYPE_STRIPPER__T_SEMI) && ch == ';') return i;
@@ -642,7 +651,10 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
642
651
  continue;
643
652
  }
644
653
 
645
- if ((terms & BARE_TYPE_STRIPPER__T_BRACE) && ch == '{' && seen_token) return i;
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;
646
658
  }
647
659
 
648
660
  if (ch == '(' || ch == '[' || ch == '{') {
@@ -720,6 +732,11 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
720
732
  static inline int
721
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);
722
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
+
723
740
  // Walk a template literal, recursively stripping type syntax inside each
724
741
  // '${...}' expression. Assumes s[*result] == '`'. Leaves the cursor after
725
742
  // the closing '`'. Unlike skip_template (which only advances the cursor),
@@ -1001,6 +1018,12 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1001
1018
  continue;
1002
1019
  }
1003
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
+
1004
1027
  // 'static' is a real JS keyword that prefixes a class member, or starts
1005
1028
  // a static initialization block. Consume it when followed by another
1006
1029
  // identifier (or '['/'*') and remember that the line now has a syntactic
@@ -1110,7 +1133,19 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1110
1133
  continue;
1111
1134
  }
1112
1135
 
1113
- // Otherwise it's a computed member name; fall through.
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
+ }
1114
1149
  }
1115
1150
 
1116
1151
  // Member name.
@@ -1124,6 +1159,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1124
1159
  while (i < n && id(u(0))) i++;
1125
1160
  } else if (u(0) == '\'' || u(0) == '"') {
1126
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, '[', ']');
1127
1165
  } else if (u(0) >= '0' && u(0) <= '9') {
1128
1166
  while (i < n && (id(u(0)) || u(0) == '.')) i++;
1129
1167
  } else {
@@ -1198,10 +1236,22 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1198
1236
  i = bare_type_stripper__skip_trivia(s, n, i);
1199
1237
  }
1200
1238
 
1201
- // Method body (or just ';' for overload).
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.
1202
1245
  if (i < n && u(0) == '{') {
1203
1246
  err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
1204
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;
1205
1255
  }
1206
1256
 
1207
1257
  continue;
@@ -1241,6 +1291,170 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1241
1291
  return 0;
1242
1292
  }
1243
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
+
1244
1458
  // Scan past an 'enum IDENT { ... }' construct. Assumes s[i] points at the
1245
1459
  // 'enum' keyword. Returns the index after the matching '}'. Used to mark
1246
1460
  // the whole construct as a non-erasable range.
@@ -1594,7 +1808,7 @@ bare_type_stripper__process_import(bare_type_stripper_t *ctx, size_t *result) {
1594
1808
 
1595
1809
  // Process 'export' at statement position.
1596
1810
  static inline int
1597
- bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result) {
1811
+ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
1598
1812
  const utf8_t *s = ctx->s;
1599
1813
  size_t n = ctx->n;
1600
1814
 
@@ -1692,6 +1906,23 @@ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result) {
1692
1906
  return 0;
1693
1907
  }
1694
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
+
1695
1926
  // 'export { ... }' - erase type-only specifiers entirely, including the
1696
1927
  // comma that separates them from value specifiers.
1697
1928
  if (u(0) == '{') {
@@ -1783,19 +2014,30 @@ bare_type_stripper__process_var_decl(bare_type_stripper_t *ctx, size_t *result,
1783
2014
  return 0;
1784
2015
  }
1785
2016
 
1786
- // Process 'function ...' or arrow function whose params we're entering.
1787
- // On entry, i points at 'function'.
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.
1788
2022
  static inline int
1789
- bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
2023
+ bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, size_t stmt_start, uint32_t depth) {
1790
2024
  const utf8_t *s = ctx->s;
1791
2025
  size_t n = ctx->n;
1792
2026
 
1793
2027
  int err;
1794
- size_t start = *result;
2028
+ uint32_t mark = ctx->len;
1795
2029
  size_t i = *result;
1796
2030
  i += 8;
1797
2031
 
1798
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
+ }
1799
2041
  if (i < n && u(0) == '*') {
1800
2042
  i++;
1801
2043
  i = bare_type_stripper__skip_trivia(s, n, i);
@@ -1836,15 +2078,17 @@ bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result,
1836
2078
  if (err < 0) return err;
1837
2079
  } else {
1838
2080
  // Overload signature: A function declaration without a body, terminated
1839
- // by ';'. Strip the whole signature line - leaving 'function f();' would
1840
- // produce invalid runtime JS.
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.
1841
2085
  size_t peek = bare_type_stripper__skip_trivia(s, n, i);
1842
- if (peek < n && s[peek] == ';') {
1843
- i = peek + 1;
2086
+ if (peek < n && s[peek] == ';') i = peek + 1;
1844
2087
 
1845
- err = bare_type_stripper__add_range_flags(ctx, start, i, bare_type_stripper_semi);
1846
- if (err < 0) return err;
1847
- }
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;
1848
2092
  }
1849
2093
 
1850
2094
  *result = i;
@@ -2138,7 +2382,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2138
2382
  *result = i;
2139
2383
  return 0;
2140
2384
  }
2141
- if (stop_at == ',' && (ch == ',' || ch == ')')) {
2385
+ if (stop_at == ',' && (ch == ',' || ch == ')' || ch == '}')) {
2142
2386
  *result = i;
2143
2387
  return 0;
2144
2388
  }
@@ -2325,7 +2569,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2325
2569
  if (kl == 6 && memcmp(&s[ks], "export", 6) == 0 && at_stmt_start) {
2326
2570
  size_t save = ks;
2327
2571
 
2328
- err = bare_type_stripper__process_export(ctx, &save);
2572
+ err = bare_type_stripper__process_export(ctx, &save, depth);
2329
2573
  if (err < 0) return err;
2330
2574
 
2331
2575
  i = save;
@@ -2337,7 +2581,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2337
2581
  if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
2338
2582
  size_t save = ks;
2339
2583
 
2340
- err = bare_type_stripper__process_function(ctx, &save, depth);
2584
+ err = bare_type_stripper__process_function(ctx, &save, ks, depth);
2341
2585
  if (err < 0) return err;
2342
2586
 
2343
2587
  i = save;
@@ -2604,7 +2848,13 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
2604
2848
 
2605
2849
  size_t save = i;
2606
2850
 
2607
- err = bare_type_stripper__walk(ctx, &save, '}', is_block_ctx, false, depth + 1);
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
+ }
2608
2858
  if (err < 0) return err;
2609
2859
 
2610
2860
  i = save;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-type-stripper",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",