Skip to content

Commit

Permalink
Merge pull request #113 from rinchsan/revert-misfixing-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchsan authored Aug 28, 2022
2 parents 8508083 + 3eda2e6 commit 867858e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
45 changes: 45 additions & 0 deletions internal/imports/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,51 @@ func f() {
`,
},

// Don't touch import lines in raw string
{
name: "import_lines_in_raw_string",
in: `package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
const helloWorld = ` + "`" + `
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}` + "`\n",
out: `package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
const helloWorld = ` + "`" + `
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}` + "`\n",
},

// Put some things in their own section
{
name: "make_sections",
Expand Down
4 changes: 3 additions & 1 deletion internal/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func separateImportsIntoGroups(r io.Reader, impsByGroup map[int][]*ast.ImportSpe
var out bytes.Buffer
in := bufio.NewReader(r)
inImports := false
done := false
impInserted := false
for {
s, err := in.ReadString('\n')
Expand All @@ -269,7 +270,7 @@ func separateImportsIntoGroups(r io.Reader, impsByGroup map[int][]*ast.ImportSpe
return nil, err
}

if !inImports && strings.HasPrefix(s, "import") && strings.Contains(s, "(") {
if !inImports && !done && strings.HasPrefix(s, "import") && strings.Contains(s, "(") {
inImports = true
fmt.Fprint(&out, s)
continue
Expand Down Expand Up @@ -297,6 +298,7 @@ func separateImportsIntoGroups(r io.Reader, impsByGroup map[int][]*ast.ImportSpe
continue
}
if inImports && !strings.Contains(s, "//") && strings.Contains(s, ")") {
done = true
inImports = false
}

Expand Down

0 comments on commit 867858e

Please sign in to comment.