@willbooster/tree-sitter-javascript 20.0.0 → 21.1.0
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 +6 -5
- package/package.json +4 -3
- package/src/literal.h +155 -0
- package/tree-sitter-javascript.wasm +0 -0
- package/tree-sitter-javascript_script.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -26,8 +26,8 @@ the trees that error recovery produces may change; such changes are released as
|
|
|
26
26
|
|
|
27
27
|
The npm package ships two languages for
|
|
28
28
|
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
|
|
29
|
-
browsers, and Cloudflare Workers. The parser carries the ABI number of WillBooster/tree-sitter (`
|
|
30
|
-
requires that runtime ABI; the upstream runtime rejects it. This package is tested with runtime
|
|
29
|
+
browsers, and Cloudflare Workers. The parser carries the ABI number of WillBooster/tree-sitter (`0x5742000c`), so it
|
|
30
|
+
requires that runtime ABI; the upstream runtime rejects it. This package is tested with runtime 17.0.1, and its optional peer dependency requires 17.0.1 or later within 17.x. In Node.js and Bun, load it from
|
|
31
31
|
the package. Choose `tree-sitter-javascript.wasm` for the Module goal, or
|
|
32
32
|
`tree-sitter-javascript_script.wasm` for the Script goal. The source goal comes from the host or file type;
|
|
33
33
|
`.mjs` uses Module and `.cjs` uses Script, while `.js` depends on the host. For example, `await\nvalue` is one
|
|
@@ -77,13 +77,14 @@ parser.setLanguage(await Language.load(javascript));
|
|
|
77
77
|
|
|
78
78
|
The package also ships the node types in `src/node-types.json`, `tree-sitter.json`, and `grammar.js` for grammars
|
|
79
79
|
that extend this one (e.g. `require('@willbooster/tree-sitter-javascript/grammar')`). Its JSDoc describes the
|
|
80
|
-
array/formal and object-rest extension hooks.
|
|
80
|
+
array/formal and object-rest extension hooks. Their external scanners can include `src/literal.h`, which scans template
|
|
81
|
+
characters and escapes and JSX text for the tokens they pass it. The crate ships `tree-sitter.json` too.
|
|
81
82
|
|
|
82
83
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-javascript) and the
|
|
83
|
-
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) runtime with the matching ABI. The tested version is
|
|
84
|
+
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) runtime with the matching ABI. The tested version is 17.0.1:
|
|
84
85
|
|
|
85
86
|
```sh
|
|
86
|
-
cargo add willbooster-tree-sitter@
|
|
87
|
+
cargo add willbooster-tree-sitter@17.0.1 --rename tree-sitter
|
|
87
88
|
cargo add willbooster-tree-sitter-javascript --rename tree-sitter-javascript
|
|
88
89
|
```
|
|
89
90
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-javascript",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "21.1.0",
|
|
4
4
|
"description": "JavaScript grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"grammar.js",
|
|
19
|
+
"src/literal.h",
|
|
19
20
|
"grammars/script/grammar.js",
|
|
20
21
|
"grammars/script/src/node-types.json",
|
|
21
22
|
"tree-sitter.json",
|
|
@@ -52,7 +53,7 @@
|
|
|
52
53
|
"@vitest/browser-playwright": "5.0.5",
|
|
53
54
|
"@willbooster/oxlint-config": "1.4.9",
|
|
54
55
|
"@willbooster/wb": "23.0.2",
|
|
55
|
-
"@willbooster/web-tree-sitter": "
|
|
56
|
+
"@willbooster/web-tree-sitter": "17.0.1",
|
|
56
57
|
"acorn": "8.19.0",
|
|
57
58
|
"acorn-jsx": "5.3.2",
|
|
58
59
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
"wrangler": "4.152.0"
|
|
67
68
|
},
|
|
68
69
|
"peerDependencies": {
|
|
69
|
-
"@willbooster/web-tree-sitter": "^
|
|
70
|
+
"@willbooster/web-tree-sitter": "^17.0.1"
|
|
70
71
|
},
|
|
71
72
|
"peerDependenciesMeta": {
|
|
72
73
|
"@willbooster/web-tree-sitter": {
|
package/src/literal.h
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_JAVASCRIPT_LITERAL_H_
|
|
2
|
+
#define TREE_SITTER_JAVASCRIPT_LITERAL_H_
|
|
3
|
+
|
|
4
|
+
#include "tree_sitter/parser.h"
|
|
5
|
+
|
|
6
|
+
static bool scan_character_reference(TSLexer *lexer);
|
|
7
|
+
static bool scan_template_escape_value(TSLexer *lexer);
|
|
8
|
+
static bool scan_fixed_hex_digits(TSLexer *lexer, unsigned length);
|
|
9
|
+
static bool scan_template_code_point(TSLexer *lexer);
|
|
10
|
+
static int hex_digit_value(int32_t c);
|
|
11
|
+
static bool is_ascii_letter(int32_t c);
|
|
12
|
+
static bool is_ascii_digit(int32_t c);
|
|
13
|
+
static bool reference_is_digit(int32_t c, bool hex);
|
|
14
|
+
|
|
15
|
+
static bool scan_template_chars(TSLexer *lexer, TSSymbol symbol) {
|
|
16
|
+
lexer->result_symbol = symbol;
|
|
17
|
+
for (bool has_content = false;; has_content = true) {
|
|
18
|
+
lexer->mark_end(lexer);
|
|
19
|
+
if (lexer->eof(lexer)) return false;
|
|
20
|
+
switch (lexer->lookahead) {
|
|
21
|
+
case '`':
|
|
22
|
+
case '\\':
|
|
23
|
+
return has_content;
|
|
24
|
+
case '$':
|
|
25
|
+
lexer->advance(lexer, false);
|
|
26
|
+
if (lexer->lookahead == '{') return has_content;
|
|
27
|
+
break;
|
|
28
|
+
default:
|
|
29
|
+
lexer->advance(lexer, false);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static bool scan_jsx_text(TSLexer *lexer, TSSymbol symbol) {
|
|
35
|
+
bool saw_text = false;
|
|
36
|
+
bool at_newline = false;
|
|
37
|
+
lexer->result_symbol = symbol;
|
|
38
|
+
for (;;) {
|
|
39
|
+
lexer->mark_end(lexer);
|
|
40
|
+
if (lexer->eof(lexer)) return saw_text;
|
|
41
|
+
switch (lexer->lookahead) {
|
|
42
|
+
case '<': case '>': case '{': case '}':
|
|
43
|
+
return saw_text;
|
|
44
|
+
case '&':
|
|
45
|
+
if (scan_character_reference(lexer)) return saw_text;
|
|
46
|
+
saw_text = true;
|
|
47
|
+
at_newline = false;
|
|
48
|
+
continue;
|
|
49
|
+
default:
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
// Use ASCII classes here because Babel retains Unicode spaces in JSX text.
|
|
53
|
+
bool is_space = (lexer->lookahead >= '\t' && lexer->lookahead <= '\r') || lexer->lookahead == ' ';
|
|
54
|
+
// Babel splits JSX text into lines at CR/LF; U+2028 and U+2029 stay text.
|
|
55
|
+
if (lexer->lookahead == '\n' || lexer->lookahead == '\r') {
|
|
56
|
+
at_newline = true;
|
|
57
|
+
} else {
|
|
58
|
+
at_newline &= is_space;
|
|
59
|
+
saw_text |= !at_newline;
|
|
60
|
+
}
|
|
61
|
+
lexer->advance(lexer, false);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static bool scan_character_reference(TSLexer *lexer) {
|
|
66
|
+
lexer->advance(lexer, false);
|
|
67
|
+
unsigned length = 0;
|
|
68
|
+
if (lexer->lookahead == '#') {
|
|
69
|
+
lexer->advance(lexer, false);
|
|
70
|
+
bool hex = lexer->lookahead == 'x' || lexer->lookahead == 'X';
|
|
71
|
+
if (hex) lexer->advance(lexer, false);
|
|
72
|
+
unsigned max_length = hex ? 6 : 5;
|
|
73
|
+
while (length < max_length && reference_is_digit(lexer->lookahead, hex)) {
|
|
74
|
+
lexer->advance(lexer, false);
|
|
75
|
+
length++;
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
while (length < 30 && is_ascii_letter(lexer->lookahead)) {
|
|
79
|
+
lexer->advance(lexer, false);
|
|
80
|
+
length++;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return length > 0 && lexer->lookahead == ';';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static bool scan_template_escape(TSLexer *lexer, TSSymbol symbol) {
|
|
87
|
+
lexer->advance(lexer, false);
|
|
88
|
+
if (lexer->eof(lexer) || !scan_template_escape_value(lexer)) return false;
|
|
89
|
+
lexer->mark_end(lexer);
|
|
90
|
+
lexer->result_symbol = symbol;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static bool scan_template_escape_value(TSLexer *lexer) {
|
|
95
|
+
int32_t c = lexer->lookahead;
|
|
96
|
+
lexer->advance(lexer, false);
|
|
97
|
+
switch (c) {
|
|
98
|
+
case '0':
|
|
99
|
+
return !is_ascii_digit(lexer->lookahead);
|
|
100
|
+
case 'x':
|
|
101
|
+
return scan_fixed_hex_digits(lexer, 2);
|
|
102
|
+
case 'u':
|
|
103
|
+
return lexer->lookahead == '{' ? scan_template_code_point(lexer) : scan_fixed_hex_digits(lexer, 4);
|
|
104
|
+
case '\r':
|
|
105
|
+
if (lexer->lookahead == '\n') lexer->advance(lexer, false);
|
|
106
|
+
return true;
|
|
107
|
+
default:
|
|
108
|
+
return !is_ascii_digit(c);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static bool scan_fixed_hex_digits(TSLexer *lexer, unsigned length) {
|
|
113
|
+
for (unsigned i = 0; i < length; i++) {
|
|
114
|
+
if (hex_digit_value(lexer->lookahead) < 0) return false;
|
|
115
|
+
lexer->advance(lexer, false);
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static bool scan_template_code_point(TSLexer *lexer) {
|
|
121
|
+
lexer->advance(lexer, false);
|
|
122
|
+
uint32_t value = 0;
|
|
123
|
+
bool has_digit = false;
|
|
124
|
+
int digit;
|
|
125
|
+
while ((digit = hex_digit_value(lexer->lookahead)) >= 0) {
|
|
126
|
+
has_digit = true;
|
|
127
|
+
value = value * 16 + (unsigned)digit;
|
|
128
|
+
if (value > 0x10FFFF) return false;
|
|
129
|
+
lexer->advance(lexer, false);
|
|
130
|
+
}
|
|
131
|
+
if (!has_digit || lexer->lookahead != '}') return false;
|
|
132
|
+
lexer->advance(lexer, false);
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static int hex_digit_value(int32_t c) {
|
|
137
|
+
if (is_ascii_digit(c)) return c - '0';
|
|
138
|
+
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
139
|
+
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
140
|
+
return -1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static bool is_ascii_letter(int32_t c) {
|
|
144
|
+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static bool reference_is_digit(int32_t c, bool hex) {
|
|
148
|
+
return hex ? hex_digit_value(c) >= 0 : is_ascii_digit(c);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static bool is_ascii_digit(int32_t c) {
|
|
152
|
+
return c >= '0' && c <= '9';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#endif
|
|
Binary file
|
|
Binary file
|