Skip to content

docs: fix BNF grammar to distinguish prerelease from build identifiers#846

Merged
wraithgar merged 1 commit intonpm:mainfrom
abhu85:fix/bnf-prerelease-build-distinction
Feb 24, 2026
Merged

docs: fix BNF grammar to distinguish prerelease from build identifiers#846
wraithgar merged 1 commit intonpm:mainfrom
abhu85:fix/bnf-prerelease-build-distinction

Conversation

@abhu85
Copy link
Contributor

@abhu85 abhu85 commented Feb 24, 2026

Summary

The BNF grammar in README.md and range.bnf incorrectly defined pre and build as identical rules (parts), but they have different requirements per the SemVer 2.0.0 specification:

  • Prerelease identifiers: Numeric parts must not have leading zeros
  • Build metadata: Allows any alphanumeric string including leading zeros

Current behavior (correctly implemented, but BNF was wrong)

semver.valid('1.2.3-00')  // null (invalid - prerelease numeric with leading zero)
semver.valid('1.2.3+00')  // '1.2.3' (valid - build metadata allows any alphanumeric)
semver.valid('1.2.3-0')   // '1.2.3-0' (valid - single zero is fine)

Changes

Updates the BNF grammar to distinguish between:

  • prepart - uses nr (no leading zeros) or alphanumid (alphanumeric identifier)
  • buildid - any alphanumeric string including leading zeros

Also adds an explanatory note after the BNF block to clarify the distinction.

Fixes #665

Test plan

The BNF grammar incorrectly showed `pre` and `build` as identical rules,
but they have different requirements per SemVer 2.0.0 specification:

- Prerelease identifiers: numeric parts must not have leading zeros
  (e.g., `1.2.3-00` is invalid, `1.2.3-0` is valid)
- Build metadata: allows any alphanumeric string including leading zeros
  (e.g., `1.2.3+00` is valid)

This updates the BNF in both README.md and range.bnf to correctly reflect
the implementation behavior and SemVer spec.

Fixes npm#665

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abhu85 abhu85 requested a review from a team as a code owner February 24, 2026 11:40
@wraithgar
Copy link
Member

Had to go back and double check a few things there, just to be sure..

> semver.parse('1.2.0--pre').prerelease
[ '-pre' ]
> semver.parse('1.2.0+-build').build
[ '-build' ]

Yep, leading - is ok on those.

@wraithgar wraithgar merged commit 37776c3 into npm:main Feb 24, 2026
29 checks passed
@github-actions github-actions bot mentioned this pull request Feb 24, 2026
@abhu85 abhu85 deleted the fix/bnf-prerelease-build-distinction branch February 24, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] BNF does not match the implementation

2 participants