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

feat: support trailing line comment for mage:import #480

Merged
merged 1 commit into from
Sep 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions mage/import_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package other

import "fmt"

func Build() {
fmt.Println("build")
}
44 changes: 31 additions & 13 deletions parse/parse.go
Original file line number Diff line number Diff line change
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