Something went wrong. Try again.
🚀 Grammar-Aware Code Formatter: Structure through separation (supports Go, JavaScript, TypeScript, JSX, and TSX)
go formatter code-formatter javascript typescript jsx tsx
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469package main
import ( "fmt" "strings" "testing")
func TestFormatRemovesExtraBlankLines(t *testing.T) { inputSource := `package main
func main() { x := 1
y := 2}` expectedOutput := `package main
func main() { x := 1 y := 2}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatScopedStatements(t *testing.T) { inputSource := `package main
func main() { x := 1 if x > 0 { y := 2 } z := 3}` expectedOutput := `package main
func main() { x := 1
if x > 0 { y := 2 }
z := 3}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatNestedScopes(t *testing.T) { inputSource := `package main
func main() { if true { x := 1 if false { y := 2 } z := 3 }}` expectedOutput := `package main
func main() { if true { x := 1
if false { y := 2 }
z := 3 }}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatForLoop(t *testing.T) { inputSource := `package main
func main() { x := 1 for i := 0; i < 10; i++ { y := i } z := 2}` expectedOutput := `package main
func main() { x := 1
for i := 0; i < 10; i++ { y := i }
z := 2}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatSwitch(t *testing.T) { inputSource := `package main
func main() { x := 1 switch x { case 1: y := 2 } z := 3}` expectedOutput := `package main
func main() { x := 1
switch x { case 1: y := 2 }
z := 3}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatMultipleFunctions(t *testing.T) { inputSource := `package main
func foo() { x := 1}
func bar() { y := 2}` expectedOutput := `package main
func foo() { x := 1}
func bar() { y := 2}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatTypeStruct(t *testing.T) { inputSource := `package main
type Foo struct { X int}var x = 1` expectedOutput := `package main
type Foo struct { X int}
var x = 1` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatDifferentStatementTypes(t *testing.T) { inputSource := `package main
func main() { x := 1 y := 2 var a = 3 defer cleanup() defer cleanup2() go worker() return}` expectedOutput := `package main
func main() { x := 1 y := 2
var a = 3
defer cleanup() defer cleanup2()
go worker()
return}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatConsecutiveIfs(t *testing.T) { inputSource := `package main
func main() { if err != nil { return } if x > 0 { y = 1 }}` expectedOutput := `package main
func main() { if err != nil { return }
if x > 0 { y = 1 }}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatCaseClauseStatements(t *testing.T) { inputSource := `package main
func main() { switch x { case 1: foo() if err != nil { return } }}` expectedOutput := `package main
func main() { switch x { case 1: foo()
if err != nil { return } }}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatDeferInlineFunc(t *testing.T) { inputSource := `package main
func main() { defer func() { _ = file.Close() }() fileInfo, err := file.Stat()}` expectedOutput := `package main
func main() { defer func() { _ = file.Close() }()
fileInfo, err := file.Stat()}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func TestFormatCaseClauseAssignments(t *testing.T) { inputSource := `package main
func main() { switch x { case "user": roleStyle = UserStyle contentStyle = ContentStyle prefix = "You" case "assistant": roleStyle = AssistantStyle }}` expectedOutput := `package main
func main() { switch x { case "user": roleStyle = UserStyle contentStyle = ContentStyle prefix = "You" case "assistant": roleStyle = AssistantStyle }}` formatter := &Formatter{CommentMode: CommentsFollow} formattedResult, err := formatter.Format([]byte(inputSource), "test.go")
if err != nil { t.Fatalf("Format error: %v", err) }
if string(formattedResult) != expectedOutput { t.Errorf("got:\n%s\nwant:\n%s", formattedResult, expectedOutput) }}
func BenchmarkFormatSmall(b *testing.B) { inputSource := []byte(`package mainfunc main() { x := 1 y := 2 if x > 0 { z := 3 } a := 4}`) formatter := &Formatter{CommentMode: CommentsFollow}
for b.Loop() { _, _ = formatter.Format(inputSource, "test.go") }}
func BenchmarkFormatLarge(b *testing.B) { var sourceBuilder strings.Builder
sourceBuilder.WriteString("package main\n\n")
for functionIndex := range 100 { fmt.Fprintf(&sourceBuilder, "func foo%d", functionIndex) sourceBuilder.WriteString("() {\n") sourceBuilder.WriteString("\tx := 1\n") sourceBuilder.WriteString("\tif x > 0 {\n") sourceBuilder.WriteString("\t\ty := 2\n") sourceBuilder.WriteString("\t}\n") sourceBuilder.WriteString("\tz := 3\n") sourceBuilder.WriteString("}\n\n") }
inputSource := []byte(sourceBuilder.String()) formatter := &Formatter{CommentMode: CommentsFollow}
for b.Loop() { _, _ = formatter.Format(inputSource, "test.go") }}