Skip to content

fix(diff): handle Symbol values in direct() without throwing#254

Open
terminalchai wants to merge 1 commit into
lukeed:masterfrom
terminalchai:fix/diff-symbol-comparison
Open

fix(diff): handle Symbol values in direct() without throwing#254
terminalchai wants to merge 1 commit into
lukeed:masterfrom
terminalchai:fix/diff-symbol-comparison

Conversation

@terminalchai
Copy link
Copy Markdown

Problem

Comparing a Symbol to any other type (e.g. a number) via assert.is or assert.equal causes an uncaught TypeError instead of a readable diff failure:

TypeError: Cannot convert a Symbol value to a string
    at direct (uvu/diff/index.mjs:136:29)
    at compare (uvu/diff/index.mjs:218:9)
    at assert (uvu/assert/index.mjs:32:28)

The crash originates in direct() which calls input += ' '.repeat(...) + [${typeA}] — but JS throws when trying to coerce a Symbol via +=.

Fixes #251

Root cause

direct() only coerced with String() in the default parameter expressions (to compute the initial lengths), but kept the raw values afterward. The crash surfaced the moment code tried to do symbolValue += someString.

Fix

Capture typeof before coercing (so the type-mismatch display still shows [symbol] / [number] correctly), then immediately coerce both input and expect to strings via String():

 export function direct(input, expect, lenA = String(input).length, lenB = String(expect).length) {
+  // Capture original types before coercion so that type-mismatch display works.
+  let typeA=typeof input, typeB=typeof expect;
+  // Coerce to strings up-front so that Symbols (and other non-string primitives
+  // that throw on implicit + coercion) do not cause an uncaught TypeError.
+  input = String(input);
+  expect = String(expect);
   let gutter = 4;
   let lenC = Math.max(lenA, lenB);
-  let typeA=typeof input, typeB=typeof expect;

Changes

  • src/diff.js - 4-line guard at the top of direct()
  • test/diff.js - 2 new direct snapshot cases + 1 compare case for Symbol vs number

Verification

Manually verified against actual outputs (test infra uses legacy esm pkg incompatible with Node 22 - a pre-existing issue unrelated to this fix):

PASS: direct(Symbol('foo'), 42) does not throw
PASS: direct(42, Symbol('foo')) does not throw
PASS: compare(Symbol('foo'), 42) does not throw
PASS: direct(123, 456) still works
PASS: direct(123, '123') still shows type mismatch
5 passed, 0 failed

Previously, passing a Symbol as either argument to direct() (which is
reached via assert.is / assert.equal through compare()) triggered an
uncaught TypeError:

  'Cannot convert a Symbol value to a string'

This happened because direct() relied on implicit string coercion via
+= after calling encodeURI, but Symbols cannot be implicitly coerced.

Fix: capture typeof before coercing, then immediately coerce both input
and expect to strings via String() so that the type-mismatch annotation
still displays the original types while all subsequent string operations
are safe.

Fixes lukeed#251
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.

BUG: Comparison between Symbol and number causes uncaught exception

1 participant