@@ -222,6 +222,10 @@ func DefaultConfig() Configuration {
222
222
223
223
// Result represents the result of one error.
224
224
type Result struct {
225
+ ReportAt token.Pos
226
+ FixRangeStart token.Pos
227
+ FixRangeEnd token.Pos
228
+
225
229
Node ast.Node
226
230
FixNode ast.Node
227
231
Reason string
@@ -416,20 +420,20 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
416
420
// it was and use *that* statement's position
417
421
if p .config .ForceExclusiveShortDeclarations && cuddledWithLastStmt {
418
422
if p .isShortDecl (stmt ) && ! p .isShortDecl (previousStatement ) {
419
- p .addError (
420
- stmt ,
421
- nil ,
423
+ p .addErrorRange (
424
+ stmt .Pos (),
425
+ stmt .End (),
426
+ stmt .End (),
422
427
reasonShortDeclNotExclusive ,
423
428
WhitespaceShouldAddAfter ,
424
- false ,
425
429
)
426
430
} else if p .isShortDecl (previousStatement ) && ! p .isShortDecl (stmt ) {
427
- p .addError (
428
- previousStatement ,
429
- nil ,
431
+ p .addErrorRange (
432
+ previousStatement .Pos (),
433
+ previousStatement .Pos (),
434
+ previousStatement .Pos (),
430
435
reasonShortDeclNotExclusive ,
431
436
WhitespaceShouldAddBefore ,
432
- false ,
433
437
)
434
438
}
435
439
}
@@ -449,7 +453,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
449
453
// If the variable on the line above is allowed to be
450
454
// cuddled, break two lines above so we keep the proper
451
455
// cuddling.
452
- p .addWhitespaceBeforeFixOtherNodeError (n1 , n2 , reason )
456
+ p .addErrorRange (n1 . Pos () , n2 . Pos (), n2 . Pos (), reason , WhitespaceShouldAddBefore )
453
457
} else {
454
458
// If not, break here so we separate the cuddled variable.
455
459
p .addWhitespaceBeforeError (n1 , reason )
@@ -1140,7 +1144,15 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
1140
1144
// And now if the first statement is passed the number of allowed lines,
1141
1145
// then we had extra WS, possibly before the first comment group.
1142
1146
if p .nodeStart (firstStatement ) > blockStartLine + allowedLinesBeforeFirstStatement {
1143
- p .addError (stmt , nil , reasonBlockStartsWithWS , WhitespaceShouldRemoveBeginning , false )
1147
+ // TODO: We need to pick a better start of the range so we don't delete
1148
+ // potential comments.
1149
+ p .addErrorRange (
1150
+ blockStartPos ,
1151
+ blockStartPos ,
1152
+ firstStatement .Pos (),
1153
+ reasonBlockStartsWithWS ,
1154
+ WhitespaceShouldRemoveBeginning ,
1155
+ )
1144
1156
}
1145
1157
1146
1158
// If the blockEndLine is not 0 we're a regular block (not case).
@@ -1163,7 +1175,13 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
1163
1175
}
1164
1176
1165
1177
if p .nodeEnd (lastStatement ) != blockEndLine - 1 && ! isExampleFunc (ident ) {
1166
- p .addError (stmt , nil , reasonBlockEndsWithWS , WhitespaceShouldRemoveEnd , false )
1178
+ p .addErrorRange (
1179
+ stmt .End (),
1180
+ lastStatement .End (),
1181
+ stmt .End (),
1182
+ reasonBlockEndsWithWS ,
1183
+ WhitespaceShouldRemoveEnd ,
1184
+ )
1167
1185
}
1168
1186
1169
1187
return
@@ -1278,32 +1296,20 @@ func isEmptyLabeledStmt(node ast.Node) bool {
1278
1296
}
1279
1297
1280
1298
func (p * Processor ) addWhitespaceBeforeError (node ast.Node , reason string ) {
1281
- p .addError (node , nil , reason , WhitespaceShouldAddBefore , false )
1282
- }
1283
-
1284
- func (p * Processor ) addWhitespaceBeforeFixOtherNodeError (reportNode , fixNode ast.Node , reason string ) {
1285
- p .addError (reportNode , fixNode , reason , WhitespaceShouldAddBefore , false )
1299
+ p .addErrorRange (node .Pos (), node .Pos (), node .Pos (), reason , WhitespaceShouldAddBefore )
1286
1300
}
1287
1301
1288
1302
func (p * Processor ) addWhitespaceAfterError (node ast.Node , reason string ) {
1289
- p .addError (node , nil , reason , WhitespaceShouldAddAfter , false )
1303
+ p .addErrorRange (node . Pos (), node . End (), node . End (), reason , WhitespaceShouldAddAfter )
1290
1304
}
1291
1305
1292
- // Add an error for the file and line number for the current token.Pos with the
1293
- // given reason.
1294
- //
1295
- //nolint:unparam // We will potentially use this in the future.
1296
- func (p * Processor ) addError (reportNode , fixNode ast.Node , reason string , errType ErrorType , noFix bool ) {
1297
- if fixNode == nil {
1298
- fixNode = reportNode
1299
- }
1300
-
1306
+ func (p * Processor ) addErrorRange (reportAt , start , end token.Pos , reason string , errType ErrorType ) {
1301
1307
p .Result = append (p .Result , Result {
1302
- Node : reportNode ,
1303
- FixNode : fixNode ,
1304
- Reason : reason ,
1305
- NoFix : noFix ,
1306
- Type : errType ,
1308
+ ReportAt : reportAt ,
1309
+ FixRangeStart : start ,
1310
+ FixRangeEnd : end ,
1311
+ Reason : reason ,
1312
+ Type : errType ,
1307
1313
})
1308
1314
}
1309
1315
0 commit comments