Skip to content

Commit 6b4e6e3

Browse files
seanmcguire12jeffbargclaude
authored
Fix type safety for undefined xpath values (#863) (#865)
## Summary - Add proper type annotations for xpath lookup that can return undefined - Update trimTrailingTextNode to handle undefined input safely - Fix potential runtime errors when xpath is not found in combinedXpathMap ## Test plan - [x] Verify no TypeScript errors 🤖 Generated with [Claude Code](https://claude.ai/code) # why # what changed # test plan --------- Co-authored-by: Jeff Barg <jeffbarg94@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent a0efb60 commit 6b4e6e3

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

.changeset/bumpy-mugs-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
improve type safety for trimTrailingTextNode

lib/handlers/observeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class StagehandObserveHandler {
170170

171171
if (elementId.includes("-")) {
172172
const lookUpIndex = elementId as EncodedId;
173-
const xpath = combinedXpathMap[lookUpIndex];
173+
const xpath: string | undefined = combinedXpathMap[lookUpIndex];
174174

175175
const trimmedXpath = trimTrailingTextNode(xpath);
176176

lib/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ export function loadApiKeyFromEnv(
493493
return undefined;
494494
}
495495

496-
export function trimTrailingTextNode(path: string): string {
497-
return path.replace(/\/text\(\)(\[\d+\])?$/iu, "");
496+
export function trimTrailingTextNode(
497+
path: string | undefined,
498+
): string | undefined {
499+
return path?.replace(/\/text\(\)(\[\d+\])?$/iu, "");
498500
}

0 commit comments

Comments
 (0)