Skip to content

Commit

Permalink
Merge pull request #1027 from goplus/main
Browse files Browse the repository at this point in the history
Release v1.0.33
  • Loading branch information
xushiwei committed Dec 5, 2021
2 parents fc42b40 + 15116b6 commit 83013e6
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
6 changes: 4 additions & 2 deletions cmd/gopfmt/gopfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(src, filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions cmd/internal/gopfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func gopfmt(path string, smart, mvgo bool) (err error) {
}
var target []byte
if smart {
target, err = xformat.Source(src)
target, err = xformat.GopstyleSource(src, path)
} else {
target, err = format.Source(src)
target, err = format.Source(src, path)
}
if err != nil {
return
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
8 changes: 6 additions & 2 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(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, "", src, true)
file, sourceAdj, indentAdj, err := parse(fset, fname, src, true)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions parser/_testdata/cmdlinestyle3/cmd3.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
println &x
27 changes: 27 additions & 0 deletions parser/_testdata/cmdlinestyle3/parser.expect
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion parser/parserdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion printer/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func testFrom(t *testing.T, fpath string) {
t.Fatal(err)
}

res, err := format.Source(src)
res, err := format.Source(src, fpath)
if err != nil {
t.Fatal("Source failed:", err)
}
Expand Down
23 changes: 16 additions & 7 deletions x/format/gopstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import (

// -----------------------------------------------------------------------------

func Source(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()
if f, err := parser.ParseFile(fset, "", src, parser.ParseComments); err == nil {
var f *ast.File
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 {
Expand All @@ -31,12 +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
}
}

Expand Down
17 changes: 14 additions & 3 deletions x/format/gopstyle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), name)
if err != nil {
t.Fatal("format.Source failed:", err)
}
Expand All @@ -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")
`)
}
Expand All @@ -37,18 +42,24 @@ func TestBasic2(t *testing.T) {
import "fmt"
// this is main
func main() {
// say hello
fmt.Println("Hello world")
}
func f() {
}
`, `import "fmt"
func f() {
// this is main
func main() {
// say hello
fmt.Println("Hello world")
}
fmt.Println("Hello world")
func f() {
}
`)
}

Expand Down

0 comments on commit 83013e6

Please sign in to comment.