Skip to content

goimports fork, which works deterministically, keeping as many comments as possible.

License

Notifications You must be signed in to change notification settings

homuler/goimports-wasabi

Repository files navigation

goimports-wasabi

Run Tests

This is a fork of goimports, inspired by the gosimports project. goimports-wasabi also tries to solve the import grouping/ordering problem, but in a different way.

Overview

goimports and gosimports have the following issues (as of 2022/11).

  • goimports doesn't sort import specs deterministically.
  • goimports doesn't treat comments associated with import specs properly.
  • goimports doesn't work well for artificial inputs.
  • gosimports removes comments associated with import specs.

goimports-wasabi is designed to work deterministically with any valid input, leaving as many comments as possible in the (supposedly) correct place.

Installation

go install github.com/homuler/goimports-wasabi@latest

Examples

Grouping/Ordering Problem

Input

import (
	// doc comment for fmt
	"fmt"

	/*
	 * block comments
	 */

	 // doc comment for foo
	"github.com/homuler/foo"
	"context"
	// footer comment (maybe for context)

	"strings" // line comment for strings

	b "github.com/homuler/bar"

	_ "runtime/pprof" // line comment for pprof
)

Output

import (
	"context"
	// footer comment (maybe for context)

	// doc comment for fmt
	"fmt"
	_ "runtime/pprof" // line comment for pprof
	"strings"         // line comment for strings

	/*
	 * block comments
	 */

	b "github.com/homuler/bar"
	// doc comment for foo
	"github.com/homuler/foo"
)
goimports
import (
	// doc comment for fmt
	"fmt"

	/*
	 * block comments
	 */

	// doc comment for foo
	"context"

	"github.com/homuler/foo"

	// footer comment (maybe for context)

	"strings" // line comment for strings

	b "github.com/homuler/bar"

	_ "runtime/pprof" // line comment for pprof
)

Merging Problem

Input

// doc comment for fmt
import "fmt"

// doc comment for context
import "context"

// doc comment
import (
	// doc comment for errors
	"errors"
)

Output

goimports-wasabi

// doc comment
import (
	// doc comment for context
	"context"
	// doc comment for errors
	"errors"
	// doc comment for fmt
	"fmt"
)
goimports
// doc comment for fmt
import (
	"context"
	"errors"
	"fmt"
)

// doc comment for context

// doc comment

// doc comment for errors

Unusual Input

Input

import ("fmt";"fmt";"context";"github.com/homuler/foo")

Output

goimports-wasabi -format-only

import (
	"context"
	"fmt"

	"github.com/homuler/foo"
)
goimports -format-only
panic: invalid line number 2 (should be < 2)

License

BSD-3-Clause

Copyright (c) 2009 The Go Authors. All rights reserved.

About

goimports fork, which works deterministically, keeping as many comments as possible.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages