@willbooster/web-tree-sitter 2.0.20 → 2.0.21

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.
@@ -4487,84 +4487,72 @@ var Query = class {
4487
4487
  * @param {QueryOptions} options - Options for query execution.
4488
4488
  */
4489
4489
  matches(node, options = {}) {
4490
- const startPosition = options.startPosition ?? ZERO_POINT;
4491
- const endPosition = options.endPosition ?? ZERO_POINT;
4492
- const startIndex = options.startIndex ?? 0;
4493
- const endIndex = options.endIndex ?? 0;
4494
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4495
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4496
- const startContainingIndex = options.startContainingIndex ?? 0;
4497
- const endContainingIndex = options.endContainingIndex ?? 0;
4498
- const matchLimit = options.matchLimit ?? 4294967295;
4499
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
4500
- const progressCallback = options.progressCallback;
4501
- if (typeof matchLimit !== "number") {
4502
- throw new Error("Arguments must be numbers");
4503
- }
4504
- this.matchLimit = matchLimit;
4505
- if (endIndex !== 0 && startIndex > endIndex) {
4506
- throw new Error("`startIndex` cannot be greater than `endIndex`");
4507
- }
4508
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4509
- throw new Error("`startPosition` cannot be greater than `endPosition`");
4510
- }
4511
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4512
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4513
- }
4514
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4515
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4516
- }
4517
- if (progressCallback) {
4518
- C.currentQueryProgressCallback = progressCallback;
4519
- }
4520
- marshalNode(node);
4521
- C._ts_query_matches_wasm(
4522
- this[0],
4523
- node.tree[0],
4524
- startPosition.row,
4525
- startPosition.column,
4526
- endPosition.row,
4527
- endPosition.column,
4490
+ const {
4491
+ startPosition,
4492
+ endPosition,
4528
4493
  startIndex,
4529
4494
  endIndex,
4530
- startContainingPosition.row,
4531
- startContainingPosition.column,
4532
- endContainingPosition.row,
4533
- endContainingPosition.column,
4495
+ startContainingPosition,
4496
+ endContainingPosition,
4534
4497
  startContainingIndex,
4535
4498
  endContainingIndex,
4536
4499
  matchLimit,
4537
- maxStartDepth
4538
- );
4539
- const rawCount = C.getValue(TRANSFER_BUFFER, "i32");
4540
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4541
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4542
- const result = new Array(rawCount);
4543
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4544
- let filteredCount = 0;
4545
- let address = startAddress;
4546
- for (let i2 = 0; i2 < rawCount; i2++) {
4547
- const patternIndex = C.getValue(address, "i32");
4548
- address += SIZE_OF_INT;
4549
- const captureCount = C.getValue(address, "i32");
4550
- address += SIZE_OF_INT;
4551
- const captures = new Array(captureCount);
4552
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4553
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4554
- result[filteredCount] = { patternIndex, captures };
4555
- const setProperties = this.setProperties[patternIndex];
4556
- result[filteredCount].setProperties = setProperties;
4557
- const assertedProperties = this.assertedProperties[patternIndex];
4558
- result[filteredCount].assertedProperties = assertedProperties;
4559
- const refutedProperties = this.refutedProperties[patternIndex];
4560
- result[filteredCount].refutedProperties = refutedProperties;
4561
- filteredCount++;
4500
+ maxStartDepth,
4501
+ progressCallback
4502
+ } = prepareQuery(this, options);
4503
+ return executeQuery(
4504
+ progressCallback,
4505
+ () => {
4506
+ marshalNode(node);
4507
+ C._ts_query_matches_wasm(
4508
+ this[0],
4509
+ node.tree[0],
4510
+ startPosition.row,
4511
+ startPosition.column,
4512
+ endPosition.row,
4513
+ endPosition.column,
4514
+ startIndex,
4515
+ endIndex,
4516
+ startContainingPosition.row,
4517
+ startContainingPosition.column,
4518
+ endContainingPosition.row,
4519
+ endContainingPosition.column,
4520
+ startContainingIndex,
4521
+ endContainingIndex,
4522
+ matchLimit,
4523
+ maxStartDepth
4524
+ );
4525
+ },
4526
+ (exceededMatchLimit) => {
4527
+ this.matchLimit = matchLimit;
4528
+ this.exceededMatchLimit = exceededMatchLimit;
4529
+ },
4530
+ (rawCount, startAddress) => {
4531
+ const result = new Array(rawCount);
4532
+ let filteredCount = 0;
4533
+ let address = startAddress;
4534
+ for (let i2 = 0; i2 < rawCount; i2++) {
4535
+ const patternIndex = C.getValue(address, "i32");
4536
+ address += SIZE_OF_INT;
4537
+ const captureCount = C.getValue(address, "i32");
4538
+ address += SIZE_OF_INT;
4539
+ const captures = new Array(captureCount);
4540
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4541
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4542
+ result[filteredCount] = { patternIndex, captures };
4543
+ const setProperties = this.setProperties[patternIndex];
4544
+ result[filteredCount].setProperties = setProperties;
4545
+ const assertedProperties = this.assertedProperties[patternIndex];
4546
+ result[filteredCount].assertedProperties = assertedProperties;
4547
+ const refutedProperties = this.refutedProperties[patternIndex];
4548
+ result[filteredCount].refutedProperties = refutedProperties;
4549
+ filteredCount++;
4550
+ }
4551
+ }
4552
+ result.length = filteredCount;
4553
+ return result;
4562
4554
  }
4563
- }
4564
- result.length = filteredCount;
4565
- C._free(startAddress);
4566
- C.currentQueryProgressCallback = null;
4567
- return result;
4555
+ );
4568
4556
  }
