Skip to content

Commit e186923

Browse files
feat(parser)!: replace parser with syntax v0.1.0
Swap legacy lexer parser for syntax v0.1.0 behind commit.Commit, add diagnostic recovery and line/column positions, and expose NewWithParser / NewLinterWithParser.
1 parent d8290bf commit e186923

24 files changed

Lines changed: 955 additions & 165 deletions

‎.github/workflows/go.yml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: Go
22

33
on:
44
push:
5-
branches: ["master"]
65
pull_request:
76
branches: ["master"]
87
types:
@@ -15,19 +14,32 @@ on:
1514
- submitted
1615
workflow_dispatch:
1716

17+
permissions:
18+
contents: read
19+
1820
jobs:
1921
build:
2022
runs-on: ubuntu-latest
2123
steps:
2224
- uses: actions/checkout@v4
25+
with:
26+
persist-credentials: false
2327

2428
- name: Set up Go
2529
uses: actions/setup-go@v5
2630
with:
27-
go-version: "1.24.2"
31+
go-version: "1.26.x"
2832

2933
- name: Build
3034
run: go build -v ./...
3135

3236
- name: Test
33-
run: go test -v ./...
37+
run: go test -race -count=1 -timeout=2m ./...
38+
39+
- name: Static checks
40+
run: go vet ./...
41+
42+
- name: CLI integration checks
43+
run: |
44+
go build -o "$RUNNER_TEMP/commitlint" .
45+
bash test/integration_test.sh "$RUNNER_TEMP/commitlint"

