From 7d5e827ecb13f2eee2ab0a7e96334329b091b104 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 3 Dec 2021 19:27:10 +0800 Subject: [PATCH 01/10] cmdlinestyle3 --- parser/_testdata/cmdlinestyle3/cmd3.gop | 1 + parser/_testdata/cmdlinestyle3/parser.expect | 27 ++++++++++++++++++++ parser/parserdir_test.go | 4 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 parser/_testdata/cmdlinestyle3/cmd3.gop create mode 100644 parser/_testdata/cmdlinestyle3/parser.expect diff --git a/parser/_testdata/cmdlinestyle3/cmd3.gop b/parser/_testdata/cmdlinestyle3/cmd3.gop new file mode 100644 index 000000000..b1f81ffdf --- /dev/null +++ b/parser/_testdata/cmdlinestyle3/cmd3.gop @@ -0,0 +1 @@ +println &x diff --git a/parser/_testdata/cmdlinestyle3/parser.expect b/parser/_testdata/cmdlinestyle3/parser.expect new file mode 100644 index 000000000..762e943ca --- /dev/null +++ b/parser/_testdata/cmdlinestyle3/parser.expect @@ -0,0 +1,27 @@ +package main + +file cmd3.gop +noEntrypoint +ast.FuncDecl: + Name: + ast.Ident: + Name: main + Type: + ast.FuncType: + Params: + ast.FieldList: + Body: + ast.BlockStmt: + List: + ast.ExprStmt: + X: + ast.CallExpr: + Fun: + ast.Ident: + Name: println + Args: + ast.UnaryExpr: + Op: & + X: + ast.Ident: + Name: x diff --git a/parser/parserdir_test.go b/parser/parserdir_test.go index 9db148cb9..62878a213 100644 --- a/parser/parserdir_test.go +++ b/parser/parserdir_test.go @@ -147,7 +147,9 @@ func TestFromTestdata(t *testing.T) { if strings.HasPrefix(name, "_") { continue } - testFrom(t, dir+"/"+name, sel, 0) + t.Run(name, func(t *testing.T) { + testFrom(t, dir+"/"+name, sel, 0) + }) } } From 2f05e2429f50bfa556d06a20a557ede6d156b573 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 3 Dec 2021 19:44:33 +0800 Subject: [PATCH 02/10] file.Comments --- x/format/gopstyle.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go index 68d8cf470..35bfc402e 100644 --- a/x/format/gopstyle.go +++ b/x/format/gopstyle.go @@ -35,6 +35,7 @@ func Gopstyle(file *ast.File) { fn := file.Decls[idx] copy(file.Decls[idx:], file.Decls[idx+1:]) file.Decls[last] = fn + // TODO: should also swap file.Comments } file.NoEntrypoint = true } From ecde15263489e72d8b096a04a9f6452ded0a2d86 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 3 Dec 2021 20:27:22 +0800 Subject: [PATCH 03/10] xformat: Source => GopstyleSource --- cmd/internal/gopfmt/fmt.go | 2 +- x/format/gopstyle.go | 2 +- x/format/gopstyle_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/internal/gopfmt/fmt.go b/cmd/internal/gopfmt/fmt.go index ae362467a..0d4ded324 100644 --- a/cmd/internal/gopfmt/fmt.go +++ b/cmd/internal/gopfmt/fmt.go @@ -70,7 +70,7 @@ func gopfmt(path string, smart, mvgo bool) (err error) { } var target []byte if smart { - target, err = xformat.Source(src) + target, err = xformat.GopstyleSource(src) } else { target, err = format.Source(src) } diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go index 35bfc402e..3f6139baf 100644 --- a/x/format/gopstyle.go +++ b/x/format/gopstyle.go @@ -11,7 +11,7 @@ import ( // ----------------------------------------------------------------------------- -func Source(src []byte) (ret []byte, err error) { +func GopstyleSource(src []byte) (ret []byte, err error) { fset := token.NewFileSet() if f, err := parser.ParseFile(fset, "", src, parser.ParseComments); err == nil { Gopstyle(f) diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go index f9f9d7dfb..2b9c88e4e 100644 --- a/x/format/gopstyle_test.go +++ b/x/format/gopstyle_test.go @@ -8,7 +8,7 @@ import ( func testFormat(t *testing.T, name string, src, expect string) { t.Run(name, func(t *testing.T) { - result, err := Source([]byte(src)) + result, err := GopstyleSource([]byte(src)) if err != nil { t.Fatal("format.Source failed:", err) } From dd229da5337376eea08bfb115e525c8b14240d06 Mon Sep 17 00:00:00 2001 From: visualfc Date: Fri, 3 Dec 2021 22:18:45 +0800 Subject: [PATCH 04/10] fix gopfmt dump error --- cmd/gopfmt/gopfmt.go | 6 ++++-- format/format.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/gopfmt/gopfmt.go b/cmd/gopfmt/gopfmt.go index afadf841a..a74afa08b 100644 --- a/cmd/gopfmt/gopfmt.go +++ b/cmd/gopfmt/gopfmt.go @@ -69,7 +69,7 @@ func processFile(filename string, in io.Reader, out io.Writer) error { return err } - res, err := format.Source(src) + res, err := format.Source(filename, src) if err != nil { return err } @@ -111,7 +111,9 @@ func walk(path string, d fs.DirEntry, err error) error { ext := filepath.Ext(path) if _, ok := extGops[ext]; ok { procCnt++ - err = processFile(path, nil, os.Stdout) + if err = processFile(path, nil, os.Stdout); err != nil { + report(err) + } } } return err diff --git a/format/format.go b/format/format.go index 9e05c16a5..63d927932 100644 --- a/format/format.go +++ b/format/format.go @@ -101,9 +101,9 @@ func Node(dst io.Writer, fset *token.FileSet, node interface{}) error { // space as src), and the result is indented by the same amount as the first // line of src containing code. Imports are not sorted for partial source files. // -func Source(src []byte) ([]byte, error) { +func Source(filename string, src []byte) ([]byte, error) { fset := token.NewFileSet() - file, sourceAdj, indentAdj, err := parse(fset, "", src, true) + file, sourceAdj, indentAdj, err := parse(fset, filename, src, true) if err != nil { return nil, err } From 9bc43c9827cb0c132fc8037b7a0e38bfaaf63dc5 Mon Sep 17 00:00:00 2001 From: visualfc Date: Fri, 3 Dec 2021 22:23:00 +0800 Subject: [PATCH 05/10] fix GopstyleSource check error --- cmd/internal/gopfmt/fmt.go | 12 ++++++++++-- x/format/gopstyle.go | 5 +++-- x/format/gopstyle_test.go | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/internal/gopfmt/fmt.go b/cmd/internal/gopfmt/fmt.go index 0d4ded324..0be10663a 100644 --- a/cmd/internal/gopfmt/fmt.go +++ b/cmd/internal/gopfmt/fmt.go @@ -70,9 +70,9 @@ func gopfmt(path string, smart, mvgo bool) (err error) { } var target []byte if smart { - target, err = xformat.GopstyleSource(src) + target, err = xformat.GopstyleSource(path, src) } else { - target, err = format.Source(src) + target, err = format.Source(path, src) } if err != nil { return @@ -127,12 +127,20 @@ func walk(path string, d fs.DirEntry, err error) error { smart := *flagSmart mvgo := smart && *flagMoveGo err = gopfmt(path, smart && (mvgo || ext != ".go"), mvgo) + if err != nil { + report(err) + } } } } return err } +func report(err error) { + fmt.Println(err) + os.Exit(2) +} + func runCmd(cmd *base.Command, args []string) { err := flag.Parse(args) if err != nil { diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go index 3f6139baf..7fba5f83f 100644 --- a/x/format/gopstyle.go +++ b/x/format/gopstyle.go @@ -11,9 +11,10 @@ import ( // ----------------------------------------------------------------------------- -func GopstyleSource(src []byte) (ret []byte, err error) { +func GopstyleSource(filename string, src []byte) (ret []byte, err error) { fset := token.NewFileSet() - if f, err := parser.ParseFile(fset, "", src, parser.ParseComments); err == nil { + var f *ast.File + if f, err = parser.ParseFile(fset, filename, src, parser.ParseComments); err == nil { Gopstyle(f) var buf bytes.Buffer if err = format.Node(&buf, fset, f); err == nil { diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go index 2b9c88e4e..77dfe0726 100644 --- a/x/format/gopstyle_test.go +++ b/x/format/gopstyle_test.go @@ -8,7 +8,7 @@ import ( func testFormat(t *testing.T, name string, src, expect string) { t.Run(name, func(t *testing.T) { - result, err := GopstyleSource([]byte(src)) + result, err := GopstyleSource(name, []byte(src)) if err != nil { t.Fatal("format.Source failed:", err) } From 61075914d3b5d8c3326c7d834f4a659baecde9f1 Mon Sep 17 00:00:00 2001 From: visualfc Date: Fri, 3 Dec 2021 22:28:03 +0800 Subject: [PATCH 06/10] x --- printer/gop_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printer/gop_test.go b/printer/gop_test.go index 13cd97035..329aaa22d 100644 --- a/printer/gop_test.go +++ b/printer/gop_test.go @@ -108,7 +108,7 @@ func testFrom(t *testing.T, fpath string) { t.Fatal(err) } - res, err := format.Source(src) + res, err := format.Source(fpath, src) if err != nil { t.Fatal("Source failed:", err) } From 9e827ef5bf798cd7a2c1d137065b562ab2e65bd2 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 3 Dec 2021 22:39:39 +0800 Subject: [PATCH 07/10] gop/format.Source: don't break compatibility of go/format.Source --- cmd/internal/gopfmt/fmt.go | 4 ++-- format/format.go | 8 ++++++-- printer/gop_test.go | 2 +- x/format/gopstyle.go | 8 ++++++-- x/format/gopstyle_test.go | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/internal/gopfmt/fmt.go b/cmd/internal/gopfmt/fmt.go index 0be10663a..11b1f1504 100644 --- a/cmd/internal/gopfmt/fmt.go +++ b/cmd/internal/gopfmt/fmt.go @@ -70,9 +70,9 @@ func gopfmt(path string, smart, mvgo bool) (err error) { } var target []byte if smart { - target, err = xformat.GopstyleSource(path, src) + target, err = xformat.GopstyleSource(src, path) } else { - target, err = format.Source(path, src) + target, err = format.Source(src, path) } if err != nil { return diff --git a/format/format.go b/format/format.go index 63d927932..3ee26f058 100644 --- a/format/format.go +++ b/format/format.go @@ -101,9 +101,13 @@ func Node(dst io.Writer, fset *token.FileSet, node interface{}) error { // space as src), and the result is indented by the same amount as the first // line of src containing code. Imports are not sorted for partial source files. // -func Source(filename string, src []byte) ([]byte, error) { +func Source(src []byte, filename ...string) ([]byte, error) { + var fname string + if filename != nil { + fname = filename[0] + } fset := token.NewFileSet() - file, sourceAdj, indentAdj, err := parse(fset, filename, src, true) + file, sourceAdj, indentAdj, err := parse(fset, fname, src, true) if err != nil { return nil, err } diff --git a/printer/gop_test.go b/printer/gop_test.go index 329aaa22d..25517a3c8 100644 --- a/printer/gop_test.go +++ b/printer/gop_test.go @@ -108,7 +108,7 @@ func testFrom(t *testing.T, fpath string) { t.Fatal(err) } - res, err := format.Source(fpath, src) + res, err := format.Source(src, fpath) if err != nil { t.Fatal("Source failed:", err) } diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go index 7fba5f83f..a5307d7a2 100644 --- a/x/format/gopstyle.go +++ b/x/format/gopstyle.go @@ -11,10 +11,14 @@ import ( // ----------------------------------------------------------------------------- -func GopstyleSource(filename string, src []byte) (ret []byte, err error) { +func GopstyleSource(src []byte, filename ...string) (ret []byte, err error) { + var fname string + if filename != nil { + fname = filename[0] + } fset := token.NewFileSet() var f *ast.File - if f, err = parser.ParseFile(fset, filename, src, parser.ParseComments); err == nil { + if f, err = parser.ParseFile(fset, fname, src, parser.ParseComments); err == nil { Gopstyle(f) var buf bytes.Buffer if err = format.Node(&buf, fset, f); err == nil { diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go index 77dfe0726..d6116bf52 100644 --- a/x/format/gopstyle_test.go +++ b/x/format/gopstyle_test.go @@ -8,7 +8,7 @@ import ( func testFormat(t *testing.T, name string, src, expect string) { t.Run(name, func(t *testing.T) { - result, err := GopstyleSource(name, []byte(src)) + result, err := GopstyleSource([]byte(src), name) if err != nil { t.Fatal("format.Source failed:", err) } From 64b581ae63a134868ed6e79fd704630ac7b2cebf Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 3 Dec 2021 22:42:32 +0800 Subject: [PATCH 08/10] x --- cmd/gopfmt/gopfmt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gopfmt/gopfmt.go b/cmd/gopfmt/gopfmt.go index a74afa08b..e29bcb134 100644 --- a/cmd/gopfmt/gopfmt.go +++ b/cmd/gopfmt/gopfmt.go @@ -69,7 +69,7 @@ func processFile(filename string, in io.Reader, out io.Writer) error { return err } - res, err := format.Source(filename, src) + res, err := format.Source(src, filename) if err != nil { return err } From c54e496799384d53e89501783e13fb7732725979 Mon Sep 17 00:00:00 2001 From: chenyuan Date: Sat, 4 Dec 2021 09:28:23 +0800 Subject: [PATCH 09/10] optimize: installer can now parse multi flags at the same time --- cmd/make.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/make.go b/cmd/make.go index 2b767c86a..74382d52b 100644 --- a/cmd/make.go +++ b/cmd/make.go @@ -182,6 +182,11 @@ func runTestcases() { coverage := "-coverprofile=coverage.txt" gopCommand := filepath.Join(detectGopBinPath(), "gop") + if !checkPathExist(gopCommand, false) { + println("Error: Go+ must be installed before running testcases.") + os.Exit(1) + } + testOutput, testErr, err := execCommand(gopCommand, "test", coverage, "-covermode=atomic", "./...") println(testOutput) println(testErr) @@ -239,13 +244,19 @@ func main() { isTest: runTestcases, } - for flag, action := range flagActionMap { + // Sort flags, for example: install flag should be checked earlier than test flag. + flags := []*bool{isInstall, isTest, isUninstall} + hasActionDone := false + + for _, flag := range flags { if *flag { - action() - return + flagActionMap[flag]() + hasActionDone = true } } - println("Usage:\n") - flag.PrintDefaults() + if !hasActionDone { + println("Usage:\n") + flag.PrintDefaults() + } } From 5745f1e22798f1cd581fcf295eca7da48f1d2a99 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 4 Dec 2021 12:42:14 +0800 Subject: [PATCH 10/10] Gopstyle: dont' swap main --- x/format/gopstyle.go | 13 ++++++++----- x/format/gopstyle_test.go | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go index a5307d7a2..66828b297 100644 --- a/x/format/gopstyle.go +++ b/x/format/gopstyle.go @@ -36,13 +36,16 @@ func Gopstyle(file *ast.File) { } if idx := findFuncDecl(file.Decls, "main"); idx >= 0 { last := len(file.Decls) - 1 - if idx != last { // swap main func to last - fn := file.Decls[idx] - copy(file.Decls[idx:], file.Decls[idx+1:]) - file.Decls[last] = fn + if idx == last { + file.NoEntrypoint = true + // TODO: idx != last: swap main func to last // TODO: should also swap file.Comments + /* + fn := file.Decls[idx] + copy(file.Decls[idx:], file.Decls[idx+1:]) + file.Decls[last] = fn + */ } - file.NoEntrypoint = true } } diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go index d6116bf52..b0a0f0377 100644 --- a/x/format/gopstyle_test.go +++ b/x/format/gopstyle_test.go @@ -23,11 +23,16 @@ func TestBasic1(t *testing.T) { import "fmt" +// this is main func main() { + // say hello fmt.Println("Hello world") } `, `import "fmt" +// this is main + +// say hello fmt.Println("Hello world") `) } @@ -37,7 +42,9 @@ func TestBasic2(t *testing.T) { import "fmt" +// this is main func main() { + // say hello fmt.Println("Hello world") } @@ -45,10 +52,14 @@ func f() { } `, `import "fmt" -func f() { +// this is main +func main() { + // say hello + fmt.Println("Hello world") } -fmt.Println("Hello world") +func f() { +} `) }