github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/packages/errors.go (about)

     1  package packages
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"go/token"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  func ParseErrorPosition(pos string) (*token.Position, error) {
    12  	// file:line(<optional>:colon)
    13  	parts := strings.Split(pos, ":")
    14  	if len(parts) == 1 {
    15  		return nil, errors.New("no colons")
    16  	}
    17  
    18  	file := parts[0]
    19  	line, err := strconv.Atoi(parts[1])
    20  	if err != nil {
    21  		return nil, fmt.Errorf("can't parse line number %q: %w", parts[1], err)
    22  	}
    23  
    24  	var column int
    25  	if len(parts) == 3 { // no column
    26  		column, err = strconv.Atoi(parts[2])
    27  		if err != nil {
    28  			return nil, fmt.Errorf("failed to parse column from %q: %w", parts[2], err)
    29  		}
    30  	}
    31  
    32  	return &token.Position{
    33  		Filename: file,
    34  		Line:     line,
    35  		Column:   column,
    36  	}, nil
    37  }