Skip to content
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
28 changes: 20 additions & 8 deletions modfile/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,34 @@ import (
"github.com/qiniu/x/errors"
)

// can be "_[class].gox" or ".[class]"
func isExt(s string) bool {
return len(s) > 1 && (s[0] == '_' || s[0] == '.')
// can be "_[class].gox", "*_[class].gox", ".[class]" or "*.[class]"
// can be "main_[class].gox", "main.[class]" if isProj is true
func isExt(s string, isProj bool) bool {
return len(s) > 1 && (s[0] == '*' || s[0] == '_' || s[0] == '.') ||
isProj && len(s) > 4 && s[:4] == "main" && (s[4] == '_' || s[4] == '.')
}

func parseExt(s *string) (t string, err error) {
t, err = parseString(s)
func getExt(s string) string {
if len(s) > 1 && s[0] == '*' {
return s[1:]
}
if len(s) > 4 && s[:4] == "main" {
return s[4:]
}
return s
}

func parseExt(s *string, isProj bool) (ext, fullExt string, err error) {
t, err := parseString(s)
if err != nil {
goto failed
}
if isExt(t) {
return
if isExt(t, isProj) {
return getExt(t), t, nil
}
err = errors.New("invalid ext format")
failed:
return "", &InvalidExtError{
return "", "", &InvalidExtError{
Ext: *s,
Err: err,
}
Expand Down
4 changes: 2 additions & 2 deletions modfile/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ gop 1.1 1.2
doTestParseErr(t, `gop.mod:2: invalid gop version '1.x': must match format 1.23`, `
gop 1.x
`)
doTestParseErr(t, `gop.mod:2: usage: project [.projExt ProjClass] classFilePkgPath ...`, `
doTestParseErr(t, `gop.mod:2: usage: project [*.projExt ProjClass] classFilePkgPath ...`, `
project
`)
doTestParseErr(t, `gop.mod:2: usage: project [.projExt ProjClass] classFilePkgPath ...`, `
doTestParseErr(t, `gop.mod:2: usage: project [*.projExt ProjClass] classFilePkgPath ...`, `
project .gmx Game
`)
doTestParseErr(t, `gop.mod:2: ext ." invalid: unquoted string cannot contain quote`, `
Expand Down
34 changes: 19 additions & 15 deletions modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ type Gop = modfile.Go

// A Class is the work class statement.
type Class struct {
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".spx"
Class string // "Sprite"
Proto string // prototype of the work class (not empty if multiple work classes)
Syntax *Line
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".spx"
FullExt string // can be "*_[class].gox", "_[class].gox", "*.[class]" or ".[class]"
Class string // "Sprite"
Proto string // prototype of the work class (not empty if multiple work classes)
Embedded bool // if true, the class instance will be embedded in the project
Syntax *Line
}

// A Import is the import statement.
Expand All @@ -75,6 +77,7 @@ type Import struct {
// A Project is the project statement.
type Project struct {
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".gmx"
FullExt string // can be "main_[class].gox", "*_[class].gox", "_[class].gox", "main.[class]", "*.[class]" or ".[class]"
Class string // "Game"
Works []*Class // work class of classfile
PkgPaths []string // package paths of classfile and optional inline-imported packages.
Expand Down Expand Up @@ -199,15 +202,15 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
f.Gop.Version = args[0]
case "project":
if len(args) < 1 {
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
errorf("usage: project [*.projExt ProjClass] classFilePkgPath ...")
return
}
if isExt(args[0]) {
if isExt(args[0], true) {
if len(args) < 3 || strings.Contains(args[1], "/") {
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
errorf("usage: project [*.projExt ProjClass] classFilePkgPath ...")
return
}
ext, err := parseExt(&args[0])
ext, fullExt, err := parseExt(&args[0], true)
if err != nil {
wrapError(err)
return
Expand All @@ -223,7 +226,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
return
}
f.addProj(&Project{
Ext: ext, Class: class, PkgPaths: pkgPaths, Syntax: line,
Ext: ext, FullExt: fullExt, Class: class, PkgPaths: pkgPaths, Syntax: line,
})
return
}
Expand All @@ -245,7 +248,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
errorf("usage: class .workExt WorkClass [ProjClass]")
return
}
workExt, err := parseExt(&args[0])
workExt, fullExt, err := parseExt(&args[0], false)
if err != nil {
wrapError(err)
return
Expand All @@ -264,10 +267,11 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
}
}
proj.Works = append(proj.Works, &Class{
Ext: workExt,
Class: class,
Proto: protoClass,
Syntax: line,
Ext: workExt,
FullExt: fullExt,
Class: class,
Proto: protoClass,
Syntax: line,
})
case "import":
proj := f.proj()
Expand Down Expand Up @@ -329,7 +333,7 @@ func AutoQuote(s string) string {
}

var (
symbolRE = regexp.MustCompile("\\*?[A-Z]\\w*")
symbolRE = regexp.MustCompile(`\*?[A-Z]\w*`)
)

// TODO: to be optimized
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Note is recommended to use "MARKER(uid): note body" format.

Details

lint 解释

这个lint结果提示在使用注释时,建议采用“MARKER(uid): note body”的格式。这意味着注释应该包含一个标记(MARKER)和一个用户ID(uid),后面跟着注释的具体内容。

错误用法

// 这是一个错误的注释示例

正确用法

// MARKER(12345): 这是一个正确的注释示例,包含标记和用户ID

💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流

Expand Down
38 changes: 35 additions & 3 deletions modfile/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,57 @@ var addParseExtTests = []struct {
desc string
ext string
want string
wantF string
wantErr string
isProj bool
}{
{
"spx ok",
".spx",
".spx",
".spx",
"",
false,
},
{
"yap ok",
"_yap.gox",
"_yap.gox",
"_yap.gox",
"",
false,
},
{
"yap ok",
"*_yap.gox",
"_yap.gox",
"*_yap.gox",
"",
false,
},
{
"yap ok",
"main_yap.gox",
"_yap.gox",
"main_yap.gox",
"",
true,
},
{
"yap ok",
"main_yap.gox",
"",
"",
"ext main_yap.gox invalid: invalid ext format",
false,
},
{
"not a ext",
"gmx",
"",
"",
"ext gmx invalid: invalid ext format",
false,
},
}

Expand All @@ -57,14 +89,14 @@ func TestParseExt(t *testing.T) {
}
for _, tt := range addParseExtTests {
t.Run(tt.desc, func(t *testing.T) {
ext, err := parseExt(&tt.ext)
ext, extF, err := parseExt(&tt.ext, tt.isProj)
if err != nil {
if err.Error() != tt.wantErr {
t.Fatalf("wanterr: %s, but got: %s", tt.wantErr, err)
}
}
if ext != tt.want {
t.Fatalf("want: %s, but got: %s", tt.want, ext)
if ext != tt.want || extF != tt.wantF {
t.Fatalf("want: %s %s, but got: %s %s", tt.want, tt.wantF, ext, extF)
}
})
}
Expand Down