Skip to content

Commit 483bc22

Browse files
committed
refactor: two small pass-2 review cleanups
Pass 2 of code review on PR #732 found two more instances of patterns pass 1 had just fixed elsewhere: - internal/rules/tokenbudget/rule.go: Check() still inlined its own copy of the TokensPerWord-fallback resolution that effectiveTokensPerWord() (pass 1) was extracted specifically to de-duplicate from tokenCount/modeLabel/definitelyUnderBudget — a 4th, un-consolidated copy. Now calls the shared helper too. - pkg/markdown/flavor/detect.go: restoring lineStartOf's negative-offset clamp (pass 1) copied the same clamp logic LineCol already had a few lines below instead of sharing it. Extracted clampOffset, used by both.
1 parent 5c167de commit 483bc22

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

internal/rules/tokenbudget/rule.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
8181
modeLabel := r.modeLabel()
8282

8383
overage := count - budget
84-
tpw := r.TokensPerWord
85-
if tpw <= 0 {
86-
tpw = defaultTokensPerWord
87-
}
88-
wordsOver := int(math.Ceil(float64(overage) / tpw))
84+
wordsOver := int(math.Ceil(float64(overage) / r.effectiveTokensPerWord()))
8985
if wordsOver < 1 {
9086
wordsOver = 1
9187
}
@@ -149,8 +145,8 @@ func (r *Rule) definitelyUnderBudget(source []byte, budget int) bool {
149145
}
150146

151147
// effectiveTokensPerWord returns r.TokensPerWord, falling back to
152-
// defaultTokensPerWord when unset — the same resolution definitelyUnderBudget,
153-
// tokenCount, and modeLabel all need for heuristic mode.
148+
// defaultTokensPerWord when unset — the same resolution Check,
149+
// definitelyUnderBudget, tokenCount, and modeLabel all need.
154150
func (r *Rule) effectiveTokensPerWord() float64 {
155151
tpw := r.TokensPerWord
156152
if tpw <= 0 {

pkg/markdown/flavor/detect.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,21 @@ func nodeByteRange(n ast.Node) (int, int) {
338338
}
339339

340340
func lineStartOf(source []byte, offset int) int {
341+
offset = clampOffset(source, offset)
342+
return bytes.LastIndexByte(source[:offset], '\n') + 1
343+
}
344+
345+
// clampOffset bounds offset to [0, len(source)] so a caller that
346+
// looks at byte -1 or one past EOF still gets a valid index to slice
347+
// or index with. Shared by lineStartOf and LineCol.
348+
func clampOffset(source []byte, offset int) int {
341349
if offset < 0 {
342-
offset = 0
350+
return 0
343351
}
344352
if offset > len(source) {
345-
offset = len(source)
353+
return len(source)
346354
}
347-
return bytes.LastIndexByte(source[:offset], '\n') + 1
355+
return offset
348356
}
349357

350358
// firstTextStart returns the byte offset of the first descendant Text
@@ -388,12 +396,7 @@ func isASCIISpace(b byte) bool {
388396
// line numbers when producing line-level edits; also used internally
389397
// by every block / inline / makeFinding helper.
390398
func LineCol(source []byte, offset int) (line, col int) {
391-
if offset < 0 {
392-
offset = 0
393-
}
394-
if offset > len(source) {
395-
offset = len(source)
396-
}
399+
offset = clampOffset(source, offset)
397400
// bytes.Count and bytes.LastIndexByte are SIMD-accelerated on
398401
// amd64; a manual byte-by-byte loop over the same prefix does not
399402
// vectorize. See docs/development/high-performance-go.md

0 commit comments

Comments
 (0)