fix(toml): handle Infinity/NaN and Date inside inline arrays#7171
Open
LeSingh1 wants to merge 1 commit into
Open
fix(toml): handle Infinity/NaN and Date inside inline arrays#7171LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
Inline array stringification used JSON.stringify, which silently turned
Infinity/-Infinity/NaN into 'null' and wrapped Date values in extra
quotes. Both forms broke round-trip through parse():
stringify({x: [Infinity, -Infinity, NaN]}) // x = [null,null,null]
stringify({x: [new Date(0)]}) // x = ["1970-01-01..."]
The same flaw affected #printAsInlineValue, which was the path for
mixed-type arrays — Date values were quoted and non-finite numbers
fell through as String(Infinity) / String(NaN).
Replace the JSON.stringify call in #arrayDeclaration with a per-element
walk through #printAsInlineValue, and teach #printAsInlineValue to emit
TOML's inf / -inf / nan keywords for non-finite numbers and to leave
Date values unquoted to match TOML's datetime literal syntax.
The 'handles mixed array' test pinned the buggy
date = "2022-05-13T00:00:00.000" form inside a nested inline map.
Updated to the correct unquoted form. Four new tests pin the issue's
specific repros (primitive Infinity/-Infinity/NaN, primitive Date,
mixed inf/nan/object, mixed date/object).
Fixes denoland#7162 (partially — the null-in-array cases are left for a
follow-up since the spec disallows null in TOML and the right behavior
is a separate design call).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7171 +/- ##
=======================================
Coverage 94.57% 94.57%
=======================================
Files 636 636
Lines 52142 52146 +4
Branches 9401 9408 +7
=======================================
+ Hits 49315 49319 +4
Misses 2249 2249
Partials 578 578 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Partial fix for #7162.
#arrayDeclarationrendered primitive arrays by callingJSON.stringify(value), which silently turnedInfinity/-Infinity/NaNinto the literalnulland wrappedDatevalues in extra quotes. Both forms broke round-trip throughparse():The same flaw affected
#printAsInlineValue, which is the path for mixed-type arrays —Datevalues were quoted there too, and non-finite numbers fell through asString(Infinity)/String(NaN)(not valid TOML).The fix is two small edits:
#arrayDeclarationno longer callsJSON.stringify. It maps the array through#printAsInlineValueso primitive arrays use the same TOML literal forms as mixed-type arrays.#printAsInlineValueemitsinf/-inf/nanfor non-finite numbers and leavesDatevalues unquoted, matching TOML's datetime literal syntax.After:
The existing
handles mixed arraytest pinned the buggydate = \"2022-05-13T00:00:00.000\"form inside a nested inline map. Updated to the correct unquoted form (and round-trip verified —parse(stringify(...))returns aDateinstance again instead of a string).Four new tests cover the issue's specific repros.
Not included in this PR:
{x: [null]}/{x: [1, null]}— TOML disallowsnull, and the right behavior (skip vs. throw with a clearer message vs. coerce) is a design call worth its own discussion. Leaving for a follow-up.Z) —#printDatedrops the offset suffix in all TOML output, not just arrays. Restoring it is a broader change touching every Date round-trip and would balloon this PR. Happy to file separately.