From 46f89432417dd68a360524ebb3856ca0b42f5c24 Mon Sep 17 00:00:00 2001 From: Vlady Veselinov Date: Thu, 6 Nov 2025 07:57:18 +0000 Subject: [PATCH 1/2] add node id stability test --- .../test/node-id-stability.test.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib/binding_web/test/node-id-stability.test.ts diff --git a/lib/binding_web/test/node-id-stability.test.ts b/lib/binding_web/test/node-id-stability.test.ts new file mode 100644 index 00000000..f73bc1d1 --- /dev/null +++ b/lib/binding_web/test/node-id-stability.test.ts @@ -0,0 +1,68 @@ +import { describe, it, expect, beforeAll, beforeEach, afterEach } from "vitest"; +import { Language, Tree, Edit } from "../src"; +import { Parser } from "../src"; +import helper from "./helper"; + +describe("node id stability", () => { + let parser: Parser; + let tree: Tree | null; + let JavaScript: Language; + + beforeAll(async () => { + ({ JavaScript } = await helper); + }); + + beforeEach(() => { + tree = null; + parser = new Parser(); + parser.setLanguage(JavaScript); + }); + + afterEach(() => { + parser.delete(); + tree!.delete(); + }); + + it("node ID of existing variable_declarator is stable after inserting second declaration", () => { + const source1 = ` + let name = "John Doe"; + `; + tree = parser.parse(source1); + + const variableDeclaratorsBefore = tree?.rootNode.descendantsOfType( + "variable_declarator" + ); + const firstDeclarationIdBefore = variableDeclaratorsBefore?.[0]?.id; + + expect(firstDeclarationIdBefore).toBeTypeOf("number"); + + const source2 = ` + let age = 31; + `; + + const startIndex = 0; + const startPosition = { row: 0, column: 0 }; + const newEndPosition = { row: 1, column: 0 }; + + const edit = new Edit({ + startIndex, + oldEndIndex: startIndex, + newEndIndex: startIndex + source2.length, + startPosition, + oldEndPosition: startPosition, + newEndPosition, + }); + + tree?.edit(edit); + + const newText = source2 + source1; + tree = parser.parse(newText, tree); + + const variableDeclaratorsAfter = tree?.rootNode.descendantsOfType( + "variable_declarator" + ); + const nodeIds = variableDeclaratorsAfter?.map((n) => n.id); + + expect(nodeIds).toContain(firstDeclarationIdBefore); + }); +}); From 9db5ef325baed90ab1def665f71af9edbf76508b Mon Sep 17 00:00:00 2001 From: Vlady Veselinov Date: Thu, 6 Nov 2025 08:09:36 +0000 Subject: [PATCH 2/2] clean up fixture --- lib/binding_web/test/node-id-stability.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/binding_web/test/node-id-stability.test.ts b/lib/binding_web/test/node-id-stability.test.ts index f73bc1d1..389bfc0e 100644 --- a/lib/binding_web/test/node-id-stability.test.ts +++ b/lib/binding_web/test/node-id-stability.test.ts @@ -24,9 +24,7 @@ describe("node id stability", () => { }); it("node ID of existing variable_declarator is stable after inserting second declaration", () => { - const source1 = ` - let name = "John Doe"; - `; + const source1 = `let name = "John Doe";\n`; tree = parser.parse(source1); const variableDeclaratorsBefore = tree?.rootNode.descendantsOfType( @@ -36,9 +34,7 @@ describe("node id stability", () => { expect(firstDeclarationIdBefore).toBeTypeOf("number"); - const source2 = ` - let age = 31; - `; + const source2 = `let age = 31;\n`; const startIndex = 0; const startPosition = { row: 0, column: 0 };