4569
4557
  /**
4570
4558
  * Iterate over all of the individual captures in the order that they
@@ -4578,85 +4566,71 @@ var Query = class {
4578
4566
  * @param {QueryOptions} options - Options for query execution.
4579
4567
  */
4580
4568
  captures(node, options = {}) {
4581
- const startPosition = options.startPosition ?? ZERO_POINT;
4582
- const endPosition = options.endPosition ?? ZERO_POINT;
4583
- const startIndex = options.startIndex ?? 0;
4584
- const endIndex = options.endIndex ?? 0;
4585
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4586
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4587
- const startContainingIndex = options.startContainingIndex ?? 0;
4588
- const endContainingIndex = options.endContainingIndex ?? 0;
4589
- const matchLimit = options.matchLimit ?? 4294967295;
4590
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
4591
- const progressCallback = options.progressCallback;
4592
- if (typeof matchLimit !== "number") {
4593
- throw new Error("Arguments must be numbers");
4594
- }
4595
- this.matchLimit = matchLimit;
4596
- if (endIndex !== 0 && startIndex > endIndex) {
4597
- throw new Error("`startIndex` cannot be greater than `endIndex`");
4598
- }
4599
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4600
- throw new Error("`startPosition` cannot be greater than `endPosition`");
4601
- }
4602
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4603
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4604
- }
4605
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4606
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4607
- }
4608
- if (progressCallback) {
4609
- C.currentQueryProgressCallback = progressCallback;
4610
- }
4611
- marshalNode(node);
4612
- C._ts_query_captures_wasm(
4613
- this[0],
4614
- node.tree[0],
4615
- startPosition.row,
4616
- startPosition.column,
4617
- endPosition.row,
4618
- endPosition.column,
4569
+ const {
4570
+ startPosition,
4571
+ endPosition,
4619
4572
  startIndex,
4620
4573
  endIndex,
4621
- startContainingPosition.row,
4622
- startContainingPosition.column,
4623
- endContainingPosition.row,
4624
- endContainingPosition.column,
4574
+ startContainingPosition,
4575
+ endContainingPosition,
4625
4576
  startContainingIndex,
4626
4577
  endContainingIndex,
4627
4578
  matchLimit,
4628
- maxStartDepth
4629
- );
4630
- const count = C.getValue(TRANSFER_BUFFER, "i32");
4631
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4632
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4633
- const result = new Array();
4634
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4635
- const captures = new Array();
4636
- let address = startAddress;
4637
- for (let i2 = 0; i2 < count; i2++) {
4638
- const patternIndex = C.getValue(address, "i32");
4639
- address += SIZE_OF_INT;
4640
- const captureCount = C.getValue(address, "i32");
4641
- address += SIZE_OF_INT;
4642
- const captureIndex = C.getValue(address, "i32");
4643
- address += SIZE_OF_INT;
4644
- captures.length = captureCount;
4645
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4646
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4647
- const capture = captures[captureIndex];
4648
- const setProperties = this.setProperties[patternIndex];
4649
- capture.setProperties = setProperties;
4650
- const assertedProperties = this.assertedProperties[patternIndex];
4651
- capture.assertedProperties = assertedProperties;
4652
- const refutedProperties = this.refutedProperties[patternIndex];
4653
- capture.refutedProperties = refutedProperties;
4654
- result.push(capture);
4579
+ maxStartDepth,
4580
+ progressCallback
4581
+ } = prepareQuery(this, options);
4582
+ const callback = progressCallback;
4583
+ return executeQuery(
4584
+ callback ? (state) => callback(state) === true : void 0,
4585
+ () => {
4586
+ marshalNode(node);
4587
+ C._ts_query_captures_wasm(
4588
+ this[0],
4589
+ node.tree[0],
4590
+ startPosition.row,
4591
+ startPosition.column,
4592
+ endPosition.row,
4593
+ endPosition.column,
4594
+ startIndex,
4595
+ endIndex,
4596
+ startContainingPosition.row,
4597
+ startContainingPosition.column,
4598
+ endContainingPosition.row,
4599
+ endContainingPosition.column,
4600
+ startContainingIndex,
4601
+ endContainingIndex,
4602
+ matchLimit,
4603
+ maxStartDepth
4604
+ );
4605
+ },
4606
+ (exceededMatchLimit) => {
4607
+ this.matchLimit = matchLimit;
4608
+ this.exceededMatchLimit = exceededMatchLimit;
4609
+ },
4610
+ (count, startAddress) => {
4611
+ const result = new Array();
4612
+ const captures = new Array();
4613
+ let address = startAddress;
4614
+ for (let i2 = 0; i2 < count; i2++) {
4615
+ const patternIndex = C.getValue(address, "i32");
4616
+ address += SIZE_OF_INT;
4617
+ const captureCount = C.getValue(address, "i32");
4618
+ address += SIZE_OF_INT;
4619
+ const captureIndex = C.getValue(address, "i32");
4620
+ address += SIZE_OF_INT;
4621
+ captures.length = captureCount;
4622
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4623
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4624
+ const capture = captures[captureIndex];
4625
+ capture.setProperties = this.setProperties[patternIndex];
4626
+ capture.assertedProperties = this.assertedProperties[patternIndex];
4627
+ capture.refutedProperties = this.refutedProperties[patternIndex];
4628
+ result.push(capture);
4629
+ }
4630
+ }
4631
+ return result;
4655
4632
  }
4656
- }
4657
- C._free(startAddress);
4658
- C.currentQueryProgressCallback = null;
4659
- return result;
4633
+ );
4660
4634
  }
