Skip to content

Commit 5446fc2

Browse files
authored
fix: invalid parsing (#85)
* fix: invalid parsing Signed-off-by: Fernandez Ludovic <[email protected]> * tests: add tests Signed-off-by: Fernandez Ludovic <[email protected]>
1 parent 2c7593d commit 5446fc2

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

pkg/gci/gci.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
122122
return src, src, nil
123123
}
124124

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

143143
head := src[:headEnd]
144-
tail := src[tailStart:tailEnd]
144+
tail := src[tailStart:]
145145

146146
// sort for custom sections
147147
allKeys := make([]string, 0, len(result))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sections:
2+
- Standard
3+
- Default
4+
- Prefix(github.com/daixiang0)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
import (
3+
"fmt"
4+
5+
g "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
8+
)
9+
10+
11+
type test int
12+
13+
// test
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
import (
3+
"fmt"
4+
5+
g "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
8+
)
9+
10+
11+
type test int
12+
13+
// test

pkg/parse/parse.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,31 @@ func getImports(imp *ast.ImportSpec) (start, end int, name string) {
6464
return
6565
}
6666

67-
func ParseFile(src []byte) (ImportList, int, int, int, error) {
68-
parserMode := parser.Mode(0)
69-
parserMode |= parser.ParseComments
67+
func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
7068
fileSet := token.NewFileSet()
71-
f, err := parser.ParseFile(fileSet, "", src, parserMode)
69+
f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
7270
if err != nil {
73-
return nil, 0, 0, 0, err
71+
return nil, 0, 0, err
7472
}
7573

7674
if len(f.Imports) == 0 {
77-
return nil, 0, 0, 0, NoImportError{}
75+
return nil, 0, 0, NoImportError{}
7876
}
7977

78+
var headEnd int
79+
var tailStart int
80+
8081
var data ImportList
81-
for _, imp := range f.Imports {
82+
for i, imp := range f.Imports {
8283
start, end, name := getImports(imp)
84+
85+
if i == 0 {
86+
headEnd = start
87+
}
88+
if i == len(f.Imports)-1 {
89+
tailStart = end
90+
}
91+
8392
data = append(data, &GciImports{
8493
Start: start,
8594
End: end,
@@ -88,15 +97,11 @@ func ParseFile(src []byte) (ImportList, int, int, int, error) {
8897
})
8998
}
9099

91-
headEnd, _, _ := getImports(f.Imports[0])
92-
_, tailStart, _ := getImports(f.Imports[len(f.Imports)-1])
93-
tailEnd := f.Decls[len(f.Decls)-1].End()
94-
95100
sort.Sort(data)
96-
return data, headEnd, tailStart, int(tailEnd), nil
101+
return data, headEnd, tailStart, nil
97102
}
98103

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

0 commit comments

Comments
 (0)