Skip to content

fix(toml): handle Infinity/NaN and Date inside inline arrays#7171

Open
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:toml-inline-array-edges
Open

fix(toml): handle Infinity/NaN and Date inside inline arrays#7171
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:toml-inline-array-edges

Conversation

@LeSingh1
Copy link
Copy Markdown

Partial fix for #7162.

#arrayDeclaration rendered primitive arrays by calling JSON.stringify(value), which silently turned Infinity / -Infinity / NaN into the literal null and wrapped Date values in extra quotes. Both forms broke round-trip through parse():

toml.stringify({ x: [Infinity, -Infinity, NaN] });
// x = [null,null,null]   (was supposed to be x = [inf,-inf,nan])

toml.stringify({ x: [new Date(0)] });
// x = ["1970-01-01T00:00:00.000"]   (reparsed as a string, not a Date)

The same flaw affected #printAsInlineValue, which is the path for mixed-type arrays — Date values were quoted there too, and non-finite numbers fell through as String(Infinity) / String(NaN) (not valid TOML).

The fix is two small edits:

  1. #arrayDeclaration no longer calls JSON.stringify. It maps the array through #printAsInlineValue so primitive arrays use the same TOML literal forms as mixed-type arrays.
  2. #printAsInlineValue emits inf / -inf / nan for non-finite numbers and leaves Date values unquoted, matching TOML's datetime literal syntax.

After:

x = [inf,-inf,nan]
x = [1970-01-01T00:00:00.000]
x = [inf,-inf,nan,{}]
x = [1970-01-01T00:00:00.000,{}]

The existing 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 (and round-trip verified — parse(stringify(...)) returns a Date instance 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 disallows null, 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.
  • Datetime offset (the trailing Z) — #printDate drops 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.

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).
@github-actions github-actions Bot added the toml label May 30, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.57%. Comparing base (cdf74a8) to head (3de36e2).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant