fix: VArg fallback for non-JSON values and handler sort comparator#296
fix: VArg fallback for non-JSON values and handler sort comparator#296nperez0111 wants to merge 1 commit intounjs:mainfrom
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThree 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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 |
Problem
Two bugs combine to cause
Unable to parse color from string: undefinedwhen using modifiers likeb_030712(hex colour with a leading zero) together withextend,flatten, orrotate.Bug 1 —
VArgsilently returnsundefinedfor values that failJSON.parseHex colour strings with a leading zero (e.g.
"030712") are invalid JSON numbers and causeJSON.parseto throw. Thecatch {}block swallows the error andVArgfalls off the end returningundefined.This propagates into
background.applywhereString(undefined)→"undefined"(the string), which gets stored ascontext.background. Sharp then receivesbackground: "undefined"and throws because it cannot parse"undefined"as a colour.Bug 2 — Sort comparator uses
localeCompareon a stringified numericorderThe sort converts
order: -1to the string"-1"and compares it against modifier names like"extend"usinglocaleCompare. Whether symbols/punctuation sort before or after letters is locale-dependent, soorder: -1modifiers (background,fit,position,quality) are not reliably applied before other modifiers.Fix
src/handlers/utils.ts— return the rawargumentstring as a fallback when JSON parsing fails. One line.src/ipx.ts— compareordervalues numerically (aOrder - bOrder) and fall back to name comparison only for equal-order handlers.src/handlers/handlers.ts— removes the stale// TODO: Support backgroundcomment fromflatten(background was already supported), and collapses single-property object literals to one line for consistency.Reproduction
With
b_030712:JSON.parse("030712")throws (leading zero → invalid JSON),VArgreturnsundefined,String(undefined)→"undefined", sharp crashes.Summary by CodeRabbit