Skip to content

fix(yaml): quote leading-zero integer-shaped strings in stringify#7164

Open
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:yaml-leading-zero-int-quotes
Open

fix(yaml): quote leading-zero integer-shaped strings in stringify#7164
LeSingh1 wants to merge 1 commit into
denoland:mainfrom
LeSingh1:yaml-leading-zero-int-quotes

Conversation

@LeSingh1
Copy link
Copy Markdown

Fixes #7040.

resolveYamlInteger accepts "07" as octal so stringify already quoted it, but "08" and "09" fell through because they aren't valid octal:

import { stringify } from "jsr:@std/yaml";
stringify("07"); // "'07'\n"  ok
stringify("08"); // "08\n"    looks like a number
stringify("09"); // "09\n"    looks like a number

These still read as numbers to humans and to other YAML parsers, so they should be quoted too. The fix adds a /^[-+]?0[0-9]+$/ guard in chooseScalarStyle so any integer-shaped string with a leading zero forces single-quoted style. After the patch:

"08"   -> "'08'\n"
"09"   -> "'09'\n"
"089"  -> "'089'\n"
"-08"  -> "'-08'\n"
"0a8"  -> "0a8\n"   (unchanged, not integer-shaped)

Tests added in yaml/stringify_test.ts cover the issue cases plus edge cases (signed, mixed digits, non-integer-shaped strings).

resolveYamlInteger accepts "07" as octal so stringify already quoted
it, but "08" and "09" fell through because they aren't valid octal.
They still read as numbers to humans and to other YAML parsers though,
so they need to be quoted too.

Force-quote any string matching /^[-+]?0[0-9]+$/ to cover the
non-octal leading-zero cases ("08", "09", "089", "-012", ...).

Fixes denoland#7040
@github-actions github-actions Bot added the yaml label May 29, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7164      +/-   ##
==========================================
- Coverage   94.57%   94.57%   -0.01%     
==========================================
  Files         636      636              
  Lines       52142    52144       +2     
  Branches     9401     9402       +1     
==========================================
+ Hits        49315    49316       +1     
  Misses       2249     2249              
- Partials      578      579       +1     

☔ 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.

@std/yaml zero-started numeric strings stringify to numbers

1 participant