‎.github/workflows/goreleaser.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Go
2020
uses: actions/setup-go@v5
2121
with:
22-
go-version: 1.24.2
22+
go-version: "1.26.x"
2323
- name: Run Tests
2424
run: go test ./... -count=1
2525
- name: Run GoReleaser

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,11 @@ type NoWIPRule struct{}
550550
551551
func (r *NoWIPRule) Name() string { return "no-wip" }
552552
func (r *NoWIPRule) Apply(setting lint.RuleSetting) error { return nil }
553-
func (r *NoWIPRule) Validate(commit lint.Commit) (*lint.Issue, error) {
553+
func (r *NoWIPRule) Validate(commit lint.Commit) (*lint.Issue, bool) {
554554
if len(commit.Description()) >= 3 && commit.Description()[:3] == "WIP" {
555-
return lint.NewIssue("description must not start with WIP"), nil
555+
return lint.NewIssue("description must not start with WIP"), false
556556
}
557-
return nil, nil
557+
return nil, true
558558
}
559559
560560
func main() {

‎commit/commit.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// Package commit provides core interfaces for parsed conventional commits.
2-
// It wraps the external parser package and is the single import point for it.
3-
// Both the lint and changelog packages depend on these interfaces.
2+
// Consumers depend on these interfaces independently of the backing parser.
43
package commit
54

6-
// Note represent a footer note
5+
// Note represents a footer note.
76
type Note interface {
87
Token() string
98
Value() string
109
}
1110

12-
// Commit represent a parsed conventional commit message
11+
// Commit represents a parsed conventional commit message.
1312
type Commit interface {
1413
Message() string
1514
Header() string
@@ -20,9 +19,16 @@ type Commit interface {
2019
Description() string
2120
Notes() []Note
2221
IsBreakingChange() bool
22+
23+
HasHeaderBang() bool
24+
HasBreakingFooter() bool
25+
BodyBlankLines() int
26+
FooterBlankLines() int
27+
NoteCount() int
28+
NoteAt(index int) Note
2329
}
2430

25-
// Parser parses a commit message into a Commit
31+
// Parser parses a commit message into a Commit.
2632
type Parser interface {
2733
Parse(msg string) (Commit, error)
2834
}

‎commit/parser.go‎

Lines changed: 97 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,119 @@
11
package commit
22

3-
import "github.com/conventionalcommit/parser"
3+
import (
4+
"errors"
45

5-
type defaultParser struct {
6-
p *parser.Parser
7-
}
6+
"github.com/conventionalcommit/syntax"
7+
)
8+
9+
type defaultParser struct{}
810

9-
// NewParser returns a new Parser that wraps the conventional commit parser
11+
// NewParser returns a stateless parser backed by syntax.
1012
func NewParser() Parser {
11-
return &defaultParser{
12-
p: parser.New(),
13-
}
13+
return &defaultParser{}
1414
}
1515

1616
func (p *defaultParser) Parse(input string) (Commit, error) {
17-
c, err := p.p.Parse(input)
17+
c, err := syntax.Parse(input)
1818
if err != nil {
1919
return nil, err
2020
}
21-
wrapC := &defaultCommit{
22-
Commit: c,
21+
return &defaultCommit{Commit: c}, nil
22+
}
23+
24+
// Inspect exposes recoverable fields alongside all syntax diagnostics.
25+
// A nil commit means structural recovery was unsafe.
26+
func Inspect(input string) (Commit, []error) {
27+
result := syntax.Inspect(input)
28+
var diagnostics []error
29+
if len(result.Diagnostics) > 0 {
30+
diagnostics = make([]error, len(result.Diagnostics))
31+
for i := range diagnostics {
32+
diagnostics[i] = &result.Diagnostics[i]
33+
}
34+
}
35+
if !result.Structured {
36+
return nil, diagnostics
2337
}
24-
return wrapC, nil
38+
return &defaultCommit{Commit: result.Commit}, diagnostics
2539
}
2640

2741
type defaultCommit struct {
28-
*parser.Commit
42+
syntax.Commit
2943
}
3044

31-
func (d *defaultCommit) Notes() []Note {
32-
outNotes := d.Commit.Notes()
33-
notes := make([]Note, len(outNotes))
45+
func (d *defaultCommit) BodyBlankLines() int {
46+
return d.BodyGap().BlankLines
47+
}
48+
49+
func (d *defaultCommit) FooterBlankLines() int {
50+
return d.FooterGap().BlankLines
51+
}
3452

35-
for i := 0; i < len(outNotes); i++ {
36-
notes[i] = &outNotes[i]
53+
func (d *defaultCommit) NoteCount() int {
54+
return d.FooterCount()
55+
}
56+
57+
func (d *defaultCommit) NoteAt(index int) Note {
58+
entry := d.FooterAt(index)
59+
return defaultNote{
60+
token: entry.Token(),
61+
value: entry.Value(),
3762
}
63+
}
3864

65+
func (d *defaultCommit) Notes() []Note {
66+
count := d.FooterCount()
67+
if count == 0 {
68+
return nil
69+
}
70+
// Keep one backing slice for all notes instead of allocating each note.
71+
values := make([]defaultNote, count)
72+
notes := make([]Note, count)
73+
for i := range values {
74+
entry := d.FooterAt(i)
75+
values[i] = defaultNote{
76+
token: entry.Token(),
77+
value: entry.Value(),
78+
}
79+
notes[i] = &values[i]
80+
}
3981
return notes
4082
}
83+
84+
type defaultNote struct {
85+
token string
86+
value string
87+
}
88+
89+
func (n defaultNote) Token() string { return n.token }
90+
func (n defaultNote) Value() string { return n.value }
91+
92+
// FirstLine returns the first line of msg without its LF or CRLF ending.
93+
func FirstLine(msg string) string {
94+
it := syntax.Lines(msg)
95+
line, _ := it.Next()
96+
return msg[line.Content.Start:line.Content.End]
97+
}
98+
99+
// ForEachLine iterates over lines in s without their LF or CRLF endings.
100+
// Iteration stops if fn returns false.
101+
func ForEachLine(s string, fn func(line string) bool) {
102+
it := syntax.Lines(s)
103+
for line, ok := it.Next(); ok; line, ok = it.Next() {
104+
if !fn(s[line.Content.Start:line.Content.End]) {
105+
return
106+
}
107+
}
108+
}
109+
110+
// DiagnosticPosition returns the 1-based line and column for err in msg when
111+
// err carries a byte offset, or false otherwise.
112+
func DiagnosticPosition(msg string, err error) (line, column int, ok bool) {
113+
var pe *syntax.ParseError
114+
if !errors.As(err, &pe) {
115+
return 0, 0, false
116+
}
117+
pos := syntax.Locate(msg, pe.Offset)
118+
return pos.Line, pos.Column, true
119+
}

‎commit/parser_test.go‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package commit_test
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
8+
"github.com/conventionalcommit/commitlint/commit"
9+
"github.com/conventionalcommit/syntax"
10+
)
11+
12+
func TestSyntaxAdapter(t *testing.T) {
13+
p := commit.NewParser()
14+
for _, newline := range []string{"\n", "\r\n"} {
15+
input := strings.ReplaceAll("feat(store)!: replace index \n\nRetain records.\n\nTrace: #91\n continuation\nRefs #92\nRefs #93\nBREAKING-CHANGE: remove legacy keys\n", "\n", newline)
16+
c, err := p.Parse(input)
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
if c.Message() != input || c.Header() != "feat(store)!: replace index " || c.Type() != "feat" || c.Scope() != "store" || c.Description() != "replace index " || c.Body() != "Retain records." || !c.IsBreakingChange() {
21+
t.Fatalf("unexpected fields: %#v", c)
22+
}
23+
want := [][2]string{{"Trace", "#91" + newline + " continuation"}, {"Refs", "92"}, {"Refs", "93"}, {"BREAKING-CHANGE", "remove legacy keys"}}
24+
notes := c.Notes()
25+
if len(notes) != len(want) {
26+
t.Fatalf("notes = %d", len(notes))
27+
}
28+
for i, entry := range want {
29+
if notes[i].Token() != entry[0] || notes[i].Value() != entry[1] {
30+
t.Fatalf("note %d = %q: %q", i, notes[i].Token(), notes[i].Value())
31+
}
32+
}
33+
notes[0] = nil
34+
if c.Notes()[0].Token() != "Trace" {
35+
t.Fatal("returned slice changed the result")
36+
}
37+
if !strings.HasPrefix(c.Footer(), "Trace: #91"+newline) {
38+
t.Fatalf("footer = %q", c.Footer())
39+
}
40+
}
41+
}
42+
43+
func TestSyntaxAdapterErrors(t *testing.T) {
44+
p := commit.NewParser()
45+
for _, tt := range []struct {
46+
input string
47+
code syntax.ErrorCode
48+
offset int
49+
}{
50+
{" feat: expose records", syntax.InvalidType, 0},
51+
{"feat:expose records", syntax.InvalidSeparator, 5},
52+
{"feat: expose records\nbody", syntax.InvalidSectionSeparator, 21},
53+
{"feat: expose records\n\nRefs #9\nEmpty: ", syntax.InvalidFooterValue, 37},
54+
} {
55+
c, err := p.Parse(tt.input)
56+
var pe *syntax.ParseError
57+
if c != nil || !errors.As(err, &pe) || pe.Code != tt.code || pe.Offset != tt.offset {
58+
t.Fatalf("input %q: result=%v error=%v", tt.input, c, err)
59+
}
60+
}
61+
}
62+
63+
func TestSyntaxAdapterWithoutNotes(t *testing.T) {
64+
c, err := commit.NewParser().Parse("feat: expose records\n")
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
if len(c.Notes()) != 0 || c.Message() != "feat: expose records\n" {
69+
t.Fatal("unexpected empty-footer result")
70+
}
71+
}
72+
73+
func TestSyntaxInspectionAdapter(t *testing.T) {
74+
p := commit.NewParser()
75+
input := "feat:expose records\nBody text."
76+
c, diagnostics := commit.Inspect(input)
77+
if c == nil || len(diagnostics) != 2 || c.Type() != "feat" || c.Body() != "Body text." {
78+
t.Fatalf("result=%v diagnostics=%v", c, diagnostics)
79+
}
80+
var pe *syntax.ParseError
81+
if !errors.As(diagnostics[0], &pe) || pe.Code != syntax.InvalidSeparator {
82+
t.Fatal("typed diagnostic lost")
83+
}
84+
if strict, err := p.Parse(input); strict != nil || err == nil {
85+
t.Fatal("strict parsing recovered invalid input")
86+
}
87+
if c, diagnostics := commit.Inspect("invalid header"); c != nil || len(diagnostics) != 1 {
88+
t.Fatal("ambiguous header recovered")
89+
}
90+
}
91+
92+
func TestCommitHelpers(t *testing.T) {
93+
parsed, err := commit.NewParser().Parse("feat!: drop v1\n\nBody text.\n\nBREAKING CHANGE: gone\nRefs #9")
94+
if err != nil {
95+
t.Fatal(err)
96+
}
97+
if !parsed.HasHeaderBang() || !parsed.HasBreakingFooter() || parsed.BodyBlankLines() != 1 || parsed.FooterBlankLines() != 1 {
98+
t.Fatal("syntax-backed Commit methods reported incorrect facts on valid breaking commit")
99+
}
100+
if parsed.NoteCount() != 2 || parsed.NoteAt(0).Token() != "BREAKING CHANGE" || parsed.NoteAt(1).Token() != "Refs" {
101+
t.Fatalf("unexpected notes: count=%d", parsed.NoteCount())
102+
}
103+
if got := commit.FirstLine("Initial commit\r\nignored"); got != "Initial commit" {
104+
t.Fatalf("FirstLine = %q", got)
105+
}
106+
var lines []string
107+
commit.ForEachLine("a\r\nb\nc", func(line string) bool {
108+
lines = append(lines, line)
109+
return len(lines) < 2
110+
})
111+
if len(lines) != 2 || lines[0] != "a" || lines[1] != "b" {
112+
t.Fatalf("ForEachLine = %q", lines)
113+
}
114+
_, diags := commit.Inspect("feat: ok\n\nRefs #9\nEmpty: ")
115+
if line, col, ok := commit.DiagnosticPosition("feat: ok\n\nRefs #9\nEmpty: ", diags[0]); !ok || line != 4 || col != 8 {
116+
t.Fatalf("DiagnosticPosition = (%d, %d, %v)", line, col, ok)
117+
}
118+
if _, _, ok := commit.DiagnosticPosition("feat: ok", errors.New("custom error")); ok {
119+
t.Fatal("expected false for non-syntax error")
120+
}
121+
}
122+
123+
var parsed commit.Commit
124+
var parseErr error
125+
var parsedNotes []commit.Note
126+
127+
func BenchmarkSyntaxAdapter(b *testing.B) {
128+
p := commit.NewParser()
129+
for _, tt := range []struct {
130+
name, input string
131+
notes bool
132+
}{
133+
{"header", "feat: expose records", false},
134+
{"footers", "feat: expose records\n\nRefs #9\nTrace: #91", false},
135+
{"footers_with_notes", "feat: expose records\n\nRefs #9\nTrace: #91", true},
136+
} {
137+
b.Run(tt.name, func(b *testing.B) {
138+
b.ReportAllocs()
139+
for i := 0; i < b.N; i++ {
140+
parsed, parseErr = p.Parse(tt.input)
141+
if tt.notes && parseErr == nil {
142+
parsedNotes = parsed.Notes()
143+
}
144+
}
145+
if parseErr != nil {
146+
b.Fatal(parseErr)
147+
}
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)