Coder Social home page Coder Social logo

rinchsan / gosimports Goto Github PK

View Code? Open in Web Editor NEW
83.0 2.0 7.0 419 KB

alias goimports='gosimports'

Home Page: https://pkg.go.dev/github.com/rinchsan/gosimports

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%
go goimports alternative

gosimports's Introduction

๐Ÿ˜Ž I'm maintaining ...

๐Ÿ‘พ Public Activities

๐Ÿƒโ€โ™‚๏ธ See more ...

๐Ÿค– Private Activities

๐Ÿค“ I like ...

โš™๏ธ Powered by ...

gosimports's People

Contributors

adonovan avatar bradfitz avatar charlievieth avatar cuishuang avatar dmitshur avatar findleyr avatar gopherbot avatar heschi avatar hyangah avatar james-crowley avatar jba avatar jhump avatar kortschak avatar matloob avatar pjweinbgo avatar renovate-bot avatar renovate[bot] avatar rinchsan avatar rolandshoemaker avatar rsc avatar shogo82148 avatar smasher164 avatar stamblerre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gosimports's Issues

Import removed when package name doesn't match directory name

Thanks for putting together this utility!

I believe I found a bug with how gosimports removes unused imports. If the package name doesn't match the name of the directory a package is imported from, gosimports will mistakenly think it is unused and remove it.

Example:

File being imported:

// project/foo/test.go
package foobar  // NOTE: package name is "foobar", but directory is "foo"

func Test() { ... }

Source file:

package server

import (
	"fmt"

	"github.com/mycompany/repo/foo/foo"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}

gosimports will incorrectly remove this import:

package server

import (
	"fmt"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}

However, if I manually name the import, gosimports will keep the import as desired.

package server

import (
	"fmt"

	foobar "github.com/mycompany/project/foo/foo"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}

imports inside raw string literals are removed

package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello world")
}

const helloWorld = `
package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello world")
}

gets reformated to

package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello world")
}

const helloWorld = `
package main

import (
)

func main() {
	fmt.Println("hello world")
}

There the import is now broken.

Duplicate import when multiple aliases and comment

When importing the same module under different aliases and when the second import has a comment with a right-paren, the second import is duplicated.

Input file:

package foo

import (
	"fmt"
	blah "fmt"  // ) fun
)

func something() {
	fmt.Println(`hi`)
	blah.Println(`hi`)
}

Actual output:

package foo

import (
	"fmt"
	blah "fmt"  // ) fun

	blah "fmt"  // ) fun
)

func something() {
	fmt.Println(`hi`)
	blah.Println(`hi`)
}

This is a pretty degenerate case! We're triggering this only because we have a tool in our repo that detects imports aliased under different names and gosimports is formatting its test file.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Adding in support for s390x and ppc64le

@rinchsan Would you be open to adding support for s390x and ppc64le?

s390x refers to the mainframe architecture on Linux or common referred to Linux on Z. While ppc64le refers to the Power architecture. Both architectures are made by IBM, even though some of it is open source.

From my experience both s390x and ppc64le both have great support in GO and usually cross compile without issue.

I believe the .goreleaser.yml would need to be update to look something like:

project_name: gosimports
changelog:
  skip: true
builds:
  - id: gosimports
    main: ./cmd/gosimports/
    env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
    goarch:
      - amd64
      - arm64
      - s390x
      - ppc64le
    ignore:
      - goos: darwin
        goarch: s390x
      - goos: darwin
        goarch: ppc64le
      - goos: windows
        goarch: s390x
      - goos: windows
        goarch: ppc64le
brews:
  - name: gosimports
    tap:
      owner: rinchsan
      name: homebrew-tap
    url_template: "https://github.com/rinchsan/gosimports/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
    commit_author:
      name: goreleaserbot
      email: [email protected]
    folder: Formula
    homepage: "https://pkg.go.dev/github.com/rinchsan/gosimports/cmd/gosimports"
    description: Command gosimports updates your Go import lines, adding missing ones, removing unreferenced ones, and removing redundant blank lines.
    test: |
      system "#{bin}/gosimports --help"
    install: |
      bin.install "gosimports"

Happy to test and validate s390x and ppc64le support as I have access to the hardware. If there are not issues, I would be happy to open a PR.

@rinchsan Let me know if you are open to the idea!

Support gosimports -version

It would be nice to know which version of gosimports I am using.

Expect something like:

> gosimports -version
v0.1.5

Grouping is broken with aliased imports

Original:

import (
        "time"
 
        "github.com/pkg/errors"
       elasticV5 "gopkg.in/olivere/elastic.v5"

        "internal/go/repo/logs"
 )

Expected:

@@ -11,9 +11,8 @@ import (
        "time"
 
        "github.com/pkg/errors"
       elasticV5 "gopkg.in/olivere/elastic.v5"

        "internal/go/repo/logs"
 )

Actual:

@@ -11,9 +11,8 @@ import (
        "time"
 
        "github.com/pkg/errors"
-       elasticV5 "gopkg.in/olivere/elastic.v5"
-
        "internal/go/repo/logs"
+       elasticV5 "gopkg.in/olivere/elastic.v5"
 )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.