gotools/gopls/internal/test/marker/testdata/codeaction/invertif.txt

This test exercises the 'invert if condition' code action.

-- p.go --
package invertif

import (
	"fmt"
	"os"
)

func Boolean() {
	b := true
	if b { //@codeactionedit("if b", "refactor.rewrite.invertIf", boolean)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func BooleanFn() {
	if os.IsPathSeparator('X') { //@codeactionedit("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", boolean_fn)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

// Note that the comment here jumps to the wrong location.
func DontRemoveParens() {
	a := false
	b := true
	if !(a ||
		b) { //@codeactionedit("b", "refactor.rewrite.invertIf", dont_remove_parens)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func ElseIf() {
	// No inversion expected when there's not else clause
	if len(os.Args) > 2 {
		fmt.Println("A")
	}

	// No inversion expected for else-if, that would become unreadable
	if len(os.Args) > 2 {
		fmt.Println("A")
	} else if os.Args[0] == "X" { //@codeactionedit(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", else_if)
		fmt.Println("B")
	} else {
		fmt.Println("C")
	}
}

func GreaterThan() {
	if len(os.Args) > 2 { //@codeactionedit("i", "refactor.rewrite.invertIf", greater_than)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func NotBoolean() {
	b := true
	if !b { //@codeactionedit("if !b", "refactor.rewrite.invertIf", not_boolean)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func RemoveElse() {
	if true { //@codeactionedit("if true", "refactor.rewrite.invertIf", remove_else)
		fmt.Println("A")
	} else {
		fmt.Println("B")
		return
	}

	fmt.Println("C")
}

func RemoveParens() {
	b := true
	if !(b) { //@codeactionedit("if", "refactor.rewrite.invertIf", remove_parens)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func Semicolon() {
	if _, err := fmt.Println("x"); err != nil { //@codeactionedit("if", "refactor.rewrite.invertIf", semicolon)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func SemicolonAnd() {
	if n, err := fmt.Println("x"); err != nil && n > 0 { //@codeactionedit("f", "refactor.rewrite.invertIf", semicolon_and)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

func SemicolonOr() {
	if n, err := fmt.Println("x"); err != nil || n < 5 { //@codeactionedit(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", semicolon_or)
		fmt.Println("A")
	} else {
		fmt.Println("B")
	}
}

-- @boolean/p.go --
@@ -10,3 +10 @@
-	if b { //@codeactionedit("if b", "refactor.rewrite.invertIf", boolean)
-		fmt.Println("A")
-	} else {
+	if !b {
@@ -14 +12,2 @@
+	} else { //@codeactionedit("if b", "refactor.rewrite.invertIf", boolean)
+		fmt.Println("A")
-- @boolean_fn/p.go --
@@ -18,3 +18 @@
-	if os.IsPathSeparator('X') { //@codeactionedit("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", boolean_fn)
-		fmt.Println("A")
-	} else {
+	if !os.IsPathSeparator('X') {
@@ -22 +20,2 @@
+	} else { //@codeactionedit("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", boolean_fn)
+		fmt.Println("A")
-- @dont_remove_parens/p.go --
@@ -29,4 +29,2 @@
-	if !(a ||
-		b) { //@codeactionedit("b", "refactor.rewrite.invertIf", dont_remove_parens)
-		fmt.Println("A")
-	} else {
+	if (a ||
+		b) {
@@ -34 +32,2 @@
+	} else { //@codeactionedit("b", "refactor.rewrite.invertIf", dont_remove_parens)
+		fmt.Println("A")
-- @else_if/p.go --
@@ -46,3 +46 @@
-	} else if os.Args[0] == "X" { //@codeactionedit(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", else_if)
-		fmt.Println("B")
-	} else {
+	} else if os.Args[0] != "X" {
@@ -50 +48,2 @@
+	} else { //@codeactionedit(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", else_if)
+		fmt.Println("B")
-- @greater_than/p.go --
@@ -54,3 +54 @@
-	if len(os.Args) > 2 { //@codeactionedit("i", "refactor.rewrite.invertIf", greater_than)
-		fmt.Println("A")
-	} else {
+	if len(os.Args) <= 2 {
@@ -58 +56,2 @@
+	} else { //@codeactionedit("i", "refactor.rewrite.invertIf", greater_than)
+		fmt.Println("A")
-- @not_boolean/p.go --
@@ -63,3 +63 @@
-	if !b { //@codeactionedit("if !b", "refactor.rewrite.invertIf", not_boolean)
-		fmt.Println("A")
-	} else {
+	if b {
@@ -67 +65,2 @@
+	} else { //@codeactionedit("if !b", "refactor.rewrite.invertIf", not_boolean)
+		fmt.Println("A")
-- @remove_else/p.go --
@@ -71,3 +71 @@
-	if true { //@codeactionedit("if true", "refactor.rewrite.invertIf", remove_else)
-		fmt.Println("A")
-	} else {
+	if false {
@@ -78 +76,3 @@
+	//@codeactionedit("if true", "refactor.rewrite.invertIf", remove_else)
+	fmt.Println("A")
+
-- @remove_parens/p.go --
@@ -83,3 +83 @@
-	if !(b) { //@codeactionedit("if", "refactor.rewrite.invertIf", remove_parens)
-		fmt.Println("A")
-	} else {
+	if b {
@@ -87 +85,2 @@
+	} else { //@codeactionedit("if", "refactor.rewrite.invertIf", remove_parens)
+		fmt.Println("A")
-- @semicolon/p.go --
@@ -91,3 +91 @@
-	if _, err := fmt.Println("x"); err != nil { //@codeactionedit("if", "refactor.rewrite.invertIf", semicolon)
-		fmt.Println("A")
-	} else {
+	if _, err := fmt.Println("x"); err == nil {
@@ -95 +93,2 @@
+	} else { //@codeactionedit("if", "refactor.rewrite.invertIf", semicolon)
+		fmt.Println("A")
-- @semicolon_and/p.go --
@@ -99,3 +99 @@
-	if n, err := fmt.Println("x"); err != nil && n > 0 { //@codeactionedit("f", "refactor.rewrite.invertIf", semicolon_and)
-		fmt.Println("A")
-	} else {
+	if n, err := fmt.Println("x"); err == nil || n <= 0 {
@@ -103 +101,2 @@
+	} else { //@codeactionedit("f", "refactor.rewrite.invertIf", semicolon_and)
+		fmt.Println("A")
-- @semicolon_or/p.go --
@@ -107,3 +107 @@
-	if n, err := fmt.Println("x"); err != nil || n < 5 { //@codeactionedit(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", semicolon_or)
-		fmt.Println("A")
-	} else {
+	if n, err := fmt.Println("x"); err == nil && n >= 5 {
@@ -111 +109,2 @@
+	} else { //@codeactionedit(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", semicolon_or)
+		fmt.Println("A")