28

Most programming languages have tools to format source code according to convention: for example, Python has autopep8 and yapf, and Go has gofmt.

Does a similar tool exist for Solidity source code, ideally according to the Solidity Style Guide?

3
  • It may be better to ask this on softwarerecs.stackexchange.com
    – Chenmunka
    Apr 29, 2016 at 12:32
  • 1
    Would be on-topic for both sites. I like it.
    – q9f
    Apr 29, 2016 at 13:08
  • 1
    would love to see this in JetBrains suite, specifically PyCharm !
    – euri10
    Jun 15, 2016 at 8:45

8 Answers 8

17

I've recently published a Linter for Solidity which aims to comply the the official Style Guide.

Here's the repo

The architecture is pretty similar to that of ESLint so I'm trying to make it as easy-to-use and customizable (including plugging in of custom rules) as ESLint is.

Its under active development right now.

To get started, you could install it via npm:

npm install -g solium

Browse to the root directory of your project and run

solium --init

This creates .soliumrc.json (determines which rules to enable and handles plugging in of custom rules) & .soliumignore (specifies which files and folders to ignore)

To run the linter on a single file, simply use: solium --file path/to/myfile.sol

To lint over your entire project (all .sol files), use:

solium

OR

solium --hot to enable hot reloading.

Hopefully, this improves workflow & developer productivity, since I've been facing a lot of trouble developing for Ethereum platform, because of the lack of dev tools. Hope you find it useful!

4
  • 3
    Thanks for sharing this! Does Solium only do linting, or can it reformat files too?
    – Pi Delport
    Aug 17, 2016 at 13:09
  • 1
    @PiDelport I'm not sure what you mean by reformatting. Do you mean fixing the lint errors automatically? (If yes, then) I'm currently in the process of implementing a fix mechanism, so in a week or so, yah, it will support fixing.
    – Raghav Dua
    Aug 17, 2016 at 15:28
  • Basically that, yes: reading the code in, and then reformatting it back out according to the style guide (like autopep8 and yapf, gofmt, and so on).
    – Pi Delport
    Aug 18, 2016 at 18:13
  • Is there any prettier plugin for VS code, like there is for javascript. I am facing this problem. I get error all the time for the indentation in my solidity code not adhering to the standard ( 4 spaces ) Sep 30, 2018 at 14:43
7

There's a work in progress towards a plugin for prettier that formats solidity code: https://github.com/prettier-solidity/prettier-plugin-solidity As with prettier, the goal is to have an opinionated tool that takes your code, builds its AST and prints it again.

(Disclaimer: I'm one of the contributors)

1
  • 1
    I think this is more than WIP now! Just tried it out and it worked flawlessly. Jul 19, 2019 at 23:47
4

If you use atom.io there is a linter: https://atom.io/packages/linter-solidity

And for syntax highlighting goodies: https://atom.io/packages/language-ethereum

6
  • Thanks! That only shows lint output, though, and doesn't format code, right?
    – Pi Delport
    Jun 15, 2016 at 11:28
  • updated to add a code formatter
    – firescar96
    Jun 16, 2016 at 18:31
  • The highlighter performs syntax highlighting, but also doesn't format code.
    – Pi Delport
    Jun 17, 2016 at 8:21
  • You're right, I thought it was that package, but it was another package:atom.io/packages/atom-beautify which I configured for solidity.
    – firescar96
    Jun 17, 2016 at 18:12
  • Ah, how did you configure it?
    – Pi Delport
    Jun 18, 2016 at 23:00
3

https://github.com/alexstep/SublimeLinter-contrib-solium

Solium linter plugin for sublime text

3

The 2023 answer is to use forge fmt. https://book.getfoundry.sh/reference/config/formatter

Do that once and be liberated from this issue

2

This is available now for IntelliJ (and related IDEs):

https://plugins.jetbrains.com/plugin/9475-intellij-solidity

1

This is a linter that provides security, style and best practice validations.

You may install it with

npm install -g solhint

For validation you need put in arguments glob expression of path to your code

solhint *.sol

I believe this tool with be useful for your project!

2
  • welcome to the community :) look here for more guide on formatting Sep 27, 2017 at 11:50
  • Solhint is awesome, but OP asked for a formatter not a linter. Jul 8, 2018 at 22:18
1

You can use YAKINDU Soliditiy Tools, its open source and it has some other interesting features even if it is currently in beta phase.

https://medium.com/solidity-ide/yakindu-solidity-tools-beta-released-dbcc76307bc0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.