Repository: goplus/gop
Pull request #2088 (2025-01-31 12:18:07)

x/format: fmt.println => echo
convert fmt.println => echo


Patch:
diff --git a/x/format/README.md b/x/format/README.md
index 5276fd6c..f4e28101 100644
--- a/x/format/README.md
+++ b/x/format/README.md
@@ -51,7 +51,7 @@ n, err := fmt.Println("Hello world")
 will be converted into:
 
 ```go
-n, err := println("Hello world")
+n, err := echo("Hello world")
 ```
 
 Note:
@@ -62,7 +62,7 @@ Note:
 * Convert `fmt.Fprintln => fprintln`
 * Convert `fmt.Print` => `print`
 * Convert `fmt.Printf` => `printf`
-* Convert `fmt.Println` => `println`
+* Convert `fmt.Println` => `echo`
 * Convert `fmt.Sprint => sprint`
 * Convert `fmt.Sprintf => sprintf`
 * Convert `fmt.Sprintln => sprintln`
@@ -79,8 +79,8 @@ fmt.Println(fmt.Println("Hello world"))
 will be converted into:
 
 ```go
-println
-println println("Hello world")
+echo
+echo echo("Hello world")
 ```
 
 Note:
diff --git a/x/format/format.go b/x/format/format.go
index eefbdc01..b643da37 100644
--- a/x/format/format.go
+++ b/x/format/format.go
@@ -40,7 +40,11 @@ func fmtToBuiltin(ctx *importCtx, sel *ast.Ident, ref *ast.Expr) bool {
 	if ctx.pkgPath == "fmt" {
 		for _, fns := range printFuncs {
 			if fns[0] == sel.Name || fns[1] == sel.Name {
-				*ref = &ast.Ident{NamePos: sel.NamePos, Name: fns[1]}
+				name := fns[1]
+				if name == "println" {
+					name = "echo"
+				}
+				*ref = &ast.Ident{NamePos: sel.NamePos, Name: name}
 				return true
 			}
 		}


Test patch:
diff --git a/x/format/_testdata/collection/format.expect b/x/format/_testdata/collection/format.expect
index 1db3a770..900ab074 100644
--- a/x/format/_testdata/collection/format.expect
+++ b/x/format/_testdata/collection/format.expect
@@ -84,17 +84,17 @@ func Map(vs []string, f func(string) string) []string {
 // Here we try out our various collection functions.
 var strs = []string{"peach", "apple", "pear", "plum"}
 
-println Index(strs, "pear")
+echo Index(strs, "pear")
 
-println Include(strs, "grape")
+echo Include(strs, "grape")
 
-println Any(strs, v => strings.hasPrefix(v, "p"))
+echo Any(strs, v => strings.hasPrefix(v, "p"))
 
-println All(strs, v => strings.hasPrefix(v, "p"))
+echo All(strs, v => strings.hasPrefix(v, "p"))
 
-println Filter(strs, v => strings.contains(v, "e"))
+echo Filter(strs, v => strings.contains(v, "e"))
 
 // The above examples all used anonymous functions,
 // but you can also use named functions of the correct
 // type.
-println Map(strs, strings.ToUpper)
+echo Map(strs, strings.ToUpper)
diff --git a/x/format/_testdata/syntax/format.expect b/x/format/_testdata/syntax/format.expect
index 52884fd9..74399e16 100644
--- a/x/format/_testdata/syntax/format.expect
+++ b/x/format/_testdata/syntax/format.expect
@@ -7,7 +7,7 @@ m := make(map[string]chan interface{})
 for range arr[1:] {
 foo:
 	*(p) = 1
-	println f(arr...)
+	echo f(arr...)
 	m["a"] <- true
 }
 for i := 0; i < 10; i++ {
diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go
index 866e90ab..cf01dde4 100644
--- a/x/format/gopstyle_test.go
+++ b/x/format/gopstyle_test.go
@@ -47,7 +47,7 @@ func main() {
 `, `// this is main
 
 // say hello
-println "Hello world"
+echo "Hello world"
 `)
 	testFormat(t, "hello world 2", `package main
 
@@ -64,7 +64,7 @@ func f() {
 `, `// this is main
 func main() {
 	// say hello
-	println "Hello world"
+	echo "Hello world"
 }
 
 func f() {
@@ -120,7 +120,7 @@ func f() {
 	fmt.Println("hello")
 }
 `, `func f() {
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -136,7 +136,7 @@ func f() {
 	fmt.Println("hello")
 }
 `, `func f() {
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -155,7 +155,7 @@ func f() {
 
 func f() {
 	errorf "%w", New("hello")
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -178,7 +178,7 @@ func f() {
 
 func f() {
 	errorf "%w", errors.new("hello")
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -197,7 +197,7 @@ func f() {
 
 func f() {
 	_ = errorf("%w", errors.new("hello"))
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -220,7 +220,7 @@ func f() {
 
 func f() {
 	_ = errorf("%w", errors.new("hello"))
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -238,7 +238,7 @@ func f() {
 
 func f() {
 	_ = fmt.Stringer
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -256,7 +256,7 @@ func f() {
 
 func f() {
 	var _ fmt.Stringer
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -280,7 +280,7 @@ func f() {
 		fmt.Stringer
 		fn func()
 	}
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -294,7 +294,7 @@ func f() {
 	fmt1.Println("hello")
 }
 `, `func f() {
-	println "hello"
+	echo "hello"
 }
 `)
 }
@@ -312,8 +312,8 @@ func f() {
 	fmt2.Println(2)
 }
 `, `func f() {
-	println 1
-	println 2
+	echo 1
+	echo 2
 }
 `)
 }
@@ -337,8 +337,8 @@ func f() {
 
 func f() {
 	var _ fmt.Stringer
-	println 1
-	println 2
+	echo 1
+	echo 2
 }
 `)
 }
@@ -426,7 +426,7 @@ func f() {
 	_ = fmt
 }
 `, `func f() {
-	println 1
+	echo 1
 	var fmt Foo
 	_ = fmt
 }