ever-guild / prettier-plugin-tvm-solidity

A Prettier plugin for automatically formatting your TVM Solidity code
https://t.me/EverscaleSmartContracts
MIT License
0 stars 0 forks source link
everscale

prettier-plugin-solidity


NOTE

This is the fork of the Ethereum-solidity plugin for prettier. This plugin works with Everscale Solidity (or ton-solidity)


Telegram

A Prettier plugin for automatically formatting your Everscale Solidity code.

Installation and usage

Install both prettier and prettier-plugin-solidity:

npm install --save-dev prettier github:pizza-777/prettier-plugin-solidity

Run prettier in your contracts:

  1. Format all contracts
npx prettier --write 'contracts/**/*.sol'
  1. Format concrete contract
npx prettier --write 'fileName.sol'

You can add a script to your package.json for running prettier on all your contracts:

"scripts": {
   "format": "prettier --write 'contracts/**/*.sol'"
  }

You can add a script to your package.json for running prettier on all your contracts:

"lint": "prettier --list-different 'contracts/**/*.sol'"

Configuration File

Prettier provides a flexible system to configure the formatting rules of a project. For more information please refer to the documentation. The following is the default configuration internally used by this plugin.

{
  "overrides": [
    {
      "files": "*.sol",
      "options": {
        "printWidth": 80,        
        "useTabs": true,
        "singleQuote": false,
        "bracketSpacing": false,
        "explicitTypes": "always"
      }
    }
  ]
}

Note the use of the overrides property which allows for multiple configurations in case there are other languages in the project (i.e. JavaScript, JSON, Markdown).

Most options are described in Prettier's documentation.

Explicit Types

Solidity provides the aliases uint and int for uint256 and int256 respectively. Multiple developers will have different coding styles and prefer one over another. This option was added to standardize the code across a project and enforce the usage of one alias over another.

Valid options:

Default CLI Override API Override
"always" --explicit-types <always\|never\|preserve> explicitTypes: "<always\|never\|preserve>"
// Input
uint public a;
int256 public b;

// "explicitTypes": "always"
uint256 public a;
int256 public b;

// "explicitTypes": "never"
uint public a;
int public b;

// "explicitTypes": "preserve"
uint public a;
int256 public b;

Note: switching between uint and uint256 does not alter the bytecode at all and we have implemented tests for this. However, there will be a change in the AST reflecting the switch.

Integrations

VSCode

VSCode is not familiar with the Everscale Solidity language, so Everscale solidity support needs to be installed.

code --install-extension everscale.solidity-support

You can format a file with the context menu:

This extension provides basic integration with Prettier for most cases no further action is needed.

Make sure your editor has format on save set to true. When you save VSCode will ask you what formatter would you like to use for the solidity language, you can choose everscale.solidity-support.

At this point VSCode's settings.json should have a configuration similar to this:

{
  "solidity.formatter": "prettier", // This is the default so it might be missing.
  "[ton-solidity]": {
    "editor.defaultFormatter": "everscale.solidity-support"
  },
}

If you want more control over other details, you should proceed to install prettier-vscode.

code --install-extension esbenp.prettier-vscode

Note: By design, Prettier prioritizes a local over a global configuration. If you have a .prettierrc file in your porject, your VSCode's default settings or rules in settings.json are ignored (prettier/prettier-vscode#1079).

Edge cases

Prettier Solidity does its best to be pretty and consistent, but in some cases it falls back to doing things that are less than ideal.

Modifiers in constructors

Modifiers with no arguments are formatted with their parentheses removed, except for constructors. The reason for this is that Prettier Solidity cannot always tell apart a modifier from a base constructor. So modifiers in constructors are not modified. For example, this:

contract Foo is Bar {
  constructor() Bar() modifier1 modifier2() modifier3(42) {}

  function f() modifier1 modifier2() modifier3(42) {}
}

will be formatted as

contract Foo is Bar {
  constructor() Bar() modifier1 modifier2() modifier3(42) {}

  function f() modifier1 modifier2 modifier3(42) {}
}

Notice that the unnecessary parentheses in modifier2 were removed in the function but not in the constructor.

Contributing

This fork is based on two others:

License

Distributed under the MIT license. See LICENSE for more information.