From 17ab744bb6c3be072cfea7a20f2e1b581ee8622b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Fo=CC=88rster?= Date: Fri, 11 Apr 2025 14:14:53 +0200 Subject: [PATCH] prevent crash in locStart/locEnd when node location is undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prettier's formatWithCursor can crash if a node is missing a valid `location` object. This patch adds null-safe checks and fallback handling in `locStart` and `locEnd` to avoid TypeErrors and ensure graceful degradation. Signed-off-by: Toni Förster --- src/parser.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/parser.js b/src/parser.js index 6845471..a815b75 100644 --- a/src/parser.js +++ b/src/parser.js @@ -204,9 +204,18 @@ const parser = { }, astFormat: "xml", locStart(node) { + if (typeof node?.location?.startOffset !== "number") { + console.warn("locStart fallback triggered:", node); + return 0; + } return node.location.startOffset; }, + locEnd(node) { + if (typeof node?.location?.endOffset !== "number") { + console.warn("locEnd fallback triggered:", node); + return 0; + } return node.location.endOffset; } };