Skip to content

feat(lint): detect tuple widening and inferred unions in noMisleadingReturnType#9843

Merged
dyc3 merged 5 commits intobiomejs:mainfrom
minseong0324:feat/no-misleading-return-type-tuple-ternary
Apr 8, 2026
Merged

feat(lint): detect tuple widening and inferred unions in noMisleadingReturnType#9843
dyc3 merged 5 commits intobiomejs:mainfrom
minseong0324:feat/no-misleading-return-type-tuple-ternary

Conversation

@minseong0324
Copy link
Copy Markdown
Contributor

I used Claude Code to assist with this implementation.

Summary

Addresses items from #9810: adds tuple element widening and inferred-union handling to noMisleadingReturnType.

Test Plan

Snapshot tests.

Docs

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 6, 2026

⚠️ No Changeset found

Latest commit: aa326fd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Apr 6, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 7, 2026

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 196 skipped benchmarks1


Comparing minseong0324:feat/no-misleading-return-type-tuple-ternary (aa326fd) with main (3d4e70a)

Open in CodSpeed

Footnotes

  1. 196 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d3946f85-4a1d-42ad-b12e-875b076b56a0

📥 Commits

Reviewing files that changed from the base of the PR and between 0014498 and c83e54f.

📒 Files selected for processing (1)
  • .changeset/afraid-items-begin.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/afraid-items-begin.md

Walkthrough

This PR extends the noMisleadingReturnType lint rule to detect two additional misleading-return scenarios: tuple element widening (when a const-asserted tuple literal is inferred narrower than a broader annotated tuple) and ternary-inferred unions (when a ternary yields a narrower string-literal union than the annotated return type). The implementation now performs structural tuple element comparisons and validates union variants of inferred types against the annotated type.

Possibly related PRs

Suggested labels

A-Diagnostic

Suggested reviewers

  • ematipico
  • dyc3
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: detecting tuple widening and inferred unions in the noMisleadingReturnType rule.
Description check ✅ Passed The description is directly related to the changeset, referencing issue #9810 and explaining the motivation for adding tuple widening and inferred-union handling.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
.changeset/afraid-items-begin.md (1)

5-5: Minor: sentence should end with a full stop.

Per coding guidelines, changeset descriptions should end every sentence with a full stop. Also consider linking to the rule documentation for discoverability.

Suggested fix
-`noMisleadingReturnType` now detects tuple element widening and ternary-inferred unions.
+[`noMisleadingReturnType`](https://biomejs.dev/linter/rules/no-misleading-return-type/) now detects tuple element widening and ternary-inferred unions.

As per coding guidelines: "End every sentence in changeset descriptions with a full stop (period)" and "for rule references, include links to the rule documentation."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.changeset/afraid-items-begin.md at line 5, Update the changeset sentence
for `noMisleadingReturnType` so it ends with a full stop and add a
discoverability link to the rule documentation; specifically, edit the line
containing "`noMisleadingReturnType` now detects tuple element widening and
ternary-inferred unions." to append a period and include a short link/reference
to the `noMisleadingReturnType` rule docs (e.g., in parentheses) immediately
after the sentence.
crates/biome_js_analyze/src/lint/nursery/no_misleading_return_type.rs (1)

811-823: Tuple comparison ignores is_optional and is_rest flags.

Per TupleElementType in crates/biome_js_type_info/src/type_data.rs:915–918, tuple elements have is_optional and is_rest fields. The current pairwise comparison only checks element types and length, so [string?, number] and [string, number] would be treated as structurally compatible. For the as const widening use case this is likely acceptable (const tuples don't typically use optional/rest elements), but worth noting if the rule scope expands.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_js_analyze/src/lint/nursery/no_misleading_return_type.rs` around
lines 811 - 823, The tuple comparison in the TypeData::Tuple arm only compares
element types and lengths, ignoring TupleElementType flags is_optional and
is_rest; update the loop that iterates ann_tuple.elements() and
inf_tuple.elements() to also check these flags for each paired element (e.g.,
require ann_e.is_optional == inf_e.is_optional and ann_e.is_rest ==
inf_e.is_rest, or implement the intended compatibility logic) and return false
on mismatch before resolving types and pushing to stack so tuples like [string?,
number] are not treated as identical to [string, number].
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In @.changeset/afraid-items-begin.md:
- Line 5: Update the changeset sentence for `noMisleadingReturnType` so it ends
with a full stop and add a discoverability link to the rule documentation;
specifically, edit the line containing "`noMisleadingReturnType` now detects
tuple element widening and ternary-inferred unions." to append a period and
include a short link/reference to the `noMisleadingReturnType` rule docs (e.g.,
in parentheses) immediately after the sentence.

In `@crates/biome_js_analyze/src/lint/nursery/no_misleading_return_type.rs`:
- Around line 811-823: The tuple comparison in the TypeData::Tuple arm only
compares element types and lengths, ignoring TupleElementType flags is_optional
and is_rest; update the loop that iterates ann_tuple.elements() and
inf_tuple.elements() to also check these flags for each paired element (e.g.,
require ann_e.is_optional == inf_e.is_optional and ann_e.is_rest ==
inf_e.is_rest, or implement the intended compatibility logic) and return false
on mismatch before resolving types and pushing to stack so tuples like [string?,
number] are not treated as identical to [string, number].

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0a841b04-4578-44b7-b22b-1b73f17ca45d

📥 Commits

Reviewing files that changed from the base of the PR and between 8040ec2 and 0014498.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_analyze/tests/specs/nursery/noMisleadingReturnType/invalid.ts.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/nursery/noMisleadingReturnType/valid.ts.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (4)
  • .changeset/afraid-items-begin.md
  • crates/biome_js_analyze/src/lint/nursery/no_misleading_return_type.rs
  • crates/biome_js_analyze/tests/specs/nursery/noMisleadingReturnType/invalid.ts
  • crates/biome_js_analyze/tests/specs/nursery/noMisleadingReturnType/valid.ts

@minseong0324 minseong0324 force-pushed the feat/no-misleading-return-type-tuple-ternary branch from 0014498 to c83e54f Compare April 7, 2026 04:19
Comment thread .changeset/afraid-items-begin.md Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no changeset needed, we haven't released the rule

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed in this commit. aa326fd

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

Labels

A-Linter Area: linter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants