Skip to content

fix: VArg fallback for non-JSON values and handler sort comparator#296

Open
nperez0111 wants to merge 1 commit intounjs:mainfrom
nperez0111:fix/background-modifier-undefined
Open

fix: VArg fallback for non-JSON values and handler sort comparator#296
nperez0111 wants to merge 1 commit intounjs:mainfrom
nperez0111:fix/background-modifier-undefined

Conversation

@nperez0111
Copy link

@nperez0111 nperez0111 commented Mar 8, 2026

Problem

Two bugs combine to cause Unable to parse color from string: undefined when using modifiers like b_030712 (hex colour with a leading zero) together with extend, flatten, or rotate.

Bug 1 — VArg silently returns undefined for values that fail JSON.parse

Hex colour strings with a leading zero (e.g. "030712") are invalid JSON numbers and cause JSON.parse to throw. The catch {} block swallows the error and VArg falls off the end returning undefined.

This propagates into background.apply where String(undefined)"undefined" (the string), which gets stored as context.background. Sharp then receives background: "undefined" and throws because it cannot parse "undefined" as a colour.

Bug 2 — Sort comparator uses localeCompare on a stringified numeric order

The sort converts order: -1 to the string "-1" and compares it against modifier names like "extend" using localeCompare. Whether symbols/punctuation sort before or after letters is locale-dependent, so order: -1 modifiers (background, fit, position, quality) are not reliably applied before other modifiers.

Fix

src/handlers/utils.ts — return the raw argument string as a fallback when JSON parsing fails. One line.

src/ipx.ts — compare order values numerically (aOrder - bOrder) and fall back to name comparison only for equal-order handlers.

src/handlers/handlers.ts — removes the stale // TODO: Support background comment from flatten (background was already supported), and collapses single-property object literals to one line for consistency.

Reproduction

/images/s_1190x665,fit_contain,extend_5_5_5_5,b_030712/<image-url>
# → Error: Unable to parse color from string: undefined

With b_030712: JSON.parse("030712") throws (leading zero → invalid JSON), VArg returns undefined, String(undefined)"undefined", sharp crashes.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced argument validation to prevent undefined returns and improve reliability.
    • Fixed handler ordering logic to use numeric sorting instead of string-based comparison, ensuring consistent and predictable handler sequencing.

VArg silently returned undefined for values that fail JSON.parse() —
for example, hex colour strings with a leading zero like "030712" are
invalid JSON numbers and throw. The catch block swallowed the error,
returning undefined, which then became the string "undefined" via
String(undefined) in background.apply, ultimately passed to sharp as a
background colour and causing: "Unable to parse color from string: undefined".

Fix: return the raw argument string as a fallback when JSON parsing fails.

The handler sort comparator also had a bug: it stringified the numeric
order field (-1) and compared it with localeCompare against modifier
names. Whether punctuation/symbols sort before or after letters is
locale-dependent, so order: -1 modifiers (background, fit, position,
quality) were not reliably applied first. Fix: compare numerically with
a name-based fallback for equal-order handlers.

Also removes the stale "TODO: Support background" comment from flatten,
and collapses single-property object literals to one line for consistency.
@nperez0111 nperez0111 requested a review from pi0 as a code owner March 8, 2026 17:55
@coderabbitai
Copy link

coderabbitai bot commented Mar 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dc4052df-f19a-4907-89c0-d54db8eec033

📥 Commits

Reviewing files that changed from the base of the PR and between 3189d0e and 7f8b47f.

📒 Files selected for processing (3)
  • src/handlers/handlers.ts
  • src/handlers/utils.ts
  • src/ipx.ts

📝 Walkthrough

Walkthrough

Three files receive localized improvements: formatting refinements in handler functions, a fallback return mechanism in a utility function to prevent undefined values, and enhanced numeric sorting logic for handler ordering based on their priority values.

Changes

Cohort / File(s) Summary
Handler Formatting
src/handlers/handlers.ts
Formatting adjustments to rotate and flatten handlers; both now return results on single lines without behavioral changes.
Utility Logic Enhancement
src/handlers/utils.ts
VArg function now includes a fallback return path that returns the original argument string when input is not Infinity or valid JSON, ensuring guaranteed return value.
Sorting Improvement
src/ipx.ts
Sort comparator updated to perform numeric comparison of handler.order when available, defaulting to Infinity if not a number, with lexicographic fallback to handler name when orders are equal.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A hop through the code, so neat and so bright,
Formatting smooths the handlers' light,
Fallback returns ensure no void's plight,
Numbers now sort with proper might—
Order restored, our functions take flight! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the two main fixes: VArg fallback for non-JSON values and handler sort comparator improvements.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

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