77 "go/token"
88 "go/types"
99 "path"
10+ "regexp"
1011 "strconv"
1112 "strings"
1213
@@ -41,10 +42,11 @@ func ConvertFile(ctx *Context, f *ast.File) (result *ir.File, err error) {
4142 }()
4243
4344 conv := & converter {
44- types : ctx .Types ,
45- pkg : ctx .Pkg ,
46- fset : ctx .Fset ,
47- src : ctx .Src ,
45+ types : ctx .Types ,
46+ pkg : ctx .Pkg ,
47+ fset : ctx .Fset ,
48+ src : ctx .Src ,
49+ versionPathRe : regexp .MustCompile (`^v[0-9]+$` ),
4850 }
4951 result = conv .ConvertFile (f )
5052 return result , nil
@@ -66,6 +68,8 @@ type converter struct {
6668 fset * token.FileSet
6769 src []byte
6870
71+ versionPathRe * regexp.Regexp
72+
6973 group * ir.RuleGroup
7074 groupFuncs []localMacroFunc
7175
@@ -224,6 +228,11 @@ func (conv *converter) convertRuleGroup(decl *ast.FuncDecl) *ir.RuleGroup {
224228 panic (conv .errorf (call , "Import() should be used before any rules definitions" ))
225229 }
226230 conv .doMatcherImport (call )
231+ case "ImportAs" :
232+ if seenRules {
233+ panic (conv .errorf (call , "ImportAs() should be used before any rules definitions" ))
234+ }
235+ conv .doMatcherImportAs (call )
227236 default :
228237 seenRules = true
229238 conv .convertRuleExpr (call )
@@ -375,7 +384,24 @@ func (conv *converter) localDefine(assign *ast.AssignStmt) {
375384
376385func (conv * converter ) doMatcherImport (call * ast.CallExpr ) {
377386 pkgPath := conv .parseStringArg (call .Args [0 ])
387+
388+ // Try to be at least somewhat module-aware.
389+ // If the last path part is "/v%d", we might want to take
390+ // the previous path part as a package name.
378391 pkgName := path .Base (pkgPath )
392+ if conv .versionPathRe .MatchString (pkgName ) {
393+ pkgName = path .Base (path .Dir (pkgPath ))
394+ }
395+
396+ conv .group .Imports = append (conv .group .Imports , ir.PackageImport {
397+ Path : pkgPath ,
398+ Name : pkgName ,
399+ })
400+ }
401+
402+ func (conv * converter ) doMatcherImportAs (call * ast.CallExpr ) {
403+ pkgPath := conv .parseStringArg (call .Args [0 ])
404+ pkgName := conv .parseStringArg (call .Args [1 ])
379405 conv .group .Imports = append (conv .group .Imports , ir.PackageImport {
380406 Path : pkgPath ,
381407 Name : pkgName ,
0 commit comments