Require fallback parameter in valueSet() for empty list safety#747
Require fallback parameter in valueSet() for empty list safety#747
valueSet() for empty list safety#747Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #747 +/- ##
==========================================
- Coverage 91.38% 91.33% -0.06%
==========================================
Files 39 39
Lines 20867 20901 +34
Branches 5689 5710 +21
==========================================
+ Hits 19070 19089 +19
- Misses 1749 1764 +15
Partials 48 48 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces a breaking change to the valueSet() function, which now requires a mandatory second parameter to specify fallback text for empty input arrays. Additionally, the values() function now throws a TypeError when given an empty array. The changes include comprehensive updates to documentation, unit tests, and internal logic across multiple packages to ensure compliance with the new API requirements. I have no feedback to provide.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (15)
WalkthroughThis PR changes empty-list handling for message utilities: Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/concepts/messages.md`:
- Around line 361-363: The locale definition entry contains a sentence fragment;
update the second sentence in the `locale` description so it is a complete
sentence (e.g., "Defaults to the system locale.") and ensure the `locale` entry
clearly describes accepted types (string, array of strings, Intl.Locale object,
or array of Intl.Locale objects) and the default behavior; edit the `locale`
definition text to replace the fragment with the complete sentence.
In `@packages/core/src/message.test.ts`:
- Around line 1129-1141: Update the test for valueSet to assert the thrown error
message as well as the TypeError for both call sites: when calling (valueSet as
any)([]) and (valueSet as any)([], { locale: "en" }). Locate the two
assert.throws calls in the "should throw TypeError when called without fallback"
test and replace/augment them to validate the error.message (via exact string or
regex) so the test verifies the expected diagnostic text from the runtime
validation path in addition to the TypeError.
- Around line 202-207: Update the test that currently only checks the error type
for values([]) to also assert the exact diagnostic message: when calling
values([]) assert.throws should verify both TypeError and the specific
error.message (either by passing the expected message string or a RegExp) so the
test asserts the error classification and the exact validation text produced by
the values function/constructor.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: d2ba95be-7da2-4616-8d92-67754c69a21c
⛔ Files ignored due to path filters (1)
deno.lockis excluded by!**/*.lock
📒 Files selected for processing (15)
CHANGES.mddocs/concepts/messages.mddocs/integrations/git.mdpackages/core/src/constructs.test.tspackages/core/src/doc.test.tspackages/core/src/message.test.tspackages/core/src/message.tspackages/core/src/primitives.tspackages/core/src/valueparser.tspackages/env/src/index.tspackages/git/src/index.test.tspackages/git/src/index.tspackages/man/src/man.test.tspackages/man/src/roff.test.tspackages/zod/src/index.ts
#747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Check both the error type and the diagnostic message pattern in values() and valueSet() throw tests, so regressions in error classification or message wording are caught. #747 (comment) #747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@codex review |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a breaking change to the valueSet() function, which now requires a second parameter to specify a fallback string or options object for empty arrays. Additionally, the values() function has been updated to throw a TypeError when provided with an empty array. These changes aim to prevent malformed sentences in message templates when lists are empty. The feedback provided suggests refactoring several error message implementations in packages/git/src/index.ts to reduce code duplication by using nested message templates for conditional parts.
|
Codex Review: Didn't find any major issues. 👍 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Use nested message templates to avoid repeating the base error message in both branches of the conditional. The period is kept outside the conditional for clarity. #747 (comment) #747 (comment) #747 (comment) #747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@codex review |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a breaking change to the valueSet() function, which now requires a mandatory second parameter for fallback text when the input array is empty. Additionally, the values() function has been updated to throw a TypeError if provided with an empty array. These changes aim to prevent malformed error messages in CLI outputs. The feedback suggests extracting a repeated conditional message pattern in packages/git/src/index.ts into a helper function to improve maintainability and adhere to DRY principles.
|
Codex Review: Didn't find any major issues. Hooray! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Both functions now throw TypeError when given an empty array. Previously, empty arrays produced zero message terms, which caused surrounding prose in message templates to collapse into malformed sentences (e.g., "Expected one of ."). #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Omit the "Available ..." suffix when the list is empty, instead of passing an empty array to valueSet() which now throws TypeError. #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
#492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Instead of throwing TypeError for empty arrays, valueSet() now requires a second parameter (a fallback string or options with a fallback field). When the values array is empty, the fallback text is returned as a single text term; an empty fallback string produces an empty Message. This replaces the throwing approach, which would have turned normal validation failures into runtime exceptions in user callbacks that receive potentially-empty arrays. #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Revise the @optique/core entry to describe the required fallback parameter instead of the throwing approach. #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Update Twoslash code examples and option descriptions in docs/concepts/messages.md and docs/integrations/git.md to use the new required fallback parameter. #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
JavaScript callers or code compiled against the old signature can omit the fallback parameter, producing a malformed message term with undefined text. The function now throws TypeError at the call site when the fallback is missing or invalid. #492 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
#747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Check both the error type and the diagnostic message pattern in values() and valueSet() throw tests, so regressions in error classification or message wording are caught. #747 (comment) #747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use nested message templates to avoid repeating the base error message in both branches of the conditional. The period is kept outside the conditional for clarity. #747 (comment) #747 (comment) #747 (comment) #747 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
85d8b7b to
b2463c8
Compare
Summary
Fixes #492
valueSet([])andvalues([])previously returned empty message content that, when embedded inmessagetagged templates, collapsed surrounding prose into malformed sentences like "Expected one of ." or "Allowed values: .".This PR makes
valueSet()require a second parameter that specifies what to display when the values array is empty. The parameter can be either a simple fallback string or an options object with a requiredfallbackfield. This is a breaking change to thevalueSet()API.The fallback is enforced both at the type level (
ValueSetOptions.fallbackis a required field) and at runtime (throwsTypeErrorfor JavaScript callers or code compiled against the old signature).For
values(), which is only used internally to represent consumed tokens, the function now throwsTypeErrorfor empty arrays since an empty consumed token list is always a bug.The git parser error messages in @optique/git have been updated to omit the "Available ..." suffix when the list is empty, and user-facing callbacks like
notFound(input, available)can now use the fallback parameter directly instead of manual ternary guards:valueSet(available ?? [], "none").Test plan
mise testpasses across all runtimes (Deno, Node.js, Bun)mise checkpasses (type check, lint, format, dry-run publish)pnpm buildunder docs/ passes (Twoslash type checking for code examples)