4661
4635
  /** Get the predicates for a given pattern. */
4662
4636
  predicatesForPattern(patternIndex) {
@@ -4741,6 +4715,81 @@ var Query = class {
4741
4715
  return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteIndex) === 1;
4742
4716
  }
4743
4717
  };
4718
+ function prepareQuery(query, options) {
4719
+ const parameters = {
4720
+ startPosition: options.startPosition ?? ZERO_POINT,
4721
+ endPosition: options.endPosition ?? ZERO_POINT,
4722
+ startIndex: options.startIndex ?? 0,
4723
+ endIndex: options.endIndex ?? 0,
4724
+ startContainingPosition: options.startContainingPosition ?? ZERO_POINT,
4725
+ endContainingPosition: options.endContainingPosition ?? ZERO_POINT,
4726
+ startContainingIndex: options.startContainingIndex ?? 0,
4727
+ endContainingIndex: options.endContainingIndex ?? 0,
4728
+ matchLimit: options.matchLimit ?? 4294967295,
4729
+ maxStartDepth: options.maxStartDepth ?? 4294967295,
4730
+ progressCallback: options.progressCallback
4731
+ };
4732
+ const {
4733
+ startPosition,
4734
+ endPosition,
4735
+ startIndex,
4736
+ endIndex,
4737
+ startContainingPosition,
4738
+ endContainingPosition,
4739
+ startContainingIndex,
4740
+ endContainingIndex,
4741
+ matchLimit
4742
+ } = parameters;
4743
+ if (typeof matchLimit !== "number") {
4744
+ throw new Error("Arguments must be numbers");
4745
+ }
4746
+ query.matchLimit = matchLimit;
4747
+ if (endIndex !== 0 && startIndex > endIndex) {
4748
+ throw new Error("`startIndex` cannot be greater than `endIndex`");
4749
+ }
4750
+ if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4751
+ throw new Error("`startPosition` cannot be greater than `endPosition`");
4752
+ }
4753
+ if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4754
+ throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4755
+ }
4756
+ if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4757
+ throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4758
+ }
4759
+ return parameters;
4760
+ }
4761
+ __name(prepareQuery, "prepareQuery");
4762
+ function executeQuery(progressCallback, execute, updateState, readResult) {
4763
+ const previousCallback = C.currentQueryProgressCallback;
4764
+ let callbackFailure;
4765
+ C.currentQueryProgressCallback = progressCallback ? (state) => {
4766
+ try {
4767
+ return progressCallback(state);
4768
+ } catch (error) {
4769
+ callbackFailure = { error };
4770
+ return true;
4771
+ }
4772
+ } : null;
4773
+ try {
4774
+ execute();
4775
+ } finally {
4776
+ C.currentQueryProgressCallback = previousCallback;
4777
+ }
4778
+ const count = C.getValue(TRANSFER_BUFFER, "i32");
4779
+ const address = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4780
+ const exceededMatchLimit = Boolean(C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32"));
4781
+ try {
4782
+ updateState(exceededMatchLimit);
4783
+ if (callbackFailure) {
4784
+ throw callbackFailure.error;
4785
+ }
4786
+ return readResult(count, address);
4787
+ } finally {
4788
+ C._free(address);
4789
+ updateState(exceededMatchLimit);
4790
+ }
4791
+ }
4792
+ __name(executeQuery, "executeQuery");
4744
4793
  // Annotate the CommonJS export names for ESM import in node:
4745
4794
  0 && (module.exports = {
4746
4795
  CaptureQuantifier,