Skip to content

Commit a8add9b

Browse files
committed
Move findKeepOverriddenComments into astutil package for consistency with other such helpers
1 parent fe0510d commit a8add9b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

build/build.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,6 @@ func ImportDir(dir string, mode build.ImportMode, installSuffix string, buildTag
117117
return pkg, nil
118118
}
119119

120-
// Test if we find the '//gopherjs:keep-original' comment
121-
func findKeepOverriddenComment(doc *ast.CommentGroup) bool {
122-
if doc == nil {
123-
return false
124-
}
125-
for _, comment := range doc.List {
126-
text := comment.Text
127-
if i := strings.Index(text, " "); i >= 0 {
128-
text = text[:i]
129-
}
130-
if text == "//gopherjs:keep-original" {
131-
return true
132-
}
133-
}
134-
return false
135-
}
136-
137120
// parseAndAugment parses and returns all .go files of given pkg.
138121
// Standard Go library packages are augmented with files in compiler/natives folder.
139122
// If isTest is true and pkg.ImportPath has no _test suffix, package is built for running internal tests.
@@ -194,7 +177,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
194177
switch d := decl.(type) {
195178
case *ast.FuncDecl:
196179
k := astutil.FuncKey(d)
197-
replacedDeclNames[k] = overrideInfo{keepOverridden: findKeepOverriddenComment(d.Doc)}
180+
replacedDeclNames[k] = overrideInfo{keepOverridden: astutil.KeepOriginal(d)}
198181
pruneOriginalFuncs[k] = astutil.PruneOriginal(d)
199182
case *ast.GenDecl:
200183
switch d.Tok {

compiler/astutil/astutil.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ func PruneOriginal(d *ast.FuncDecl) bool {
9393
return false
9494
}
9595

96+
// KeepOriginal returns true if gopherjs:keep-original directive is present
97+
// before a function decl.
98+
//
99+
// `//gopherjs:keep-original` is a GopherJS-specific directive, which can be
100+
// applied to functions in native overlays and will instruct the augmentation
101+
// logic to expose the original function such that it can be called. For a
102+
// function in the original called `foo`, it will be accessible by the name
103+
// `_gopherjs_overridden_foo`.
104+
func KeepOriginal(d *ast.FuncDecl) bool {
105+
if d.Doc == nil {
106+
return false
107+
}
108+
for _, c := range d.Doc.List {
109+
if strings.HasPrefix(c.Text, "//gopherjs:keep-original") {
110+
return true
111+
}
112+
}
113+
return false
114+
}
115+
96116
// FindLoopStmt tries to find the loop statement among the AST nodes in the
97117
// |stack| that corresponds to the break/continue statement represented by
98118
// branch.

0 commit comments

Comments
 (0)