NOTE
This is the fork of the Ethereum-solidity plugin for prettier. This plugin works with Everscale Solidity (or ton-solidity)
A Prettier plugin for automatically formatting your Everscale Solidity code.
Install both prettier
and prettier-plugin-solidity
:
npm install --save-dev prettier github:pizza-777/prettier-plugin-solidity
Run prettier in your contracts:
npx prettier --write 'contracts/**/*.sol'
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'"
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.
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:
"always"
: Prefer explicit types (uint256
, int256
, etc.)"never"
: Prefer type aliases (uint
, int
, etc.)."preserve"
: Respect the type used by the developer.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.
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).
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 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.
This fork is based on two others:
Distributed under the MIT license. See LICENSE for more information.