Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid parsing #85

Merged
merged 2 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
return src, src, nil
}

imports, headEnd, tailStart, tailEnd, err := parse.ParseFile(src)
imports, headEnd, tailStart, err := parse.ParseFile(src, file.Path())
if err != nil {
if errors.Is(err, parse.NoImportError{}) {
return src, src, nil
Expand All @@ -141,7 +141,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
}

head := src[:headEnd]
tail := src[tailStart:tailEnd]
tail := src[tailStart:]

// sort for custom sections
allKeys := make([]string, 0, len(result))
Expand Down
4 changes: 4 additions & 0 deletions pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- Prefix(github.com/daixiang0)
13 changes: 13 additions & 0 deletions pkg/gci/internal/testdata/comment-in-the-tail.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main
import (
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
)


type test int

// test
13 changes: 13 additions & 0 deletions pkg/gci/internal/testdata/comment-in-the-tail.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main
import (
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
)


type test int

// test
31 changes: 18 additions & 13 deletions pkg/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,31 @@ func getImports(imp *ast.ImportSpec) (start, end int, name string) {
return
}

func ParseFile(src []byte) (ImportList, int, int, int, error) {
parserMode := parser.Mode(0)
parserMode |= parser.ParseComments
func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, "", src, parserMode)
f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
if err != nil {
return nil, 0, 0, 0, err
return nil, 0, 0, err
}

if len(f.Imports) == 0 {
return nil, 0, 0, 0, NoImportError{}
return nil, 0, 0, NoImportError{}
}

var headEnd int
var tailStart int

var data ImportList
for _, imp := range f.Imports {
for i, imp := range f.Imports {
start, end, name := getImports(imp)

if i == 0 {
headEnd = start
}
if i == len(f.Imports)-1 {
tailStart = end
}

data = append(data, &GciImports{
Start: start,
End: end,
Expand All @@ -88,15 +97,11 @@ func ParseFile(src []byte) (ImportList, int, int, int, error) {
})
}

headEnd, _, _ := getImports(f.Imports[0])
_, tailStart, _ := getImports(f.Imports[len(f.Imports)-1])
tailEnd := f.Decls[len(f.Decls)-1].End()

sort.Sort(data)
return data, headEnd, tailStart, int(tailEnd), nil
return data, headEnd, tailStart, nil
}

// isGenerated reports whether the source file is generated code.
// IsGeneratedFileByComment reports whether the source file is generated code.
// Using a bit laxer rules than https://golang.org/s/generatedcode to
// match more generated code.
// Taken from https://github.com/golangci/golangci-lint.
Expand Down