Skip to content

Commit 37776c3

Browse files
abhu85claude
andauthored
docs: fix BNF grammar to distinguish prerelease from build identifiers (#846)
## 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](https://semver.org/#spec-item-9): - **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) ```js 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 - [x] Verified the implementation behavior matches the updated BNF - [x] Confirmed alignment with [SemVer 2.0.0 spec item 9](https://semver.org/#spec-item-9) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5993c2e commit 37776c3

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,19 @@ nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
399399
tilde ::= '~' partial
400400
caret ::= '^' partial
401401
qualifier ::= ( '-' pre )? ( '+' build )?
402-
pre ::= parts
403-
build ::= parts
404-
parts ::= part ( '.' part ) *
405-
part ::= nr | [-0-9A-Za-z]+
402+
pre ::= prepart ( '.' prepart ) *
403+
prepart ::= nr | alphanumid
404+
build ::= buildid ( '.' buildid ) *
405+
alphanumid ::= ( ['0'-'9'] ) * [-A-Za-z] [-0-9A-Za-z] *
406+
buildid ::= [-0-9A-Za-z]+
406407
```
407408

409+
Note: Prerelease identifiers (`pre`) use `nr` for numeric parts, which
410+
disallows leading zeros (e.g., `1.2.3-00` is invalid). Build metadata
411+
identifiers (`build`) allow any alphanumeric string including leading
412+
zeros (e.g., `1.2.3+00` is valid). This matches the
413+
[SemVer 2.0.0 specification](https://semver.org/#spec-item-9).
414+
408415
## Functions
409416

410417
All methods and classes take a final `options` object argument. All

range.bnf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ nr ::= '0' | [1-9] ( [0-9] ) *
1010
tilde ::= '~' partial
1111
caret ::= '^' partial
1212
qualifier ::= ( '-' pre )? ( '+' build )?
13-
pre ::= parts
14-
build ::= parts
15-
parts ::= part ( '.' part ) *
16-
part ::= nr | [-0-9A-Za-z]+
13+
pre ::= prepart ( '.' prepart ) *
14+
prepart ::= nr | alphanumid
15+
build ::= buildid ( '.' buildid ) *
16+
alphanumid ::= ( [0-9] ) * [A-Za-z-] [-0-9A-Za-z] *
17+
buildid ::= [-0-9A-Za-z]+

0 commit comments

Comments
 (0)