clarity-pattern-parser 10.2.12 → 10.2.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.2.12",
3
+ "version": "10.2.13",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -392,6 +392,26 @@ describe("Node", () => {
392
392
  expect(parent.endIndex).toBe(11);
393
393
  });
394
394
 
395
+ test("Normalize values and index deep", () => {
396
+ const firstChild = new Node("literal", "first", 0, 0, [], "first");
397
+ const secondChild = new Node("literal", "second", 0, 0, [], "second");
398
+ const grandChild = new Node("literal", "three", 0, 0, [], "three");
399
+ const parent = new Node("literal", "parent", 0, 0);
400
+
401
+ parent.appendChild(firstChild);
402
+ parent.appendChild(secondChild);
403
+
404
+ parent.normalize();
405
+
406
+ secondChild.appendChild(grandChild);
407
+ parent.normalize();
408
+
409
+ expect(parent.startIndex).toBe(0);
410
+ expect(parent.firstIndex).toBe(0);
411
+ expect(parent.lastIndex).toBe(9);
412
+ expect(parent.endIndex).toBe(10);
413
+ });
414
+
395
415
  test("Normalize values with no values", () => {
396
416
  const node = new Node("literal", "node", 0, 0, [], "");
397
417
  node.normalize();
@@ -451,8 +471,6 @@ describe("Node", () => {
451
471
  Node.createValueNode("aunt", "aunt")
452
472
  ]);
453
473
 
454
-
455
-
456
474
  expect(result.toJson()).toBe(expected.toJson());
457
475
  });
458
476
 
package/src/ast/Node.ts CHANGED
@@ -291,11 +291,17 @@ export class Node {
291
291
 
292
292
  normalize(startIndex = this._firstIndex): number {
293
293
  let length = 0;
294
+ let runningOffset = startIndex;
294
295
 
295
296
  if (this.children.length === 0) {
296
297
  length = this._value.length;
297
298
  } else {
298
- length = this.children.reduce((acc, c) => acc + c.normalize(acc + startIndex), startIndex) - startIndex;
299
+ for (let x = 0; x < this.children.length; x++) {
300
+ const child = this.children[x];
301
+ const childLength = child.normalize(runningOffset);
302
+ runningOffset += childLength;
303
+ length += childLength;
304
+ }
299
305
  }
300
306
 
301
307
  this._firstIndex = startIndex;