Skip to content

Commit

Permalink
feat(parser): handle incomplete scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
SKalt committed Aug 10, 2024
1 parent 73ddab7 commit c72bb94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions pkg/parser/combinators.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// of a pointer wouldn't save space.

type Result struct {
// The Results of each off a the child parsers
Children []Result
Type string
Value string
Expand Down Expand Up @@ -81,6 +82,7 @@ func Marked(mark string) func(Parser) Parser {
}
}

// Note that `Opt` never returns an error.
func Opt(parser Parser) Parser {
return func(input []rune) (*Result, error) {
result, err := parser(input)
Expand Down Expand Up @@ -185,6 +187,7 @@ func Some(parsers ...Parser) Parser {
}
}

// Matches the end of the input
func Empty(input []rune) (*Result, error) {
if len(input) == 0 {
return nil, nil
Expand Down
30 changes: 22 additions & 8 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (cc *CC) ToString() string {
var Newline = Marked("Newline")(Any(LiteralRune('\n'), Tag("\r\n")))

var DoubleNewline = Sequence(Newline, Newline)
var ColonSep = Tag(": ")
var ColonSep = Regex(": ?") // accept a colon with or without a space after it

// The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.

Expand All @@ -90,14 +90,15 @@ var ColonSep = Tag(": ")
// A description MUST immediately follow the colon and space after the type/scope prefix. The description is a short summary of the code changes, e.g., fix: array parsing issue when multiple spaces were contained in string.

var CommitType Parser = Marked("CommitType")(
TakeUntil(Any(BreakingChangeBang, Tag(":"), Tag("("), Empty)),
TakeUntil(Any(BreakingChangeBang, Tag(":"), Tag("("), Newline, Empty)),
)

// A scope MAY be provided after a type. A scope MUST consist of a noun describing a section of the codebase surrounded by parenthesis, e.g., fix(parser):
var Scope Parser = Marked("Scope")(Delimited(Tag("("), TakeUntil(Tag(")")), Tag(")")))
var BreakingChangeBang Parser = Marked("BreakingChangeBang")(Tag("!"))
var ShortDescription Parser = Marked("Description")(TakeUntil(Any(Empty, Newline)))

// The bit before the description, e.g. "feat", "fix(scope)", "refactor!", etc.
var Context = Sequence(CommitType, Opt(Scope), Opt(BreakingChangeBang))

var BreakingChange = Any(Tag("BREAKING CHANGE"), Tag("BREAKING-CHANGE"))
Expand All @@ -114,18 +115,31 @@ var Footer = Marked("Footer")(
)
var Footers = Marked("Footers")(Many0(Footer))

var asMuchOfScopeAsPossible = Marked("Scope")(
Delimited(
Tag("("),
TakeUntil(Any(Tag(")"), Empty, Newline, Tag(":"), Tag("!"))),
Opt(Tag(")")),
),
)

var asMuchOfCCAsPossible = Some(
CommitType, Opt(asMuchOfScopeAsPossible), Opt(BreakingChangeBang), ColonSep, ShortDescription,
Opt(Newline), Opt(Newline),
Opt(Body),
Opt(Footers),
)

func ParseAsMuchOfCCAsPossible(fullCommit string) (*CC, error) {
parsed, err := Some(
CommitType, Opt(Scope), Opt(BreakingChangeBang), ColonSep, ShortDescription,
Opt(Newline), Opt(Newline),
Opt(Body),
Opt(Footers),
)([]rune(fullCommit))
parsed, err := asMuchOfCCAsPossible([]rune(fullCommit))
result := &CC{}
if parsed != nil && parsed.Children != nil {
for _, token := range parsed.Children {
result = result.Ingest(token)
}
}
if parsed.Remaining != nil {
result.Body += string(parsed.Remaining)
}
return result, err
}
10 changes: 6 additions & 4 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestParsingFullCommit(t *testing.T) {
t.FailNow()
}
if actual.Scope != expected.Scope {
fmt.Printf("Scope: expected: %+v actual: %+v\n", expected.Type, actual.Type)
fmt.Printf("Scope: expected: %+v actual: %+v\n", expected.Scope, actual.Scope)
t.FailNow()
}
if actual.BreakingChange != expected.BreakingChange {
Expand Down Expand Up @@ -278,7 +278,9 @@ func TestParsingPartialCommit(t *testing.T) {
}
}
}
t.Run("", test("feat", CC{Type: "feat"}))
t.Run("", test("feat:", CC{Type: "feat"}))
t.Run("", test("feat: ", CC{Type: "feat"}))
t.Run("bare `type`", test("feat", CC{Type: "feat"}))
t.Run("valid `type:`", test("feat:", CC{Type: "feat"}))
t.Run("valid `type: `", test("feat: ", CC{Type: "feat"}))

t.Run("invalid `type\nbody`", test("feat\nbody", CC{Type: "feat", Body: "\nbody"}))
}

0 comments on commit c72bb94

Please sign in to comment.