Skip to content

Extending directive checking #1257

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

Merged
merged 2 commits into from
Dec 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"go/ast"
"go/token"
"go/types"
"strings"
"regexp"
)

func RemoveParens(e ast.Expr) ast.Expr {
Expand Down Expand Up @@ -82,15 +82,7 @@ func FuncKey(d *ast.FuncDecl) string {
// such as code expecting ints to be 64-bit. It should be used with caution
// since it may create unused imports in the original source file.
func PruneOriginal(d *ast.FuncDecl) bool {
if d.Doc == nil {
return false
}
for _, c := range d.Doc.List {
if strings.HasPrefix(c.Text, "//gopherjs:prune-original") {
return true
}
}
return false
return hasDirective(d, `prune-original`)
}

// KeepOriginal returns true if gopherjs:keep-original directive is present
Expand All @@ -102,15 +94,38 @@ func PruneOriginal(d *ast.FuncDecl) bool {
// function in the original called `foo`, it will be accessible by the name
// `_gopherjs_original_foo`.
func KeepOriginal(d *ast.FuncDecl) bool {
if d.Doc == nil {
return false
}
for _, c := range d.Doc.List {
if strings.HasPrefix(c.Text, "//gopherjs:keep-original") {
return true
return hasDirective(d, `keep-original`)
}

// directiveMatcher is a regex which matches a GopherJS directive
// and finds the directive action.
var directiveMatcher = regexp.MustCompile(`^\/(?:\/|\*)gopherjs:([\w-]+)`)

// hasDirective returns true if the associated documentation
// or line comments for the given node have the given directive action.
//
// All GopherJS-specific directives must start with `//gopherjs:` or
// `/*gopherjs:` and followed by an action without any whitespace. The action
// must be one or more letter, decimal, underscore, or hyphen.
//
// see https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives
func hasDirective(node ast.Node, directiveAction string) bool {
foundDirective := false
ast.Inspect(node, func(n ast.Node) bool {
switch a := n.(type) {
case *ast.Comment:
m := directiveMatcher.FindStringSubmatch(a.Text)
if len(m) == 2 && m[1] == directiveAction {
foundDirective = true
}
return false
case *ast.CommentGroup:
return !foundDirective
default:
return n == node
}
}
return false
})
return foundDirective
}

// FindLoopStmt tries to find the loop statement among the AST nodes in the
Expand Down
Loading