fix(diff): handle Symbol values in direct() without throwing#254
Open
terminalchai wants to merge 1 commit into
Open
fix(diff): handle Symbol values in direct() without throwing#254terminalchai wants to merge 1 commit into
terminalchai wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Comparing a
Symbolto any other type (e.g. a number) viaassert.isorassert.equalcauses an uncaughtTypeErrorinstead of a readable diff failure:The crash originates in
direct()which callsinput += ' '.repeat(...) + [${typeA}]— but JS throws when trying to coerce aSymbolvia+=.Fixes #251
Root cause
direct()only coerced withString()in the default parameter expressions (to compute the initial lengths), but kept the raw values afterward. The crash surfaced the moment code tried to dosymbolValue += someString.Fix
Capture
typeofbefore coercing (so the type-mismatch display still shows[symbol]/[number]correctly), then immediately coerce bothinputandexpectto strings viaString():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 ofdirect()test/diff.js- 2 newdirectsnapshot cases + 1comparecase for Symbol vs numberVerification
Manually verified against actual outputs (test infra uses legacy
esmpkg incompatible with Node 22 - a pre-existing issue unrelated to this fix):