@willbooster/web-tree-sitter 1.5.0 → 2.0.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 CHANGED
@@ -158,12 +158,8 @@ npm install @willbooster/tree-sitter-javascript
158
158
  ```
159
159
 
160
160
  Then you can find `tree-sitter-javascript.wasm` in the `node_modules/@willbooster/tree-sitter-javascript` directory.
161
- Other grammar packages on npm, such as `tree-sitter-javascript`, may ship a `.wasm` file as well.
162
-
163
- #### From GitHub
164
-
165
- You can also download the `.wasm` files from the GitHub releases of grammars that publish them with Tree-sitter's
166
- reusable workflow, such as the tree-sitter-javascript [releases page][gh release js].
161
+ Take a release of the grammar package that was generated for this package's major version: the releases generated for
162
+ `@willbooster/web-tree-sitter` 1 are rejected (see [Wasm compatibility](#wasm-compatibility)).
167
163
 
168
164
  #### Generating `.wasm` files
169
165
 
@@ -171,13 +167,16 @@ You can also generate the `.wasm` file for your desired grammar with the CLI tha
171
167
  repository][releases] from v1.0.7 on carry as `tree-sitter-cli-<platform>.tar.gz` (`linux-x64`, `linux-arm64`,
172
168
  `macos-arm64`, or `macos-x64`), which extracts to an executable `tree-sitter`; a workflow builds and attaches the
173
169
  archives after a release is published, so the newest release lacks them for a while. `tree-sitter build --wasm`
174
- downloads [wasi-sdk][] on first use, so no other tools need to be installed. For example, for the JavaScript grammar:
170
+ downloads [wasi-sdk][] on first use, so no other tools need to be installed. Regenerate the parser with this CLI
171
+ before building it, because the one a grammar package ships was generated by another CLI. For example, for the
172
+ JavaScript grammar:
175
173
 
176
174
  ```sh
177
175
  platform=macos-arm64 # or linux-x64, linux-arm64, macos-x64
178
- tag=v1.0.9 # a release that lists tree-sitter-cli-$platform.tar.gz among its assets
176
+ tag=v2.0.0 # a release from v2.0.0 on that lists tree-sitter-cli-$platform.tar.gz among its assets
179
177
  curl -fsSL "https://github.com/WillBooster/tree-sitter/releases/download/$tag/tree-sitter-cli-$platform.tar.gz" | tar -xz
180
178
  npm install tree-sitter-javascript
179
+ (cd node_modules/tree-sitter-javascript && ../../tree-sitter generate src/grammar.json)
181
180
  ./tree-sitter build --wasm node_modules/tree-sitter-javascript
182
181
  ```
183
182
 
@@ -185,11 +184,12 @@ If everything is fine, file `tree-sitter-javascript.wasm` should be generated in
185
184
 
186
185
  ### Wasm compatibility
187
186
 
188
- `@willbooster/web-tree-sitter` loads parsers of ABI versions 13 to 16. Compact ABI 16 parsers require this runtime's ABI 16 support.
189
-
190
- > [!WARNING]
191
- > Some prebuilt `.wasm` files use an older dynamic-linking format that newer versions of `web-tree-sitter` cannot
192
- > load, even if their parser ABI is supported. Rebuild these files using a current tree-sitter CLI.
187
+ `@willbooster/web-tree-sitter` loads only parsers generated by the CLI of this repository, which marks them with the
188
+ language ABI number `0x57420001`. It rejects every other `.wasm` file: the ones built from parsers that upstream's
189
+ Tree-sitter CLI generated, such as those upstream grammar packages and releases ship, and the ones built by the CLI
190
+ releases of this repository before v2.0.0, such as those in the `@willbooster/tree-sitter-*` releases generated for
191
+ `@willbooster/web-tree-sitter` 1. Upstream's `web-tree-sitter` rejects the files built here. Upgrade this package and
192
+ the grammar `.wasm` files together.
193
193
 
194
194
  ### Running .wasm in Node.js
195
195
 
@@ -254,7 +254,6 @@ For more information on the module options you can pass in, see the [emscripten
254
254
  For changes to the bindings or DSL declarations, read [the contributor guide](https://github.com/WillBooster/tree-sitter/blob/main/lib/binding_web/CONTRIBUTING.md).
255
255
 
256
256
  [emscripten-module-options]: https://emscripten.org/docs/api_reference/module.html#affecting-execution
257
- [gh release js]: https://github.com/tree-sitter/tree-sitter-javascript/releases/latest
258
257
  [releases]: https://github.com/WillBooster/tree-sitter/releases
259
258
  [node bindings]: https://github.com/tree-sitter/node-tree-sitter
260
259
  [wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
@@ -3877,6 +3877,15 @@ __name(checkModule, "checkModule");
3877
3877
  // src/parser.ts
3878
3878
  var TRANSFER_BUFFER;
3879
3879
  var LANGUAGE_VERSION;
3880
+ function assertSupportedLanguage(language) {
3881
+ const version = C._ts_language_abi_version(language[0]);
3882
+ if (version !== LANGUAGE_VERSION) {
3883
+ throw new Error(
3884
+ `Incompatible language version 0x${(version >>> 0).toString(16)}. Expected 0x${LANGUAGE_VERSION.toString(16)}.`
3885
+ );
3886
+ }
3887
+ }
3888
+ __name(assertSupportedLanguage, "assertSupportedLanguage");
3880
3889
  var MIN_COMPATIBLE_VERSION;
3881
3890
  var finalizer4 = newFinalizer((addresses) => {
3882
3891
  C._ts_parser_delete(addresses[0]);
@@ -3939,8 +3948,8 @@ var Parser = class {
3939
3948
  * If the language was not successfully assigned, an error will be thrown.
3940
3949
  * This happens if the language was generated with an incompatible
3941
3950
  * version of the Tree-sitter CLI. Check the language's version using
3942
- * {@link Language#version} and compare it to this library's
3943
- * {@link LANGUAGE_VERSION} and {@link MIN_COMPATIBLE_VERSION} constants.
3951
+ * {@link Language#abiVersion} and compare it to this library's
3952
+ * {@link LANGUAGE_VERSION} constant.
3944
3953
  */
3945
3954
  setLanguage(language) {
3946
3955
  let address;
@@ -3949,12 +3958,7 @@ var Parser = class {
3949
3958
  this.language = null;
3950
3959
  } else if (language.constructor === Language) {
3951
3960
  address = language[0];
3952
- const version = C._ts_language_abi_version(address);
3953
- if (version < MIN_COMPATIBLE_VERSION || LANGUAGE_VERSION < version) {
3954
- throw new Error(
3955
- `Incompatible language version ${version}. Compatibility range ${MIN_COMPATIBLE_VERSION} through ${LANGUAGE_VERSION}.`
3956
- );
3957
- }
3961
+ assertSupportedLanguage(language);
3958
3962
  this.language = language;
3959
3963
  } else {
3960
3964
  throw new Error("Argument must be a Language");
@@ -4352,6 +4356,7 @@ var Query = class {
4352
4356
  * @link {@see https://tree-sitter.github.io/tree-sitter/using-parsers/queries}
4353
4357
  */
4354
4358
  constructor(language, source) {
4359
+ assertSupportedLanguage(language);
4355
4360
  const sourceLength = C.lengthBytesUTF8(source);
4356
4361
  const sourceAddress = C._malloc(sourceLength + 1);
4357
4362
  C.stringToUTF8(source, sourceAddress, sourceLength + 1);