Fix duplicate singleton subtag being treated as valid - #26
Open
youdie006 wants to merge 1 commit into
Open
Conversation
Per RFC 5646 a singleton subtag must appear at most once (outside a private-use sequence), but the extensions loop in parse.js never checked for a repeated singleton, so `en-a-bbb-a-ccc` parsed as fully valid with no warning. Track the singletons seen while walking the extensions loop and return the existing fail(..., 7, 'Duplicate singleton, singletons must not be repeated') on a repeat, reusing the same warning-code convention the sibling spec violations (empty extension, superfluous content) already use. The check is scoped to extension singletons, so private-use duplicates and different singletons stay valid. Fixes wooorm#23.
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.
Fixes #23.
Problem
parse('en-a-bbb-a-ccc')returns a fully valid schema even though the singletonaappears twice. Per RFC 5646, each singleton subtag MUST appear at most once (other than in a private-use sequence), so this tag is invalid.Root cause
The extensions loop in
lib/parse.jspushes each extension without ever checking whether its one-character singleton was already used, so a tag with the same singleton twice parses as valid -- no warning, and the end-of-input check passes.Fix
Track the singletons seen while walking the extensions loop; on a repeat, return the existing
fail(..., 7, 'Duplicate singleton, singletons must not be repeated'). This reuses the samefail()/ warning-code convention the sibling spec violations already use (empty extension, superfluous content), and adds a warning -- matching the steer in #23 that "a warning is probably a good idea at least". Warning code 7 is added to the readme table.The check is scoped to extension singletons only, so cases that stay valid per RFC are unaffected and covered by regression tests:
en-a-bbb-x-a-ccc-- the secondais in a private-use sequence (valid)en-a-bbb-t-ccc-- two different singletons (valid)de-DE-u-co-phonebk-- a normal extension (valid)Verification
Duplicate singletontest fail (not ok); with the fix, all tests pass.npm run test-api: 10/10 pass.c8 --100: 100% across all files.tscbuild, 100% type-coverage, prettier, and xo all clean.This change was written with AI assistance and reviewed by me before submitting.