Skip to content

Commit dcabe7c

Browse files
committed
refactor(astutil): remove two more CountLeadingSpaces duplicates
listscan.leadingSpaces and requiredstructure.lineIndent were identical copies of astutil.CountLeadingSpaces (body: len-TrimLeft with space-only cutset) missed by the initial consolidation. Replace both with the canonical helper. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gj8AvSb7r12iqa7r3vYYof
1 parent 02800ef commit dcabe7c

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

internal/rules/listscan/listscan.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
// (already in internal/lint/layer0.go) into this parser.
2828
package listscan
2929

30-
import "bytes"
30+
import (
31+
"bytes"
32+
33+
"github.com/jeduden/mdsmith/internal/rules/astutil"
34+
)
3135

3236
// Item is one parsed list item.
3337
type Item struct {
@@ -180,7 +184,7 @@ func (p *parser) run() {
180184
// new list item, or a continuation line.
181185
func (p *parser) scanLine(i int, line []byte) int {
182186
lineNo := i + 1
183-
indent := leadingSpaces(line)
187+
indent := astutil.CountLeadingSpaces(line)
184188
markerToken := hasMarkerToken(line, indent)
185189
interrupts := interruptsParagraph(line, indent)
186190

@@ -672,10 +676,6 @@ func contentColumn(line []byte, afterMarker int) int {
672676
return afterMarker + spaces
673677
}
674678

675-
func leadingSpaces(line []byte) int {
676-
return len(line) - len(bytes.TrimLeft(line, " "))
677-
}
678-
679679
func isBlankLine(line []byte) bool {
680680
for _, c := range line {
681681
if c != ' ' && c != '\t' && c != '\r' {
@@ -735,7 +735,7 @@ func openingFenceRel(line []byte, indent, baseCol int) (fenceInfo, bool) {
735735
// fence run sits no more than 3 columns past fi.baseCol, runs >= fi.length
736736
// identical fence characters, and is followed only by whitespace.
737737
func closingFence(line []byte, fi fenceInfo) bool {
738-
indent := leadingSpaces(line)
738+
indent := astutil.CountLeadingSpaces(line)
739739
if indent-fi.baseCol >= 4 {
740740
return false
741741
}
@@ -752,7 +752,7 @@ func closingFence(line []byte, fi fenceInfo) bool {
752752
// isThematicBreak reports whether line is a thematic break (3+ of a
753753
// single -, *, or _ with only spaces between), which is not a list item.
754754
func isThematicBreak(line []byte) bool {
755-
indent := leadingSpaces(line)
755+
indent := astutil.CountLeadingSpaces(line)
756756
if indent >= 4 || indent >= len(line) {
757757
return false
758758
}

internal/rules/requiredstructure/rule.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jeduden/mdsmith/internal/piparser"
2222
"github.com/jeduden/mdsmith/internal/placeholders"
2323
"github.com/jeduden/mdsmith/internal/rule"
24+
"github.com/jeduden/mdsmith/internal/rules/astutil"
2425
rulesettings "github.com/jeduden/mdsmith/internal/rules/settings"
2526
"github.com/jeduden/mdsmith/internal/schema"
2627
"github.com/jeduden/mdsmith/internal/yamlutil"
@@ -1437,7 +1438,7 @@ func fenceOpenRun(raw, lineB []byte) (byte, int) {
14371438
if len(lineB) == 0 || (lineB[0] != '`' && lineB[0] != '~') {
14381439
return 0, 0
14391440
}
1440-
if lineIndent(raw) > 3 {
1441+
if astutil.CountLeadingSpaces(raw) > 3 {
14411442
return 0, 0
14421443
}
14431444
n := fenceRun(lineB, lineB[0])
@@ -1452,7 +1453,7 @@ func fenceOpenRun(raw, lineB []byte) (byte, int) {
14521453
// opener, nothing but the run on the trimmed line, and at most three
14531454
// spaces of indentation.
14541455
func fenceClose(raw, lineB []byte, ch byte, openLen int) bool {
1455-
if lineIndent(raw) > 3 {
1456+
if astutil.CountLeadingSpaces(raw) > 3 {
14561457
return false
14571458
}
14581459
n := fenceRun(lineB, ch)
@@ -1468,18 +1469,13 @@ func fenceRun(line []byte, ch byte) int {
14681469
return n
14691470
}
14701471

1471-
// lineIndent returns the number of leading spaces on the raw line.
1472-
func lineIndent(raw []byte) int {
1473-
return len(raw) - len(bytes.TrimLeft(raw, " "))
1474-
}
1475-
14761472
// isPIOpenLine reports whether a raw body line opens a processing
14771473
// instruction, mirroring the block parser in pkg/markdown: at most
14781474
// three spaces of indentation, a `<?` opener, and a non-empty name
14791475
// (the bytes up to the first whitespace or `?>`). An indented code
14801476
// example showing a directive is therefore not mistaken for one.
14811477
func isPIOpenLine(raw []byte) bool {
1482-
if lineIndent(raw) > 3 {
1478+
if astutil.CountLeadingSpaces(raw) > 3 {
14831479
return false
14841480
}
14851481
trimmed := bytes.TrimLeft(raw, " ")

0 commit comments

Comments
 (0)