Skip to content

Commit

Permalink
Fix "Error: an error occured while trying to parse imports: path is m…
Browse files Browse the repository at this point in the history
…issing starting quotes" when parsing a file with block comments.

Fixes #48.

Note that this only handles simple cases for block comments, and does
not cover more complex cases (see FIXME in parse.go).
  • Loading branch information
charleskorn committed Mar 2, 2022
1 parent 76d765e commit eb4921c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 7 deletions.
8 changes: 5 additions & 3 deletions pkg/constants/sequences.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package constants

const (
CommentFlag = "//"
ImportStartFlag = "\nimport (\n"
ImportEndFlag = "\n)"
LineCommentFlag = "//"
BlockCommentStartFlag = "/*"
BlockCommentEndFlag = "*/"
ImportStartFlag = "\nimport (\n"
ImportEndFlag = "\n)"

Blank = " "
Indent = "\t"
Expand Down
2 changes: 1 addition & 1 deletion pkg/gci/imports/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (i ImportDef) String() string {
func (i ImportDef) Format(cfg configuration.FormatterConfiguration) string {
linePrefix := constants.Indent
var output string
if cfg.NoPrefixComments == false {
if cfg.NoPrefixComments == false || i.QuotedPath == `"C"` {
for _, prefixComment := range i.PrefixComment {
output += linePrefix + prefixComment + constants.Linebreak
}
Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-block.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-line.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-line.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// #include "types.h"
"C"
)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-line.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// #include "types.h"
"C"
)
37 changes: 34 additions & 3 deletions pkg/gci/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,56 @@ import (
// Recursively parses import lines into a list of ImportDefs
func parseToImportDefinitions(unformattedLines []string) ([]importPkg.ImportDef, error) {
newImport := importPkg.ImportDef{}
inBlockComment := false
for index, unformattedLine := range unformattedLines {
line := strings.TrimSpace(unformattedLine)
if line == "" {
//empty line --> starts a new import
return parseToImportDefinitions(unformattedLines[index+1:])
}
if strings.HasPrefix(line, constants.CommentFlag) {
if strings.HasPrefix(line, constants.LineCommentFlag) {
// comment line
newImport.PrefixComment = append(newImport.PrefixComment, line)
continue
}

// FIXME: this doesn't correctly handle block comments that start part-way through a line or end part-way through a line, for example:
// /* some comment */ "golang.org/x/tools"
// Or:
// /* some comment
// */ "golang.org/x/tools"
//
// It only supports block comments that start and end on their own line, for example:
// /* some comment */
// "golang.org/x/tools"
// Or:
// /*
// some comment
// */
// "golang.org/x/tools"
if inBlockComment {
if strings.HasSuffix(line, constants.BlockCommentEndFlag) {
inBlockComment = false
} else {
line = "\t" + line
}

newImport.PrefixComment = append(newImport.PrefixComment, line)
continue
} else if strings.HasPrefix(line, constants.BlockCommentStartFlag) {
inBlockComment = true
newImport.PrefixComment = append(newImport.PrefixComment, line)
continue
}

// split inline comment from import
importSegments := strings.SplitN(line, constants.CommentFlag, 2)
importSegments := strings.SplitN(line, constants.LineCommentFlag, 2)
switch len(importSegments) {
case 1:
// no inline comment
case 2:
// inline comment present
newImport.InlineComment = constants.CommentFlag + importSegments[1]
newImport.InlineComment = constants.LineCommentFlag + importSegments[1]
default:
return nil, InvalidImportSplitError{importSegments}
}
Expand Down

0 comments on commit eb4921c

Please sign in to comment.