Skip to content

Commit 783c312

Browse files
authored
Update README.md (#548)
1 parent b857715 commit 783c312

1 file changed

Lines changed: 71 additions & 25 deletions

File tree

README.md

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,69 @@ These are some of the projects using Prettier Solidity:
5151
- [UMA](https://umaproject.org/)
5252
- [Uniswap](https://uniswap.org)
5353

54+
## Configuration File
55+
56+
Prettier provides a flexible system to configure the formatting rules of a project. For more information please refer to the [documentation](https://prettier.io/docs/en/configuration.html).
57+
The following is the default configuration internally used by this plugin.
58+
59+
```json
60+
{
61+
"overrides": [
62+
{
63+
"files": "*.sol",
64+
"options": {
65+
"printWidth": 80,
66+
"tabWidth": 4,
67+
"useTabs": false,
68+
"singleQuote": false,
69+
"bracketSpacing": false,
70+
"explicitTypes": "always"
71+
}
72+
}
73+
]
74+
}
75+
```
76+
77+
Note the use of the [overrides property](https://prettier.io/docs/en/configuration.html#configuration-overrides) which allows for multiple configurations in case there are other languages in the project (i.e. JavaScript, JSON, Markdown).
78+
79+
Most options are described in Prettier's [documentation](https://prettier.io/docs/en/options.html).
80+
81+
### Explicit Types
82+
83+
Solidity provides the aliases `uint` and `int` for `uint256` and `int256` respectively.
84+
Multiple developers will have different coding styles and prefer one over another.
85+
This option was added to standardize the code across a project and enforce the usage of one alias over another.
86+
87+
Valid options:
88+
89+
- `"always"`: Prefer the explicit types `uint256`, `int256`.
90+
- `"never"`: Prefer the type aliases `uint`, `int`.
91+
- `"preserve"`: Respect the type used by the developer.
92+
93+
| Default | CLI Override | API Override |
94+
| ---------- | -------------------------------------------- | -------------------------------------------- |
95+
| `"always"` | `--explicit-types <always\|never\|preserve>` | `explicitTypes: "<always\|never\|preserve>"` |
96+
97+
```Solidity
98+
// Input
99+
uint public a;
100+
int256 public b;
101+
102+
// "explicitTypes": "always"
103+
uint256 public a;
104+
int256 public b;
105+
106+
// "explicitTypes": "never"
107+
uint public a;
108+
int public b;
109+
110+
// "explicitTypes": "preserve"
111+
uint public a;
112+
int256 public b;
113+
```
114+
115+
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.
116+
54117
## Integrations
55118

56119
### Vim
@@ -100,13 +163,15 @@ Now Prettier will be run every time the file is saved.
100163

101164
### VSCode
102165

103-
VSCode is not familiar with the solidity language, so [`solidity support`](https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity) needs to be installed.
166+
VSCode is not familiar with the solidity language, so [`solidity`](https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity) support needs to be installed.
104167

105168
```Bash
106169
code --install-extension JuanBlanco.solidity
107170
```
108171

109-
Having done that you should proceed to install [`prettier-vscode`](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode).
172+
This extension provides basic integration with Prettier for most cases no further action is needed.
173+
174+
If you want more control over other details, you should proceed to install [`prettier-vscode`](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode).
110175

111176
```Bash
112177
code --install-extension esbenp.prettier-vscode
@@ -118,31 +183,12 @@ To interact with 3rd party plugins, `prettier-vscode` will look in the project's
118183
npm install --save-dev prettier prettier-plugin-solidity
119184
```
120185

121-
As a final check, make sure that VSCode is configured to format files on save.
122-
123-
You'll notice now that `prettier` is formatting every time the files are saved but the indentation is using 2 spaces instead of 4. This has been [reported](https://github.com/prettier/prettier-vscode/issues/961) and in the meantime you can use the following configuration in your `.prettierrc` file:
186+
This will allow you to specify the version of the plugin in case you need to freeze the formatting since new versions of this plugin will implement tweaks on the possible formats.
124187

125-
```json
126-
{
127-
"overrides": [
128-
{
129-
"files": "*.sol",
130-
"options": {
131-
"printWidth": 80,
132-
"tabWidth": 4,
133-
"useTabs": false,
134-
"singleQuote": false,
135-
"bracketSpacing": false,
136-
"explicitTypes": "always"
137-
}
138-
}
139-
]
140-
}
141-
```
142-
143-
Note: When you install the npm package `prettier` in your project and create a `.prettierrc` file (which wasn't in your project before this), your VSCode's default settings or rules in `settings.json` are ignored ([prettier/prettier-vscode#1079](https://github.com/prettier/prettier-vscode/issues/1079)).
188+
You'll have to let VSCode what formatter you prefer.
189+
As a final check, make sure that VSCode is configured to format files on save.
144190

145-
If you want a different configuration for your javascript and solidity files, you can add an [overrides property](https://prettier.io/docs/en/configuration.html#configuration-overrides) to your `.prettierrc`.
191+
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](https://github.com/prettier/prettier-vscode/issues/1079)).
146192

147193
## Contributing
148194

0 commit comments

Comments
 (0)