bare-type-stripper 0.1.7 → 0.1.8
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/CMakeLists.txt +5 -12
- package/README.md +2 -2
- package/binding.c +14 -3
- package/index.d.ts +20 -5
- package/index.js +4 -4
- package/lib/strip.h +143 -1
- package/package.json +3 -4
- 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/CMakeLists.txt
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
cmake_minimum_required(VERSION 4.0)
|
|
2
2
|
|
|
3
3
|
find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
|
|
4
|
+
find_package(cmake-fetch REQUIRED PATHS node_modules/cmake-fetch)
|
|
4
5
|
find_package(cmake-napi REQUIRED PATHS node_modules/cmake-napi)
|
|
5
|
-
find_package(cmake-npm REQUIRED PATHS node_modules/cmake-npm)
|
|
6
6
|
|
|
7
7
|
project(bare_type_stripper C)
|
|
8
8
|
|
|
9
|
+
fetch_package("github:holepunchto/libnapish#8cbfe78")
|
|
10
|
+
|
|
9
11
|
add_bare_module(bare_type_stripper)
|
|
10
12
|
|
|
11
13
|
target_sources(
|
|
@@ -22,17 +24,8 @@ target_sources(
|
|
|
22
24
|
binding.c
|
|
23
25
|
)
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
${bare_type_stripper_napi}
|
|
27
|
-
PRIVATE
|
|
28
|
-
NAPI_VERSION=9
|
|
29
|
-
NAPI_EXPERIMENTAL
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
resolve_node_module(bare-compat-napi compat)
|
|
33
|
-
|
|
34
|
-
target_include_directories(
|
|
27
|
+
target_link_libraries(
|
|
35
28
|
${bare_type_stripper_napi}
|
|
36
29
|
PRIVATE
|
|
37
|
-
|
|
30
|
+
napish::static
|
|
38
31
|
)
|
package/README.md
CHANGED
|
@@ -54,11 +54,11 @@ Constructs with runtime semantics that a purely lexical stripper cannot reproduc
|
|
|
54
54
|
- `enum` / `const enum` declarations - they emit a runtime object.
|
|
55
55
|
- `namespace` / `module` declarations with bodies - they emit runtime code.
|
|
56
56
|
- Parameter properties - `constructor(public x: number)` implies a `this.x = x` assignment that stripping the modifier would silently lose.
|
|
57
|
-
- Old-style angle-bracket type assertions (`<Foo>expr`) - indistinguishable from JSX
|
|
57
|
+
- Old-style angle-bracket type assertions (`<Foo>expr`) - indistinguishable from JSX. With `{ jsx: true }`, `<` where an expression starts is read as JSX instead, as TypeScript reads it in `.tsx` files, unless it opens the type parameters of a generic arrow function, written `<T,>` or `<T extends U>`.
|
|
58
58
|
|
|
59
59
|
## Limitations
|
|
60
60
|
|
|
61
|
-
The stripper targets
|
|
61
|
+
The stripper targets `.ts` sources, and `.tsx` sources with `{ jsx: true }`. JSX is left as it is, for a later step to lower, and only the types in its expression containers and the type arguments of its tags are stripped.
|
|
62
62
|
|
|
63
63
|
## Threat model
|
|
64
64
|
|
package/binding.c
CHANGED
|
@@ -29,8 +29,8 @@ static js_value_t *
|
|
|
29
29
|
bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
|
|
30
30
|
int err;
|
|
31
31
|
|
|
32
|
-
size_t argc =
|
|
33
|
-
js_value_t *argv[
|
|
32
|
+
size_t argc = 4;
|
|
33
|
+
js_value_t *argv[4];
|
|
34
34
|
|
|
35
35
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
36
36
|
assert(err == 0);
|
|
@@ -112,8 +112,19 @@ bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
|
|
|
112
112
|
input = copy;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
bool jsx = false;
|
|
116
|
+
|
|
117
|
+
bool is_boolean;
|
|
118
|
+
err = js_is_boolean(env, argv[3], &is_boolean);
|
|
119
|
+
assert(err == 0);
|
|
120
|
+
|
|
121
|
+
if (is_boolean) {
|
|
122
|
+
err = js_get_value_bool(env, argv[3], &jsx);
|
|
123
|
+
assert(err == 0);
|
|
124
|
+
}
|
|
125
|
+
|
|
115
126
|
bare_type_stripper_t ctx;
|
|
116
|
-
err = bare_type_stripper__lex(&ctx, input, (size_t) len);
|
|
127
|
+
err = bare_type_stripper__lex(&ctx, input, (size_t) len, jsx);
|
|
117
128
|
|
|
118
129
|
free(copy);
|
|
119
130
|
|
package/index.d.ts
CHANGED
|
@@ -8,12 +8,25 @@ import Buffer, { BufferEncoding } from 'bare-buffer'
|
|
|
8
8
|
* before it is lexed, so a buffer backed by a `SharedArrayBuffer` cannot change under the strip.
|
|
9
9
|
* @param encoding - Encoding used to decode `input` when it is a string (default `'utf8'`); ignored
|
|
10
10
|
* when `input` is already a `Buffer`.
|
|
11
|
-
* @param opts -
|
|
11
|
+
* @param opts - Options for stripping.
|
|
12
12
|
* @throws {TypeError} `input` is neither a string nor a buffer.
|
|
13
13
|
* @throws {SyntaxError} the source contains non-erasable TypeScript syntax (`enum`/`const enum`,
|
|
14
14
|
* `namespace`/`module` with a body, parameter properties, or angle-bracket type assertions).
|
|
15
15
|
*/
|
|
16
|
-
declare function strip(
|
|
16
|
+
declare function strip(
|
|
17
|
+
input: string | Buffer,
|
|
18
|
+
encoding?: BufferEncoding | StripOptions,
|
|
19
|
+
opts?: StripOptions
|
|
20
|
+
): Buffer
|
|
21
|
+
|
|
22
|
+
interface StripOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Read `<` where an expression starts as JSX, as in a `.tsx` file, rather than as an angle-bracket
|
|
25
|
+
* type assertion. JSX text is left as it is, and types in its expression containers are stripped.
|
|
26
|
+
* Defaults to `false`.
|
|
27
|
+
*/
|
|
28
|
+
jsx?: boolean
|
|
29
|
+
}
|
|
17
30
|
|
|
18
31
|
declare namespace strip {
|
|
19
32
|
/**
|
|
@@ -22,14 +35,16 @@ declare namespace strip {
|
|
|
22
35
|
* `SharedArrayBuffer` is copied before it is lexed; transfer a plain `ArrayBuffer` when that copy
|
|
23
36
|
* matters.
|
|
24
37
|
* @param encoding - Encoding used to decode `input` when it is a string (default `'utf8'`).
|
|
25
|
-
* @param opts -
|
|
38
|
+
* @param opts - Options for lexing, as for `strip()`.
|
|
26
39
|
*/
|
|
27
40
|
export function lex(
|
|
28
41
|
input: string | Buffer,
|
|
29
|
-
encoding?: BufferEncoding,
|
|
30
|
-
opts?:
|
|
42
|
+
encoding?: BufferEncoding | StripOptions,
|
|
43
|
+
opts?: StripOptions
|
|
31
44
|
): [start: number, end: number, flags?: number][]
|
|
32
45
|
|
|
46
|
+
export { type StripOptions }
|
|
47
|
+
|
|
33
48
|
export const constants: {
|
|
34
49
|
SEMI: number
|
|
35
50
|
PAREN: number
|
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@ const binding = require('#binding')
|
|
|
3
3
|
module.exports = exports = function strip(input, encoding, opts = {}) {
|
|
4
4
|
if (typeof encoding === 'object' && encoding !== null) {
|
|
5
5
|
opts = encoding
|
|
6
|
-
encoding =
|
|
6
|
+
encoding = undefined
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
// Strip a copy of the input: the ranges must be applied to the same bytes
|
|
@@ -11,7 +11,7 @@ module.exports = exports = function strip(input, encoding, opts = {}) {
|
|
|
11
11
|
// thread change in between.
|
|
12
12
|
const buffer = Buffer.from(toBuffer(input, encoding))
|
|
13
13
|
|
|
14
|
-
const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
14
|
+
const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength, opts.jsx === true)
|
|
15
15
|
|
|
16
16
|
check(buffer, ranges)
|
|
17
17
|
|
|
@@ -21,12 +21,12 @@ module.exports = exports = function strip(input, encoding, opts = {}) {
|
|
|
21
21
|
exports.lex = function lex(input, encoding, opts = {}) {
|
|
22
22
|
if (typeof encoding === 'object' && encoding !== null) {
|
|
23
23
|
opts = encoding
|
|
24
|
-
encoding =
|
|
24
|
+
encoding = undefined
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
const buffer = toBuffer(input, encoding)
|
|
28
28
|
|
|
29
|
-
const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
29
|
+
const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength, opts.jsx === true)
|
|
30
30
|
|
|
31
31
|
// The binding returns the ranges as a flat Uint32Array of (start, end,
|
|
32
32
|
// flags) triples; reshape into the nested form.
|
package/lib/strip.h
CHANGED
|
@@ -51,6 +51,10 @@ typedef struct {
|
|
|
51
51
|
// the budget down stops the lookahead from recognizing further arrow
|
|
52
52
|
// functions, degrading the same way exceeding MAX_DEPTH does.
|
|
53
53
|
size_t scan_budget;
|
|
54
|
+
|
|
55
|
+
// Whether '<' where an expression starts begins JSX, as in a '.tsx' file,
|
|
56
|
+
// rather than an angle cast.
|
|
57
|
+
bool jsx;
|
|
54
58
|
} bare_type_stripper_t;
|
|
55
59
|
|
|
56
60
|
// Previous-token classification, used to decide how to interpret the next
|
|
@@ -867,6 +871,137 @@ bare_type_stripper__walk_template(bare_type_stripper_t *ctx, size_t *result, uin
|
|
|
867
871
|
return 0;
|
|
868
872
|
}
|
|
869
873
|
|
|
874
|
+
static inline bool
|
|
875
|
+
bare_type_stripper__is_jsx_name(uint8_t c) {
|
|
876
|
+
return id(c) || c == '-' || c == '.' || c == ':';
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// Skip the name of a JSX tag at i, and the type arguments after it. Returns
|
|
880
|
+
// the index past them, or SIZE_MAX if the type arguments do not close.
|
|
881
|
+
static inline size_t
|
|
882
|
+
bare_type_stripper__skip_jsx_name(const utf8_t *s, size_t n, size_t i, size_t *name_end) {
|
|
883
|
+
size_t start = i;
|
|
884
|
+
|
|
885
|
+
while (i < n && bare_type_stripper__is_jsx_name(u(0))) i++;
|
|
886
|
+
|
|
887
|
+
*name_end = i;
|
|
888
|
+
|
|
889
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, i);
|
|
890
|
+
|
|
891
|
+
if (i > start && p < n && s[p] == '<') {
|
|
892
|
+
size_t ge = bare_type_stripper__scan_generic(s, n, p);
|
|
893
|
+
if (ge == p + 1) return SIZE_MAX;
|
|
894
|
+
return ge;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
return i;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// Whether the '<' at i starts JSX in a '.tsx' source. There, '<' where an
|
|
901
|
+
// expression starts is JSX, unless it opens the type parameters of a generic
|
|
902
|
+
// arrow function, written '<T,>' or '<T extends U>' so as not to read as JSX.
|
|
903
|
+
static inline bool
|
|
904
|
+
bare_type_stripper__at_jsx(const utf8_t *s, size_t n, size_t i) {
|
|
905
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, i + 1);
|
|
906
|
+
|
|
907
|
+
if (p >= n) return false;
|
|
908
|
+
if (s[p] == '>') return true;
|
|
909
|
+
if (!ids(s[p])) return false;
|
|
910
|
+
|
|
911
|
+
while (p < n && id(s[p])) p++;
|
|
912
|
+
|
|
913
|
+
p = bare_type_stripper__skip_trivia(s, n, p);
|
|
914
|
+
|
|
915
|
+
if (p < n && s[p] == ',') return false;
|
|
916
|
+
|
|
917
|
+
return !bare_type_stripper__at_kw(s, n, p, "extends", 7);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// Process a JSX element from its '<'. Its text and attribute strings are left
|
|
921
|
+
// alone, type arguments after its tag names are stripped, and the code in its
|
|
922
|
+
// expression containers is walked. Leaves the cursor just past the element.
|
|
923
|
+
static inline int
|
|
924
|
+
bare_type_stripper__process_jsx(bare_type_stripper_t *ctx, size_t *result, uint32_t depth) {
|
|
925
|
+
const utf8_t *s = ctx->s;
|
|
926
|
+
size_t n = ctx->n;
|
|
927
|
+
|
|
928
|
+
int err;
|
|
929
|
+
size_t i = *result;
|
|
930
|
+
|
|
931
|
+
// Elements still open, and whether the cursor is inside a tag.
|
|
932
|
+
int open = 0;
|
|
933
|
+
bool tag = false;
|
|
934
|
+
|
|
935
|
+
while (i < n) {
|
|
936
|
+
uint8_t ch = u(0);
|
|
937
|
+
|
|
938
|
+
if (ch == '{') {
|
|
939
|
+
err = bare_type_stripper__walk(ctx, &i, '}', false, false, depth + 1);
|
|
940
|
+
if (err < 0) return err;
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
if (tag) {
|
|
945
|
+
if (ch == '/' && i + 1 < n && u(1) == '>') {
|
|
946
|
+
i += 2;
|
|
947
|
+
tag = false;
|
|
948
|
+
if (--open == 0) break;
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
if (ch == '>') {
|
|
953
|
+
i++;
|
|
954
|
+
tag = false;
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
if (ch == '"' || ch == '\'') {
|
|
959
|
+
i++;
|
|
960
|
+
while (i < n && u(0) != ch) i++;
|
|
961
|
+
i++;
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
if (ch == '/' && i + 1 < n && (u(1) == '/' || u(1) == '*')) {
|
|
966
|
+
i = bare_type_stripper__skip_trivia(s, n, i);
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
i++;
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
if (ch == '<') {
|
|
975
|
+
if (i + 1 < n && u(1) == '/') {
|
|
976
|
+
while (i < n && u(0) != '>') i++;
|
|
977
|
+
i++;
|
|
978
|
+
if (--open == 0) break;
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
size_t ne;
|
|
983
|
+
size_t end = bare_type_stripper__skip_jsx_name(s, n, i + 1, &ne);
|
|
984
|
+
|
|
985
|
+
if (end > ne) {
|
|
986
|
+
size_t p = bare_type_stripper__skip_trivia(s, n, ne);
|
|
987
|
+
|
|
988
|
+
err = bare_type_stripper__add_range(ctx, p, end);
|
|
989
|
+
if (err < 0) return err;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
i = end;
|
|
993
|
+
open++;
|
|
994
|
+
tag = true;
|
|
995
|
+
continue;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
i++;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
*result = i > n ? n : i;
|
|
1002
|
+
return 0;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
870
1005
|
// Process a decorator '@...'. Expects s[i] == '@'. Handles three forms:
|
|
871
1006
|
//
|
|
872
1007
|
// @Name.Path bare reference
|
|
@@ -3017,6 +3152,12 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
3017
3152
|
continue;
|
|
3018
3153
|
}
|
|
3019
3154
|
}
|
|
3155
|
+
} else if (ctx->jsx && bare_type_stripper__at_jsx(s, n, i)) {
|
|
3156
|
+
err = bare_type_stripper__process_jsx(ctx, &i, depth);
|
|
3157
|
+
if (err < 0) return err;
|
|
3158
|
+
|
|
3159
|
+
prev = bare_type_stripper__prev_expr;
|
|
3160
|
+
continue;
|
|
3020
3161
|
} else {
|
|
3021
3162
|
// '<' at the start of an expression isn't valid JS - it's TS-only
|
|
3022
3163
|
// syntax: Generic arrow type parameters ('<T,>(x) => x') when a '('
|
|
@@ -3096,11 +3237,12 @@ bare_type_stripper__walk(bare_type_stripper_t *ctx, size_t *result, uint8_t stop
|
|
|
3096
3237
|
}
|
|
3097
3238
|
|
|
3098
3239
|
static inline int
|
|
3099
|
-
bare_type_stripper__lex(bare_type_stripper_t *ctx, const utf8_t *s, size_t n) {
|
|
3240
|
+
bare_type_stripper__lex(bare_type_stripper_t *ctx, const utf8_t *s, size_t n, bool jsx) {
|
|
3100
3241
|
size_t i = 0;
|
|
3101
3242
|
|
|
3102
3243
|
ctx->s = s;
|
|
3103
3244
|
ctx->n = n;
|
|
3245
|
+
ctx->jsx = jsx;
|
|
3104
3246
|
|
|
3105
3247
|
ctx->ranges = NULL;
|
|
3106
3248
|
ctx->len = 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-type-stripper",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./package": "./package.json",
|
|
@@ -49,11 +49,10 @@
|
|
|
49
49
|
"require-addon": "^1.0.2"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"bare-compat-napi": "^1.0.0",
|
|
53
52
|
"brittle": "^3.1.1",
|
|
54
53
|
"cmake-bare": "^1.1.7",
|
|
55
|
-
"cmake-
|
|
56
|
-
"cmake-
|
|
54
|
+
"cmake-fetch": "^1.5.5",
|
|
55
|
+
"cmake-napi": "^1.5.0",
|
|
57
56
|
"lunte": "^1.8.0",
|
|
58
57
|
"prettier": "^3.4.1",
|
|
59
58
|
"prettier-config-holepunch": "^2.0.0"
|
|
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
|