bare-type-stripper 0.1.0 → 0.1.2
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 +2 -0
- package/lib/strip.h +275 -22
- package/package.json +1 -1
- package/prebuilds/android-arm/bare-type-stripper.bare +0 -0
- package/prebuilds/android-arm/bare-type-stripper.node +0 -0
- package/prebuilds/android-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/android-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/android-ia32/bare-type-stripper.bare +0 -0
- package/prebuilds/android-ia32/bare-type-stripper.node +0 -0
- package/prebuilds/android-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/android-x64/bare-type-stripper.node +0 -0
- package/prebuilds/darwin-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/darwin-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/darwin-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/darwin-x64/bare-type-stripper.node +0 -0
- package/prebuilds/ios-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-type-stripper.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/linux-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/linux-x64/bare-type-stripper.node +0 -0
- package/prebuilds/win32-arm64/bare-type-stripper.bare +0 -0
- package/prebuilds/win32-arm64/bare-type-stripper.node +0 -0
- package/prebuilds/win32-x64/bare-type-stripper.bare +0 -0
- package/prebuilds/win32-x64/bare-type-stripper.node +0 -0
package/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
|
-
|
|
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 == '{') {
|
|
@@ -695,10 +707,12 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
|
|
|
695
707
|
// as 'op' keeps multi-line conditional types intact (e.g. 'T extends\n
|
|
696
708
|
// U ? A : B').
|
|
697
709
|
last_was_op =
|
|
710
|
+
(kl == 2 && memcmp(&s[ks], "is", 2) == 0) ||
|
|
698
711
|
(kl == 5 && (memcmp(&s[ks], "infer", 5) == 0 ||
|
|
699
712
|
memcmp(&s[ks], "keyof", 5) == 0)) ||
|
|
700
713
|
(kl == 6 && memcmp(&s[ks], "typeof", 6) == 0) ||
|
|
701
|
-
(kl == 7 && memcmp(&s[ks], "extends", 7) == 0
|
|
714
|
+
(kl == 7 && (memcmp(&s[ks], "extends", 7) == 0 ||
|
|
715
|
+
memcmp(&s[ks], "asserts", 7) == 0));
|
|
702
716
|
seen_token = true;
|
|
703
717
|
continue;
|
|
704
718
|
}
|
|
@@ -720,6 +734,11 @@ bare_type_stripper__scan_type(const utf8_t *s, size_t n, size_t i, uint32_t term
|
|
|
720
734
|
static inline int
|
|
721
735
|
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
736
|
|
|
737
|
+
// Forward declaration of the function processor - used by the export
|
|
738
|
+
// processor to delegate 'export [default] function ...' statements.
|
|
739
|
+
static inline int
|
|
740
|
+
bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, size_t stmt_start, uint32_t depth);
|
|
741
|
+
|
|
723
742
|
// Walk a template literal, recursively stripping type syntax inside each
|
|
724
743
|
// '${...}' expression. Assumes s[*result] == '`'. Leaves the cursor after
|
|
725
744
|
// the closing '`'. Unlike skip_template (which only advances the cursor),
|
|
@@ -1001,6 +1020,12 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1001
1020
|
continue;
|
|
1002
1021
|
}
|
|
1003
1022
|
|
|
1023
|
+
// Remember where this member starts and how many ranges were emitted
|
|
1024
|
+
// before it. A member that turns out to be a bodyless overload or
|
|
1025
|
+
// abstract signature is rolled back to this point and erased whole.
|
|
1026
|
+
size_t member_start = i;
|
|
1027
|
+
uint32_t member_mark = ctx->len;
|
|
1028
|
+
|
|
1004
1029
|
// 'static' is a real JS keyword that prefixes a class member, or starts
|
|
1005
1030
|
// a static initialization block. Consume it when followed by another
|
|
1006
1031
|
// identifier (or '['/'*') and remember that the line now has a syntactic
|
|
@@ -1056,8 +1081,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1056
1081
|
size_t peek = bare_type_stripper__skip_trivia(s, n, ke);
|
|
1057
1082
|
|
|
1058
1083
|
// Erase modifier only if followed by another identifier (member name
|
|
1059
|
-
// or another modifier), '[' (computed name),
|
|
1060
|
-
|
|
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'))) {
|
|
1061
1087
|
uint32_t flags = member_strip_seen ? 0 : bare_type_stripper_semi;
|
|
1062
1088
|
|
|
1063
1089
|
err = bare_type_stripper__add_range_flags(ctx, ks, peek, flags);
|
|
@@ -1110,7 +1136,19 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1110
1136
|
continue;
|
|
1111
1137
|
}
|
|
1112
1138
|
|
|
1113
|
-
// Otherwise it's a computed member name
|
|
1139
|
+
// Otherwise it's a computed member name - rewind and let the member
|
|
1140
|
+
// name parsing below consume it.
|
|
1141
|
+
i = bs;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
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
|
+
}
|
|
1114
1152
|
}
|
|
1115
1153
|
|
|
1116
1154
|
// Member name.
|
|
@@ -1124,6 +1162,9 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1124
1162
|
while (i < n && id(u(0))) i++;
|
|
1125
1163
|
} else if (u(0) == '\'' || u(0) == '"') {
|
|
1126
1164
|
i = bare_type_stripper__skip_string(s, n, i);
|
|
1165
|
+
} else if (u(0) == '[') {
|
|
1166
|
+
// Computed member name.
|
|
1167
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
|
|
1127
1168
|
} else if (u(0) >= '0' && u(0) <= '9') {
|
|
1128
1169
|
while (i < n && (id(u(0)) || u(0) == '.')) i++;
|
|
1129
1170
|
} else {
|
|
@@ -1198,10 +1239,22 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1198
1239
|
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1199
1240
|
}
|
|
1200
1241
|
|
|
1201
|
-
// Method body
|
|
1242
|
+
// Method body. Without one this is an overload or abstract method
|
|
1243
|
+
// signature, which has no runtime semantics: Drop the ranges emitted
|
|
1244
|
+
// for its pieces and erase the whole member as one range instead -
|
|
1245
|
+
// leaving a bodyless 'f()' behind would produce invalid runtime JS.
|
|
1246
|
+
// The injected ';' keeps ASI from folding a preceding field
|
|
1247
|
+
// initializer into whatever follows the erased member.
|
|
1202
1248
|
if (i < n && u(0) == '{') {
|
|
1203
1249
|
err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
|
|
1204
1250
|
if (err < 0) return err;
|
|
1251
|
+
} else {
|
|
1252
|
+
if (i < n && u(0) == ';') i++;
|
|
1253
|
+
|
|
1254
|
+
ctx->len = member_mark;
|
|
1255
|
+
|
|
1256
|
+
err = bare_type_stripper__add_range_flags(ctx, member_start, i, bare_type_stripper_semi);
|
|
1257
|
+
if (err < 0) return err;
|
|
1205
1258
|
}
|
|
1206
1259
|
|
|
1207
1260
|
continue;
|
|
@@ -1241,6 +1294,170 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
|
|
|
1241
1294
|
return 0;
|
|
1242
1295
|
}
|
|
1243
1296
|
|
|
1297
|
+
// True when the property starting at i parses as a method head - i.e.
|
|
1298
|
+
// '[async] [*] [get|set] KEY [<...>]' directly followed by '(' - meaning
|
|
1299
|
+
// the property is a shorthand method whose signature needs type stripping.
|
|
1300
|
+
// Pure lookahead: Consumes nothing and emits no ranges.
|
|
1301
|
+
static inline bool
|
|
1302
|
+
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
|
+
}
|
|
1317
|
+
|
|
1318
|
+
// Key.
|
|
1319
|
+
if (i < n && ids(s[i])) {
|
|
1320
|
+
while (i < n && id(s[i])) i++;
|
|
1321
|
+
} else if (i < n && (s[i] == '\'' || s[i] == '"')) {
|
|
1322
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1323
|
+
} else if (i < n && s[i] == '[') {
|
|
1324
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
|
|
1325
|
+
} else if (i < n && s[i] >= '0' && s[i] <= '9') {
|
|
1326
|
+
while (i < n && (id(s[i]) || s[i] == '.')) i++;
|
|
1327
|
+
} else {
|
|
1328
|
+
return false;
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1332
|
+
|
|
1333
|
+
// Method generics '<T>'.
|
|
1334
|
+
if (i < n && s[i] == '<') {
|
|
1335
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, i);
|
|
1336
|
+
if (ge <= i + 1) return false;
|
|
1337
|
+
i = bare_type_stripper__skip_trivia(s, n, ge);
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
return i < n && s[i] == '(';
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
// Process the body of an object literal. Expects s[i] == '{'. Walks until
|
|
1344
|
+
// the matching '}'. Properties are walked as plain expression code, except
|
|
1345
|
+
// shorthand methods, whose signatures (generics, parameter annotations,
|
|
1346
|
+
// return types) are stripped like class methods.
|
|
1347
|
+
static inline int
|
|
1348
|
+
bare_type_stripper__process_object_literal(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
1349
|
+
const utf8_t *s = ctx->s;
|
|
1350
|
+
size_t n = ctx->n;
|
|
1351
|
+
|
|
1352
|
+
int err;
|
|
1353
|
+
size_t i = *result;
|
|
1354
|
+
|
|
1355
|
+
if (u(0) != '{') return 0;
|
|
1356
|
+
i++;
|
|
1357
|
+
|
|
1358
|
+
while (i < n) {
|
|
1359
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1360
|
+
if (i >= n) break;
|
|
1361
|
+
|
|
1362
|
+
if (u(0) == '}') {
|
|
1363
|
+
i++;
|
|
1364
|
+
break;
|
|
1365
|
+
}
|
|
1366
|
+
if (u(0) == ',') {
|
|
1367
|
+
i++;
|
|
1368
|
+
continue;
|
|
1369
|
+
}
|
|
1370
|
+
if (u(0) == ')') {
|
|
1371
|
+
// Unmatched ')' - skip it so the loop can't wedge on malformed input.
|
|
1372
|
+
i++;
|
|
1373
|
+
continue;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
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
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// Key.
|
|
1399
|
+
if (i < n && ids(u(0))) {
|
|
1400
|
+
while (i < n && id(u(0))) i++;
|
|
1401
|
+
} else if (i < n && (u(0) == '\'' || u(0) == '"')) {
|
|
1402
|
+
i = bare_type_stripper__skip_string(s, n, i);
|
|
1403
|
+
} else if (i < n && u(0) == '[') {
|
|
1404
|
+
i = bare_type_stripper__scan_balanced(s, n, i, '[', ']');
|
|
1405
|
+
} else if (i < n && u(0) >= '0' && u(0) <= '9') {
|
|
1406
|
+
while (i < n && (id(u(0)) || u(0) == '.')) i++;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1410
|
+
|
|
1411
|
+
// Method generics '<T>'.
|
|
1412
|
+
err = bare_type_stripper__strip_generic(ctx, &i);
|
|
1413
|
+
if (err < 0) return err;
|
|
1414
|
+
|
|
1415
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1416
|
+
|
|
1417
|
+
// Parameter list.
|
|
1418
|
+
if (i < n && u(0) == '(') {
|
|
1419
|
+
err = bare_type_stripper__process_params(ctx, &i, depth);
|
|
1420
|
+
if (err < 0) return err;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1424
|
+
|
|
1425
|
+
// Return type annotation.
|
|
1426
|
+
if (i < n && u(0) == ':') {
|
|
1427
|
+
size_t ts = i;
|
|
1428
|
+
i++;
|
|
1429
|
+
i = bare_type_stripper__scan_type(
|
|
1430
|
+
s,
|
|
1431
|
+
n,
|
|
1432
|
+
i,
|
|
1433
|
+
BARE_TYPE_STRIPPER__T_BRACE | BARE_TYPE_STRIPPER__T_SEMI | BARE_TYPE_STRIPPER__T_NL
|
|
1434
|
+
);
|
|
1435
|
+
|
|
1436
|
+
err = bare_type_stripper__add_range(ctx, ts, i);
|
|
1437
|
+
if (err < 0) return err;
|
|
1438
|
+
|
|
1439
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// Method body.
|
|
1443
|
+
if (i < n && u(0) == '{') {
|
|
1444
|
+
err = bare_type_stripper__walk(ctx, &i, '}', true, false, depth + 1);
|
|
1445
|
+
if (err < 0) return err;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
continue;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
// Anything else - spread, shorthand, or 'key: value' - is plain
|
|
1452
|
+
// expression code up to the next depth-0 ',' or the closing '}'.
|
|
1453
|
+
err = bare_type_stripper__walk(ctx, &i, ',', false, false, depth + 1);
|
|
1454
|
+
if (err < 0) return err;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
*result = i;
|
|
1458
|
+
return 0;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1244
1461
|
// Scan past an 'enum IDENT { ... }' construct. Assumes s[i] points at the
|
|
1245
1462
|
// 'enum' keyword. Returns the index after the matching '}'. Used to mark
|
|
1246
1463
|
// the whole construct as a non-erasable range.
|
|
@@ -1594,7 +1811,7 @@ bare_type_stripper__process_import(bare_type_stripper_t *ctx, size_t *result) {
|
|
|
1594
1811
|
|
|
1595
1812
|
// Process 'export' at statement position.
|
|
1596
1813
|
static inline int
|
|
1597
|
-
bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result) {
|
|
1814
|
+
bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
1598
1815
|
const utf8_t *s = ctx->s;
|
|
1599
1816
|
size_t n = ctx->n;
|
|
1600
1817
|
|
|
@@ -1692,6 +1909,23 @@ bare_type_stripper__process_export(bare_type_stripper_t *ctx, size_t *result) {
|
|
|
1692
1909
|
return 0;
|
|
1693
1910
|
}
|
|
1694
1911
|
|
|
1912
|
+
// 'export [default] function ...' - delegate, threading through the
|
|
1913
|
+
// statement start so a bodyless overload signature erases the 'export'
|
|
1914
|
+
// prefix too instead of leaving a dangling 'export ;' behind.
|
|
1915
|
+
size_t p = i;
|
|
1916
|
+
|
|
1917
|
+
if (bare_type_stripper__at_kw(s, n, p, "default", 7)) {
|
|
1918
|
+
p = bare_type_stripper__skip_trivia(s, n, p + 7);
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
if (bare_type_stripper__at_kw(s, n, p, "function", 8)) {
|
|
1922
|
+
err = bare_type_stripper__process_function(ctx, &p, start, depth);
|
|
1923
|
+
if (err < 0) return err;
|
|
1924
|
+
|
|
1925
|
+
*result = p;
|
|
1926
|
+
return 0;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1695
1929
|
// 'export { ... }' - erase type-only specifiers entirely, including the
|
|
1696
1930
|
// comma that separates them from value specifiers.
|
|
1697
1931
|
if (u(0) == '{') {
|
|
@@ -1783,19 +2017,30 @@ bare_type_stripper__process_var_decl(bare_type_stripper_t *ctx, size_t *result,
|
|
|
1783
2017
|
return 0;
|
|
1784
2018
|
}
|
|
1785
2019
|
|
|
1786
|
-
// Process 'function ...'
|
|
1787
|
-
//
|
|
2020
|
+
// Process 'function ...'. On entry, *result points at 'function' and
|
|
2021
|
+
// stmt_start at the start of the enclosing statement - usually the same
|
|
2022
|
+
// position, but earlier when the declaration is prefixed by
|
|
2023
|
+
// 'export [default]'. A bodyless signature (an overload) is erased whole,
|
|
2024
|
+
// starting from stmt_start so no dangling 'export' survives.
|
|
1788
2025
|
static inline int
|
|
1789
|
-
bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
2026
|
+
bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result, size_t stmt_start, uint32_t depth) {
|
|
1790
2027
|
const utf8_t *s = ctx->s;
|
|
1791
2028
|
size_t n = ctx->n;
|
|
1792
2029
|
|
|
1793
2030
|
int err;
|
|
1794
|
-
|
|
2031
|
+
uint32_t mark = ctx->len;
|
|
1795
2032
|
size_t i = *result;
|
|
1796
2033
|
i += 8;
|
|
1797
2034
|
|
|
1798
2035
|
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
2036
|
+
|
|
2037
|
+
// 'function' used as a property key ('{ function: 1 }') rather than a
|
|
2038
|
+
// declaration or expression - leave it alone. A real function never has
|
|
2039
|
+
// ':' directly after the keyword.
|
|
2040
|
+
if (i < n && u(0) == ':') {
|
|
2041
|
+
*result += 8;
|
|
2042
|
+
return 0;
|
|
2043
|
+
}
|
|
1799
2044
|
if (i < n && u(0) == '*') {
|
|
1800
2045
|
i++;
|
|
1801
2046
|
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
@@ -1836,15 +2081,17 @@ bare_type_stripper__process_function(bare_type_stripper_t *ctx, size_t *result,
|
|
|
1836
2081
|
if (err < 0) return err;
|
|
1837
2082
|
} else {
|
|
1838
2083
|
// Overload signature: A function declaration without a body, terminated
|
|
1839
|
-
// by ';'.
|
|
1840
|
-
//
|
|
2084
|
+
// by ';' or a line break. Drop the ranges emitted for its pieces and
|
|
2085
|
+
// erase the whole signature as one range instead - leaving a bodyless
|
|
2086
|
+
// 'function f()' behind would produce invalid runtime JS. The injected
|
|
2087
|
+
// ';' keeps ASI from folding the signature line into its neighbors.
|
|
1841
2088
|
size_t peek = bare_type_stripper__skip_trivia(s, n, i);
|
|
1842
|
-
if (peek < n && s[peek] == ';')
|
|
1843
|
-
i = peek + 1;
|
|
2089
|
+
if (peek < n && s[peek] == ';') i = peek + 1;
|
|
1844
2090
|
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
2091
|
+
ctx->len = mark;
|
|
2092
|
+
|
|
2093
|
+
err = bare_type_stripper__add_range_flags(ctx, stmt_start, i, bare_type_stripper_semi);
|
|
2094
|
+
if (err < 0) return err;
|
|
1848
2095
|
}
|
|
1849
2096
|
|
|
1850
2097
|
*result = i;
|
|
@@ -2138,7 +2385,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
2138
2385
|
*result = i;
|
|
2139
2386
|
return 0;
|
|
2140
2387
|
}
|
|
2141
|
-
if (stop_at == ',' && (ch == ',' || ch == ')')) {
|
|
2388
|
+
if (stop_at == ',' && (ch == ',' || ch == ')' || ch == '}')) {
|
|
2142
2389
|
*result = i;
|
|
2143
2390
|
return 0;
|
|
2144
2391
|
}
|
|
@@ -2325,7 +2572,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
2325
2572
|
if (kl == 6 && memcmp(&s[ks], "export", 6) == 0 && at_stmt_start) {
|
|
2326
2573
|
size_t save = ks;
|
|
2327
2574
|
|
|
2328
|
-
err = bare_type_stripper__process_export(ctx, &save);
|
|
2575
|
+
err = bare_type_stripper__process_export(ctx, &save, depth);
|
|
2329
2576
|
if (err < 0) return err;
|
|
2330
2577
|
|
|
2331
2578
|
i = save;
|
|
@@ -2337,7 +2584,7 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
2337
2584
|
if (kl == 8 && memcmp(&s[ks], "function", 8) == 0) {
|
|
2338
2585
|
size_t save = ks;
|
|
2339
2586
|
|
|
2340
|
-
err = bare_type_stripper__process_function(ctx, &save, depth);
|
|
2587
|
+
err = bare_type_stripper__process_function(ctx, &save, ks, depth);
|
|
2341
2588
|
if (err < 0) return err;
|
|
2342
2589
|
|
|
2343
2590
|
i = save;
|
|
@@ -2604,7 +2851,13 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
2604
2851
|
|
|
2605
2852
|
size_t save = i;
|
|
2606
2853
|
|
|
2607
|
-
|
|
2854
|
+
if (is_block_ctx) {
|
|
2855
|
+
err = bare_type_stripper__walk(ctx, &save, '}', true, false, depth + 1);
|
|
2856
|
+
} else {
|
|
2857
|
+
// Object literal - structured processing so shorthand method
|
|
2858
|
+
// signatures get their annotations stripped.
|
|
2859
|
+
err = bare_type_stripper__process_object_literal(ctx, &save, depth + 1);
|
|
2860
|
+
}
|
|
2608
2861
|
if (err < 0) return err;
|
|
2609
2862
|
|
|
2610
2863
|
i = save;
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|