Git Product home page Git Product logo

gocommas's Introduction

gocommas

⚠️ In development ⚠️

A simple tool to find missing trailing commas in Go source code to make it valid.

In several cases, Go requires a trailing comma on the final line before a closing brace, including:

  • composite literals
  • function calls
  • function signature

Because of this, missing commas are considered invalid syntax, and prevent most auto-formatting tools from working.

Example

example.go:

func main() {
	x := []string{
		"1",
		"2",
		"3"
	}
	fmt.Println(
		x
	)
}
$ gofmt -w example.go
example.go:7:6: missing ',' before newline in composite literal
example.go:10:4: missing ',' before newline in argument list

The output file is never written because gofmt never got to the point of formatting it.

Solution

There have been proposals to fix this automatically, but they were rejected because the tools do not have a precedent for converting invalid syntax into valid syntax.

Well, we can still make external tools that do it! So this is exactly what gocommas does: it reads a potentially invalid Go source file and adds commas where it thinks they are needed. In several cases, this is enough for the file to become valid, and it can be passed on to other formatters or the Go compiler.

$ ./gocommas -w  example.go
Missing comma: example.go:7:6
Missing comma: example.go:10:4

example.go:

package main

func main() {
	x := []string{
		"1",
		"2",
		"3",
	}
	fmt.Println(
		x,
	)
}

🎉 Now the file can be built and formatted using your favorite tool!

gocommas's People

Contributors

ian-h-chamberlain avatar

Stargazers

 avatar

Watchers

 avatar

gocommas's Issues

Support stdin as an input source

Either explicitly passed as -, or maybe by default if no source files are passed, it would be good to support stdin as a source like gofmt does.

-w can't work with this, so following in gofmt's steps:

$ echo 'package main; func main(){}' | gofmt -w
error: cannot use -w with standard input

Allow chaining to other formatting tools

A common use case is probably to run this, then another formatter on success. It would be nice to specify another tool (and its args, presumably) to be run upon successful completion, maybe something like this?

gocommas -w foo.go --format-with="gofmt -w"

Also, in the case -w is not used, we probably want to use stdin of the other tool, roughly equivalent to

gocommas foo.go | gofmt

Maybe that shouldn't be supported, and people should just do literally the above for the input/output case?

Some error messages for commas may be different

Example:

go/bin/Ember/server/middleware/selfupgrade.go:21:32: syntax error: unexpected newline, expecting comma or )

It might be necessary to set a list of filters, this particular example comes from the signature of a function definition.

What would really be nice is if we could avoid using the message entirely, but scanner.ErrorList doesn't seem to have any type information (just a message + position).

Another option might be to get error positions and look for missing commas, but that seems like a tricky heuristic to get right so probably just enumerating all the error message possibilities would be easier. Hopefully it's not too many!

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.