Skip to content

Commit 32b7360

Browse files
Fix lint issues
1 parent df42bdf commit 32b7360

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

.golangci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ linters:
189189
# G115 checks for use of truncating conversions.
190190
path: private/buf/buflsp/semantic_tokens.go
191191
text: "G115:"
192-
- linters:
193-
- gosec
194-
# G115 checks for use of truncating conversions.
195-
path: private/buf/buflsp/semantic_tokens_cel.go
196-
text: "G115:"
197192
- linters:
198193
- gosec
199194
# G115 checks for use of truncating conversions.

private/buf/buflsp/cel.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ func fileByteOffsetToCELOffset(fileByteOffset int, exprLiteralSpan source.Span)
193193
return -1
194194
}
195195

196+
// celLocByteOffset converts a CEL source location (line, col) to a byte offset
197+
// within exprString. line and col come from common.Location, which returns int,
198+
// but ComputeOffset requires int32; the conversion is safe for any realistic
199+
// CEL expression length.
200+
func celLocByteOffset(line, col int, sourceInfo *celast.SourceInfo, exprString string) int {
201+
runeOffset := int32(col) + sourceInfo.ComputeOffset(int32(line), 0) //nolint:gosec // CEL expressions are never large enough to overflow int32
202+
return celRuneOffsetToByteOffset(exprString, runeOffset)
203+
}
204+
196205
// celRuneOffsetToByteOffset converts a CEL source position (Unicode code point offset)
197206
// to a UTF-8 byte offset within the expression string.
198207
//

