Skip to content

Commit

Permalink
Fix; raise error on missing title before changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwint committed Aug 4, 2022
1 parent dbd2af7 commit b25628e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Raise error on missing title before changes.
- Strip trailing whitespace from text.


Expand All @@ -32,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Raise error on wrong title level for changes.
- Raise error on wrong title level before changes.
- Raise error on duplicate version sections.
- Merge duplicate changes sections.

Expand All @@ -48,7 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Print output on success.
- Print message on success.

### Changed

Expand Down
15 changes: 10 additions & 5 deletions ocdc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,26 @@ def version(p: Parser) -> Tuple[ast.Version, Token]:
else:
v.changes[key] = changes_

detect_wrong_title_level(p, 3)
detect_wrong_title_level(p, 3, "before changes")

return v, title_token


def detect_wrong_title_level(p: Parser, expected: int) -> None:
def detect_wrong_title_level(p: Parser, expected: int, reason: str) -> None:
with p.checkpoint():
level = p.match_many(TokenType.HASH)
if level and level != expected:
if level != expected:
token = p.peek(1)
p.match({TokenType.TEXT})
skip_newlines(p)
if p.match({TokenType.DASH}):
msg = f"Expected a title of H{expected}, but found H{level}"
raise p.error(msg, token, back=level)
msg = f"Expected a title of H{expected} {reason}"
if level:
msg += f", but found H{level}"
raise p.error(msg, token, back=level)
else:
token = p.peek(1)
raise p.error(msg, token, back=len(token.text))
raise p.Rollback


Expand Down
2 changes: 1 addition & 1 deletion tests/data/wrong_title_level.err.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Expected a title of H3, but found H2 at line 14, column 1:
Expected a title of H3 before changes, but found H2 at line 14, column 1:

## Removed
^^
18 changes: 18 additions & 0 deletions tests/data/wrong_title_level_none.err.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## Unreleased

- Experimental stuff.


## 0.1.0 - 2022-08-04

### Added

- Something.
4 changes: 4 additions & 0 deletions tests/data/wrong_title_level_none.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Expected a title of H3 before changes at line 11, column 1:

- Experimental stuff.
^

0 comments on commit b25628e

Please sign in to comment.