Skip to content

Preserve type-guard narrowing in query functions - #2379

Open
iamabhisheknag wants to merge 2 commits into
fb55:masterfrom
iamabhisheknag:feat/query-type-guard-narrowing
Open

iamabhisheknag wants to merge 2 commits into
fb55:masterfrom
iamabhisheknag:feat/query-type-guard-narrowing

Conversation

@iamabhisheknag

@iamabhisheknag iamabhisheknag commented Nov 12, 2026 •

Copy link
Copy Markdown

Calls such as filter(isText, nodes).map(node => node.data) currently lose type-guard narrowing, so TypeScript rejects access to narrowed node properties; find(), findOne() and findAll() likewise discard predicate narrowing.

Add type predicate overloads that preserve the narrowed node result, alongside the existing boolean predicate signatures. Tests cover element/text guards, narrowed element types, boolean callbacks, recursion, and limits.

Validation: npm test passes all 94 tests plus ESLint, TypeScript and Biome checks; npm run build passes. The new type checks fail before the overloads are added. Transpiled runtime JavaScript is identical with comments removed.

AI assistance: OpenAI coding assistant (GPT-6).

View guided diff

Summary by CodeRabbit

  • New Features
    • Query results now retain more specific TypeScript types when you use type-guard predicates, making it easier to work safely with the matched nodes.
  • Tests
    • Added checks for type narrowing, recursive and non-recursive searches, result limits, and the types returned by boolean predicates.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

filter, find, findOne, and findAll add overloads that narrow result types for type-guard predicates. Boolean-predicate overloads retain general result types. Tests check inferred types and query results.

Changes: Query Result Typing

Layer / File(s) Summary
Predicate overloads and type tests
src/querying.ts, src/querying.spec.ts
filter, find, findOne, and findAll add type-guard overloads with narrowed result types. Boolean-predicate overloads retain general result types. Tests cover inferred types, matches, traversal options, limits, and empty inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: fb55

Merge Risk: Minimal · up to 03c41

This change improves TypeScript type narrowing for query helpers without altering runtime behavior. Callers that pass a boolean predicate keep their previous result types. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Preserve type-guard narrowing in query functions" matches the main change: the PR adds type-predicate overloads to filter, find, findOne, and findAll so that type guards keep narrowed node …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "Preserve type-guard narrowing in query functions" matches the main change: the PR adds type-predicate overloads to filter, find, findOne, and findAll so that type guards keep narrowed node types. It is specific and concise. The title says "query functions" rather than naming filter and find, which is slightly broad, but it still describes the primary change accurately.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the types in rows,
And finds the shape each predicate knows.
With narrowed nodes tucked in its sack,
It bounds through tests and hops right back.
The query paths now tell it so.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9d848c14-6990-4631-87b1-7da58b0cf596
Commits

Reviewing files that changed from the base of the PR and between a058376 and 03c4133.

Files selected for processing: 2

  • src/querying.spec.ts
  • src/querying.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T03:27:10.860032Z 03c4133 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no blocking issues were found.

Summary

Adds type-guard signatures so filter, find, findOne, and findAll return narrower types.

  • Filter and find results keep the type named by a type guard.
  • Element searches keep the type named by a type guard.

Reviews (2) · Last reviewed commit: "Preserve narrowing in element-only query..." · Reviewed by Greptile

@iamabhisheknag iamabhisheknag changed the title Preserve type-guard narrowing in filter and find Preserve type-guard narrowing in query functions Nov 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant