prettier-plugin-solidity 1.1.3 → 1.1.4-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -9
- package/dist/standalone.cjs +3 -0
- package/dist/standalone.cjs.map +1 -0
- package/package.json +42 -33
- package/src/binary-operator-printers/arithmetic.js +12 -14
- package/src/binary-operator-printers/assignment.js +4 -6
- package/src/binary-operator-printers/bit.js +2 -2
- package/src/binary-operator-printers/comparison.js +11 -13
- package/src/binary-operator-printers/exponentiation.js +4 -6
- package/src/binary-operator-printers/index.js +7 -13
- package/src/binary-operator-printers/logical.js +12 -14
- package/src/binary-operator-printers/shift.js +2 -2
- package/src/clean.js +1 -1
- package/src/comments/handler.js +7 -15
- package/src/comments/handlers/handleContractDefinitionComments.js +9 -11
- package/src/comments/handlers/handleModifierInvocationComments.js +9 -11
- package/src/comments/handlers/index.js +5 -3
- package/src/comments/ignore.js +1 -1
- package/src/comments/index.js +2 -4
- package/src/comments/printer.js +5 -10
- package/src/common/printer-helpers.js +15 -28
- package/src/common/util.js +30 -20
- package/src/index.js +14 -12
- package/src/loc.js +1 -1
- package/src/nodes/ArrayTypeName.js +1 -3
- package/src/nodes/AssemblyAssignment.js +4 -8
- package/src/nodes/AssemblyBlock.js +6 -11
- package/src/nodes/AssemblyCall.js +2 -4
- package/src/nodes/AssemblyCase.js +1 -3
- package/src/nodes/AssemblyFor.js +4 -8
- package/src/nodes/AssemblyFunctionDefinition.js +6 -11
- package/src/nodes/AssemblyIf.js +1 -3
- package/src/nodes/AssemblyLocalDefinition.js +4 -9
- package/src/nodes/AssemblyMemberAccess.js +1 -3
- package/src/nodes/AssemblyStackAssignment.js +1 -3
- package/src/nodes/AssemblySwitch.js +4 -8
- package/src/nodes/BinaryOperation.js +2 -5
- package/src/nodes/Block.js +6 -11
- package/src/nodes/BooleanLiteral.js +1 -3
- package/src/nodes/Break.js +1 -3
- package/src/nodes/BreakStatement.js +1 -3
- package/src/nodes/CatchClause.js +2 -4
- package/src/nodes/Conditional.js +4 -8
- package/src/nodes/Continue.js +1 -3
- package/src/nodes/ContinueStatement.js +1 -3
- package/src/nodes/ContractDefinition.js +11 -17
- package/src/nodes/CustomErrorDefinition.js +2 -4
- package/src/nodes/DecimalNumber.js +1 -3
- package/src/nodes/DoWhileStatement.js +4 -9
- package/src/nodes/ElementaryTypeName.js +1 -3
- package/src/nodes/EmitStatement.js +1 -3
- package/src/nodes/EnumDefinition.js +4 -9
- package/src/nodes/EnumValue.js +1 -3
- package/src/nodes/EventDefinition.js +2 -4
- package/src/nodes/ExpressionStatement.js +4 -9
- package/src/nodes/FileLevelConstant.js +1 -3
- package/src/nodes/ForStatement.js +4 -9
- package/src/nodes/FunctionCall.js +4 -9
- package/src/nodes/FunctionDefinition.js +11 -18
- package/src/nodes/FunctionTypeName.js +4 -9
- package/src/nodes/HexLiteral.js +4 -9
- package/src/nodes/HexNumber.js +1 -3
- package/src/nodes/Identifier.js +1 -3
- package/src/nodes/IfStatement.js +6 -11
- package/src/nodes/ImportDirective.js +12 -17
- package/src/nodes/IndexAccess.js +4 -8
- package/src/nodes/IndexRangeAccess.js +1 -3
- package/src/nodes/InheritanceSpecifier.js +2 -4
- package/src/nodes/InlineAssemblyStatement.js +3 -4
- package/src/nodes/LabelDefinition.js +4 -8
- package/src/nodes/Mapping.js +17 -18
- package/src/nodes/MemberAccess.js +6 -10
- package/src/nodes/ModifierDefinition.js +4 -9
- package/src/nodes/ModifierInvocation.js +3 -5
- package/src/nodes/NameValueExpression.js +1 -3
- package/src/nodes/NameValueList.js +4 -9
- package/src/nodes/NewExpression.js +1 -3
- package/src/nodes/NumberLiteral.js +1 -3
- package/src/nodes/PragmaDirective.js +1 -3
- package/src/nodes/ReturnStatement.js +4 -8
- package/src/nodes/RevertStatement.js +1 -3
- package/src/nodes/SourceUnit.js +4 -9
- package/src/nodes/StateVariableDeclaration.js +4 -8
- package/src/nodes/StringLiteral.js +5 -9
- package/src/nodes/StructDefinition.js +13 -22
- package/src/nodes/ThrowStatement.js +1 -3
- package/src/nodes/TryStatement.js +6 -11
- package/src/nodes/TupleExpression.js +4 -9
- package/src/nodes/TypeDefinition.js +1 -3
- package/src/nodes/UnaryOperation.js +1 -3
- package/src/nodes/UncheckedStatement.js +4 -8
- package/src/nodes/UserDefinedTypeName.js +1 -3
- package/src/nodes/UsingForDeclaration.js +28 -44
- package/src/nodes/VariableDeclaration.js +4 -9
- package/src/nodes/VariableDeclarationStatement.js +6 -11
- package/src/nodes/WhileStatement.js +4 -9
- package/src/nodes/index.js +72 -78
- package/src/options.js +1 -1
- package/src/parser.js +5 -12
- package/src/prettier-comments/language-js/comments.js +13 -23
- package/src/printer.js +6 -6
- package/dist/standalone.js +0 -3
- package/dist/standalone.js.map +0 -1
- package/src/prettier-comments/index.js +0 -3
- /package/dist/{standalone.js.LICENSE.txt → standalone.cjs.LICENSE.txt} +0 -0
package/README.md
CHANGED
|
@@ -10,8 +10,6 @@
|
|
|
10
10
|
|
|
11
11
|
A [Prettier plugin](https://prettier.io/docs/en/plugins.html) for automatically formatting your [Solidity](https://github.com/ethereum/solidity) code.
|
|
12
12
|
|
|
13
|
-
If you like this project, please consider contributing to our [Gitcoin grant](https://gitcoin.co/grants/1534/prettier-solidity)!
|
|
14
|
-
|
|
15
13
|
## Installation and usage
|
|
16
14
|
|
|
17
15
|
### Using in NodeJS
|
|
@@ -25,19 +23,19 @@ npm install --save-dev prettier prettier-plugin-solidity
|
|
|
25
23
|
Run prettier in your contracts:
|
|
26
24
|
|
|
27
25
|
```Bash
|
|
28
|
-
npx prettier --write 'contracts/**/*.sol'
|
|
26
|
+
npx prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol'
|
|
29
27
|
```
|
|
30
28
|
|
|
31
29
|
You can add a script for running prettier on all your contracts:
|
|
32
30
|
|
|
33
31
|
```
|
|
34
|
-
"prettier": "prettier --write 'contracts/**/*.sol'"
|
|
32
|
+
"prettier": "prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol'"
|
|
35
33
|
```
|
|
36
34
|
|
|
37
35
|
Or you can use it as part of your linting to check that all your code is prettified:
|
|
38
36
|
|
|
39
37
|
```
|
|
40
|
-
"lint": "prettier --list-different 'contracts/**/*.sol'"
|
|
38
|
+
"lint": "prettier --list-different --plugin=prettier-plugin-solidity 'contracts/**/*.sol'"
|
|
41
39
|
```
|
|
42
40
|
|
|
43
41
|
> Prettier Solidity only works with valid code. If there is a syntax error, nothing will be done and a parser error will be thrown.
|
|
@@ -61,16 +59,41 @@ We follow Prettier's strategy for populating their plugins in the object `pretti
|
|
|
61
59
|
|
|
62
60
|
```html
|
|
63
61
|
<script>
|
|
62
|
+
async function format(code) {
|
|
63
|
+
return await prettier.format(code, {
|
|
64
|
+
parser: 'solidity-parse',
|
|
65
|
+
plugins: [solidityPlugin]
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
64
69
|
const originalCode = 'contract Foo {}';
|
|
65
|
-
const formattedCode =
|
|
66
|
-
parser: 'solidity-parse',
|
|
67
|
-
plugins: prettierPlugins
|
|
68
|
-
});
|
|
70
|
+
const formattedCode = format(originalCode);
|
|
69
71
|
</script>
|
|
70
72
|
```
|
|
71
73
|
|
|
72
74
|
For more details and please have a look at [Prettier's documentation](https://prettier.io/docs/en/browser.html).
|
|
73
75
|
|
|
76
|
+
### Creating a package for the Browser
|
|
77
|
+
|
|
78
|
+
_Added in v1.1.4_
|
|
79
|
+
|
|
80
|
+
If you are creating your own package to be run in a browser, you might want to import the standalone files directly.
|
|
81
|
+
|
|
82
|
+
```Javascript
|
|
83
|
+
import prettier from 'prettier/standalone';
|
|
84
|
+
import solidityPlugin from 'prettier-plugin-solidity/standalone';
|
|
85
|
+
|
|
86
|
+
async function format(code) {
|
|
87
|
+
return await prettier.format(code, {
|
|
88
|
+
parser: "solidity-parse",
|
|
89
|
+
plugins: [solidityPlugin],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const originalCode = 'contract Foo {}';
|
|
94
|
+
const formattedCode = format(originalCode);
|
|
95
|
+
```
|
|
96
|
+
|
|
74
97
|
## Configuration File
|
|
75
98
|
|
|
76
99
|
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).
|
|
@@ -78,10 +101,12 @@ The following is the default configuration internally used by this plugin.
|
|
|
78
101
|
|
|
79
102
|
```JSON
|
|
80
103
|
{
|
|
104
|
+
"plugins": ["prettier-plugin-solidity"],
|
|
81
105
|
"overrides": [
|
|
82
106
|
{
|
|
83
107
|
"files": "*.sol",
|
|
84
108
|
"options": {
|
|
109
|
+
"parser": "solidity-parse",
|
|
85
110
|
"printWidth": 80,
|
|
86
111
|
"tabWidth": 4,
|
|
87
112
|
"useTabs": false,
|
|
@@ -95,6 +120,8 @@ The following is the default configuration internally used by this plugin.
|
|
|
95
120
|
|
|
96
121
|
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).
|
|
97
122
|
|
|
123
|
+
Since Prettier v3.0.0, the plugin search feature has been removed so we encourage adding our plugin to the configuration file.
|
|
124
|
+
|
|
98
125
|
Most options are described in Prettier's [documentation](https://prettier.io/docs/en/options.html).
|
|
99
126
|
|
|
100
127
|
### Compiler
|
|
@@ -297,6 +324,18 @@ Now VSCode's `settings.json` should have this:
|
|
|
297
324
|
|
|
298
325
|
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](https://github.com/prettier/prettier-vscode/issues/1079)).
|
|
299
326
|
|
|
327
|
+
### Pnpm
|
|
328
|
+
|
|
329
|
+
To make Prettier Solidity work in your project, you have to add a `.prettierrc` file as shown [here](#configuration-file).
|
|
330
|
+
|
|
331
|
+
Then, if you are using VSCode, you also need to add this to your VSCode settings:
|
|
332
|
+
|
|
333
|
+
```json
|
|
334
|
+
{
|
|
335
|
+
"prettier.documentSelectors": ["**/*.sol"]
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
300
339
|
## Edge cases
|
|
301
340
|
|
|
302
341
|
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.
|