Merge 9db5ef325b into 6739742fb6
This commit is contained in:
commit
906e684345
1 changed files with 64 additions and 0 deletions
64
lib/binding_web/test/node-id-stability.test.ts
Normal file
64
lib/binding_web/test/node-id-stability.test.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
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";\n`;
|
||||
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;\n`;
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue