Skip to content

Commit 63ef7ce

Browse files
authored
fix: don't crash when an ESQuery match has an empty range (#587)
1 parent dfc1447 commit 63ef7ce

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎e2e-tests/code-editing-and-ast-interaction.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,40 @@ test(`should keep ESQuery highlights aligned while typing before a matching lite
162162
await expect(highlight).toHaveText(["42"]);
163163
});
164164

165+
test(`should not crash when an ESQuery selector matches a node with an empty range`, async ({
166+
page,
167+
}) => {
168+
const consoleErrors: string[] = [];
169+
page.on("console", message => {
170+
if (message.type() === "error") {
171+
consoleErrors.push(message.text());
172+
}
173+
});
174+
175+
await page.goto("/");
176+
177+
const codeEditor = page.getByRole("textbox", {
178+
name: "Code Editor",
179+
exact: true,
180+
});
181+
const highlight = codeEditor.locator(".bg-editorHighlightedRangeColor");
182+
183+
await codeEditor.fill("");
184+
185+
await expect(
186+
page.getByRole("listitem").filter({ hasText: "bodyArray[]" }),
187+
).toBeVisible();
188+
189+
await page
190+
.getByRole("textbox", { name: "ESQuery Selector" })
191+
.fill("Program");
192+
193+
await codeEditor.fill("42;");
194+
195+
await expect(highlight).toHaveText(["42;"]);
196+
expect(consoleErrors).toStrictEqual([]);
197+
});
198+
165199
test.describe("AST node expansion", () => {
166200
test.beforeEach(async ({ page }) => {
167201
await page.goto("/");

‎src/utils/highlighted-ranges.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const highlightRangeDecoration = Decoration.mark({
77

88
function createHighlightedRangeDecorations(ranges: SourceRange[]) {
99
return Decoration.set(
10-
ranges.map(([rangeFrom, rangeTo]) =>
11-
highlightRangeDecoration.range(rangeFrom, rangeTo),
12-
),
10+
ranges
11+
.filter(([rangeFrom, rangeTo]) => rangeFrom < rangeTo)
12+
.map(([rangeFrom, rangeTo]) =>
13+
highlightRangeDecoration.range(rangeFrom, rangeTo),
14+
),
1315
true,
1416
);
1517
}

0 commit comments

Comments
 (0)