NOTE
This is the fork of the Ethereum-solidity plugin for prettier. This plugin works with Everscale Solidity (or ton-solidity). Works with .sol
and .tsol
files. It's not compatible with Ethereum Solidity.
A Prettier plugin for automatically formatting your Everscale Solidity code.
Install both prettier
and prettier-plugin-solidity
:
npm install --save-dev prettier prettier-plugin-tvmsolidity
Run prettier in your contracts:
- Format all contracts
npx prettier --write --plugin=prettier-plugin-tvmsolidity 'contracts/**/*.{tsol,sol}'
- Format concrete contract
npx prettier --write --plugin=prettier-plugin-tvmsolidity 'fileName.{tsol,sol}'
You can add a script to your package.json for running prettier on all your contracts:
"scripts": {
"format": "prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.{tsol,sol}'"
}
You can add a script to your package.json for running prettier on all your contracts:
"lint": "prettier --list-different --plugin=prettier-plugin-solidity 'contracts/**/*.{tsol,sol}'"
Prettier Solidity only works with valid code. If there is a syntax error, nothing will be done and a parser error will be thrown.
Added in v1.1.0
To use this package in the browser, you need to load Prettier's standalone bundle before loading the build provided in this package.
<script src="https://unpkg.com/prettier@latest"></script>
<script src="https://unpkg.com/prettier-plugin-solidity@latest"></script>
Prettier's unpkg field points to https://unpkg.com/prettier/standalone.js
, in a similar way this plugin points to https://unpkg.com/prettier-plugin-solidity/dist/standalone.js
.
Once the scripts are loaded you will have access the globals prettier
and prettierPlugins
.
We follow Prettier's strategy for populating their plugins in the object prettierPlugins
, you can load other plugins like https://unpkg.com/[email protected]/parser-markdown.js
and Prettier will have access to multiple parsers.
<script>
const originalCode = 'contract Foo {}';
const formattedCode = prettier.format(originalCode, {
parser: 'solidity-parse',
plugins: prettierPlugins
});
</script>
For more details and please have a look at Prettier's documentation.
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.
{
"plugins": ["prettier-plugin-tvmsolidity"],
"overrides": [
{
"files": "*.{tsol,sol}",
"options": {
"printWidth": 80,
"useTabs": true,
"singleQuote": false,
"bracketSpacing": false,
}
}
]
}
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).
Since Prettier v3.0.0, the plugin search feature has been removed so we encourage adding our plugin to the configuration file.
Most options are described in Prettier's documentation.
"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
To interact with 3rd party plugins, prettier-vscode
will look in the project's npm modules, so you'll need to have prettier
and prettier-plugin-solidity
in your package.json
npm install --save-dev prettier prettier-plugin-solidity
This will allow you to specify the version of the plugin in case you want to use the latest version of the plugin or need to freeze the formatting since new versions of this plugin will implement tweaks on the possible formats.
Note: By design, Prettier prioritizes a local over a global configuration. If you have a .prettierrc
file in your project, your VSCode's default settings or rules in settings.json
are ignored (prettier/prettier-vscode#1079).
To make Prettier Solidity work in your project, you have to add a .prettierrc
file as shown here.
Then, if you are using VSCode, you also need to add this to your VSCode settings:
{
"prettier.documentSelectors": ["**/*.{tsol,sol}"]
}
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.