private/buf/buflsp/hover_cel.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ func findMacroAtOffset(sourceInfo *ast.SourceInfo, offset int, exprString string
191191
if startLoc.Line() <= 0 {
192192
continue
193193
}
194-
celRuneOffset := int32(startLoc.Column()) + sourceInfo.ComputeOffset(int32(startLoc.Line()), 0)
195194

196195
// For method-style macros like this.all(...), find the method name after the dot
197196
// For standalone macros like has(...), find the function name before the paren
@@ -203,13 +202,13 @@ func findMacroAtOffset(sourceInfo *ast.SourceInfo, offset int, exprString string
203202
targetID := call.Target().ID()
204203
targetLoc := sourceInfo.GetStartLocation(targetID)
205204
if targetLoc.Line() > 0 {
206-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
205+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
207206
funcStart, funcEnd = findMethodNameAfterDot(targetByteOffset, funcName, exprString)
208207
found = funcStart >= 0
209208
}
210209
} else {
211210
// Standalone function call - CEL position points to opening paren, look backwards
212-
celByteOffset := celRuneOffsetToByteOffset(exprString, celRuneOffset)
211+
celByteOffset := celLocByteOffset(startLoc.Line(), startLoc.Column(), sourceInfo, exprString)
213212
funcStart, funcEnd, found = findStandaloneFunctionName(celByteOffset, funcName, exprString)
214213
}
215214

@@ -249,7 +248,7 @@ func walkCELExprForHover(
249248
if startLoc.Line() <= 0 {
250249
return nil
251250
}
252-
celByteOffset := celRuneOffsetToByteOffset(exprString, int32(startLoc.Column())+sourceInfo.ComputeOffset(int32(startLoc.Line()), 0))
251+
celByteOffset := celLocByteOffset(startLoc.Line(), startLoc.Column(), sourceInfo, exprString)
253252

254253
// Variable to hold the best match (deepest expression containing offset)
255254
var bestMatch *celHoverInfo
@@ -306,7 +305,7 @@ func walkCELExprForHover(
306305
if sel.Operand() != nil {
307306
targetLoc := sourceInfo.GetStartLocation(sel.Operand().ID())
308307
if targetLoc.Line() > 0 {
309-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
308+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
310309
fieldStart, fieldEnd := findMethodNameAfterDot(targetByteOffset, sel.FieldName(), exprString)
311310
if fieldStart >= 0 && offset >= fieldStart && offset < fieldEnd {
312311
// Cursor is on the field name
@@ -383,7 +382,7 @@ func walkCELExprForHover(
383382
// Method call - search for the function name after the target
384383
targetLoc := sourceInfo.GetStartLocation(call.Target().ID())
385384
if targetLoc.Line() > 0 {
386-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
385+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
387386
funcStart, funcEnd = findMethodNameAfterDot(targetByteOffset, funcName, exprString)
388387
found = funcStart >= 0
389388
}

private/buf/buflsp/semantic_tokens_cel.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,24 +221,22 @@ func collectMacroTokens(
221221
if startLoc.Line() <= 0 {
222222
continue
223223
}
224-
celRuneOffset := int32(startLoc.Column()) + sourceInfo.ComputeOffset(int32(startLoc.Line()), 0)
225-
226224
// For method-style macros like this.all(...), we need to find the method name
227225
// For standalone macros like has(...), we need to find the function name
228226
if call.IsMemberFunction() {
229227
// Method call - search for ".funcName" after the target
230228
targetID := call.Target().ID()
231229
targetLoc := sourceInfo.GetStartLocation(targetID)
232230
if targetLoc.Line() > 0 {
233-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
231+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
234232
tokenSpan := findNameAfterDot(targetByteOffset, funcName, exprString, exprLiteralSpan)
235233
if !tokenSpan.IsZero() {
236234
collectToken(tokenSpan, semanticTypeMacro, 0, keyword.Unknown)
237235
}
238236
}
239237
} else {
240238
// Standalone function call - CEL position points to opening paren, look backwards
241-
celByteOffset := celRuneOffsetToByteOffset(exprString, celRuneOffset)
239+
celByteOffset := celLocByteOffset(startLoc.Line(), startLoc.Column(), sourceInfo, exprString)
242240
funcStart := celByteOffset - len(funcName)
243241
funcEnd := funcStart + len(funcName)
244242
if funcStart >= 0 && funcEnd <= len(exprString) {
@@ -273,7 +271,7 @@ func walkCELExprWithVars(
273271
if startLoc.Line() <= 0 {
274272
return
275273
}
276-
celByteOffset := celRuneOffsetToByteOffset(exprString, int32(startLoc.Column())+sourceInfo.ComputeOffset(int32(startLoc.Line()), 0))
274+
celByteOffset := celLocByteOffset(startLoc.Line(), startLoc.Column(), sourceInfo, exprString)
277275

278276
var tokenSpan source.Span
279277

@@ -318,7 +316,7 @@ func walkCELExprWithVars(
318316
if sel.Operand() != nil {
319317
targetLoc := sourceInfo.GetStartLocation(sel.Operand().ID())
320318
if targetLoc.Line() > 0 {
321-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
319+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
322320
tokenSpan = findNameAfterDot(targetByteOffset, sel.FieldName(), exprString, exprLiteralSpan)
323321
if !tokenSpan.IsZero() {
324322
collectToken(tokenSpan, semanticTypeProperty, 0, keyword.Unknown)
@@ -373,7 +371,7 @@ func walkCELExprWithVars(
373371
// Method call - search for the function name after the target
374372
targetLoc := sourceInfo.GetStartLocation(call.Target().ID())
375373
if targetLoc.Line() > 0 {
376-
targetByteOffset := celRuneOffsetToByteOffset(exprString, int32(targetLoc.Column())+sourceInfo.ComputeOffset(int32(targetLoc.Line()), 0))
374+
targetByteOffset := celLocByteOffset(targetLoc.Line(), targetLoc.Column(), sourceInfo, exprString)
377375
tokenSpan = findNameAfterDot(targetByteOffset, funcName, exprString, exprLiteralSpan)
378376
if !tokenSpan.IsZero() {
379377
collectToken(tokenSpan, tokenType, tokenModifier, keyword.Unknown)

0 commit comments

Comments
 (0)