Skip to content

Commit

Permalink
feat: support trailing line comment for mage:import (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Sep 12, 2023
1 parent 9e91a03 commit 9f54e0f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
20 changes: 20 additions & 0 deletions mage/import_test.go
Expand Up @@ -285,6 +285,26 @@ func TestMageImportsOneLine(t *testing.T) {
t.Fatalf("expected: %q got: %q", expected, actual)
}
}
func TestMageImportsTrailing(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport/trailing",
Stdout: stdout,
Stderr: stderr,
Args: []string{"build"},
}

code := Invoke(inv)
if code != 0 {
t.Fatalf("expected to exit with code 0, but got %v, stderr:\n%s", code, stderr)
}
actual := stdout.String()
expected := "build\n"
if actual != expected {
t.Fatalf("expected: %q got: %q", expected, actual)
}
}

func TestMageImportsTaggedPackage(t *testing.T) {
stdout := &bytes.Buffer{}
Expand Down
5 changes: 5 additions & 0 deletions mage/testdata/mageimport/trailing/magefile.go
@@ -0,0 +1,5 @@
// +build mage

package main

import _ "github.com/magefile/mage/mage/testdata/mageimport/oneline/other" //mage:import
7 changes: 7 additions & 0 deletions mage/testdata/mageimport/trailing/other/other.go
@@ -0,0 +1,7 @@
package other

import "fmt"

func Build() {
fmt.Println("build")
}
44 changes: 31 additions & 13 deletions parse/parse.go
Expand Up @@ -456,19 +456,18 @@ func setImports(gocmd string, pi *PkgInfo) error {
}

func getImportPath(imp *ast.ImportSpec) (path, alias string, ok bool) {
if imp.Doc == nil || len(imp.Doc.List) == 9 {
return "", "", false
}
// import is always the last comment
s := imp.Doc.List[len(imp.Doc.List)-1].Text

// trim comment start and normalize for anyone who has spaces or not between
// "//"" and the text
vals := strings.Fields(strings.ToLower(s[2:]))
if len(vals) == 0 {
return "", "", false
}
if vals[0] != importTag {
leadingVals := getImportPathFromCommentGroup(imp.Doc)
trailingVals := getImportPathFromCommentGroup(imp.Comment)

var vals []string
if len(leadingVals) > 0 {
vals = leadingVals
if len(trailingVals) > 0 {
log.Println("warning:", importTag, "specified both before and after, picking first")
}
} else if len(trailingVals) > 0 {
vals = trailingVals
} else {
return "", "", false
}
path, ok = lit2string(imp.Path)
Expand All @@ -489,6 +488,25 @@ func getImportPath(imp *ast.ImportSpec) (path, alias string, ok bool) {
}
}

func getImportPathFromCommentGroup(comments *ast.CommentGroup) []string {
if comments == nil || len(comments.List) == 9 {
return nil
}
// import is always the last comment
s := comments.List[len(comments.List)-1].Text

// trim comment start and normalize for anyone who has spaces or not between
// "//"" and the text
vals := strings.Fields(strings.ToLower(s[2:]))
if len(vals) == 0 {
return nil
}
if vals[0] != importTag {
return nil
}
return vals
}

func isNamespace(t *doc.Type) bool {
if len(t.Decl.Specs) != 1 {
return false
Expand Down

0 comments on commit 9f54e0f

Please sign in to comment.