Skip to content

Commit 1118a10

Browse files
Jantherfvictorio
andauthored
Compiler option (#535)
* adding solidity versions to options * visit each PragmaDirective on parse and infer the compiler * recreate logic for byte and bytes if we are on a compiler that supports them * adding tests for the scenarios * not splitting imports if we are in version before 0.7.4 * No need to run eslint with json files * refactor of solc.json to a leaner array * reorganising tests to be more descriptive * dependency updates * Keeping the documentation for the ImportDirective * added more documentation on the full system. * moving tests to new tests/format folder * implementing feedback from Franco - compiler should not be inferred - safe formats should be used if no compiler is provided * spell checking * Not needed anymore * addressing feedback * missing dot * semver.coerce(options.compiler) is much simpler than having a dedicated util for that * updating dependencies * adding documentation for compiler option * Update src/parser.js Co-authored-by: Franco Victorio <[email protected]> * Move the "Explicit Types" option before the "Compiler" one * Mark the compiler option as experimental and rewrite its description * Update README.md Co-authored-by: Franco Victorio <[email protected]>
1 parent 56b8d87 commit 1118a10

28 files changed

Lines changed: 2419 additions & 1465 deletions

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ typings/
6262

6363
.DS_Store
6464

65-
# package-lock.json
66-
65+
# yarn 2 config
6766
yarn.lock
67+
.yarn
68+
.yarnrc.yml
69+
70+
# vscode config
71+
.vscode

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In the interest of fostering an open and welcoming environment, we as
66
contributors and maintainers pledge to making participation in our project and
77
our community a harassment-free experience for everyone, regardless of age, body
88
size, disability, ethnicity, sex characteristics, gender identity and expression,
9-
level of experience, education, socio-economic status, nationality, personal
9+
level of experience, education, socioeconomic status, nationality, personal
1010
appearance, race, religion, or sexual identity and orientation.
1111

1212
## Our Standards

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,95 @@ uint public a;
112112
int256 public b;
113113
```
114114

115+
Note: if the compiler option is provided and is lesser than 0.8.0, explicitTypes will also consider the alias `byte` for the explicit type `bytes1`.
116+
115117
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.
116118

119+
### Compiler (experimental)
120+
121+
Many versions of the Solidity compiler have changes that affect how the code should be formatted. This plugin, by default, tries to format the code in the most compatible way that it's possible, but you can use the experimental `compiler` option to nudge it in the right direction.
122+
123+
One example of this are import directives. Before `0.7.4`, the compiler didn't accept multi-line import statements, so we always format them in a single line. But if you use the `compiler` option to indicate that you are using a version greater or equal than `0.7.4`, the plugin will use multi-line imports when it makes sense.
124+
125+
The solidity versions taken into consideration during formatting are:
126+
127+
- `v0.7.4`: Versions prior `0.7.4` had a bug that would not interpret correctly imports unless they are formatted in a single line.
128+
129+
```Solidity
130+
// Input
131+
import { Foo as Bar } from "/an/extremely/long/location";
132+
133+
// "compiler": undefined
134+
import { Foo as Bar } from "/an/extremely/long/location";
135+
136+
// "compiler": "0.7.3" (or lesser)
137+
import { Foo as Bar } from "/an/extremely/long/location";
138+
139+
// "compiler": "0.7.4" (or greater)
140+
import {
141+
Foo as Bar
142+
} from "/an/extremely/long/location";
143+
```
144+
145+
- `v0.8.0`: Introduced these [changes](https://docs.soliditylang.org/en/v0.8.0/080-breaking-changes.html)
146+
147+
- The type `byte` has been removed. It was an alias of `bytes1`.
148+
- Exponentiation is right associative, i.e., the expression `a**b**c` is parsed as `a**(b**c)`. Before 0.8.0, it was parsed as `(a**b)**c`.
149+
150+
```Solidity
151+
// Input
152+
bytes1 public a;
153+
byte public b;
154+
155+
uint public c = 1 ** 2 ** 3;
156+
157+
// "compiler": undefined
158+
// "explicitTypes": "never"
159+
bytes1 public a;
160+
bytes1 public b;
161+
162+
uint public c = 1 ** 2 ** 3;
163+
164+
// "compiler": "0.7.6" (or lesser)
165+
// "explicitTypes": "never"
166+
byte public a;
167+
byte public b;
168+
169+
uint public c = (1**2)**3;
170+
171+
// "compiler": "0.8.0" (or greater)
172+
// "explicitTypes": "never"
173+
bytes1 public a;
174+
bytes1 public b;
175+
176+
uint public c = 1**(2**3);
177+
```
178+
179+
You might have a multi-version project, where different files are compiled with different compilers. If that's the case, you can use [overrides](https://prettier.io/docs/en/configuration.html#configuration-overrides) to have a more granular configuration:
180+
181+
```
182+
{
183+
"overrides": [
184+
{
185+
"files": "contracts/v1/**/*.sol",
186+
"options": {
187+
"compiler": "0.6.3"
188+
}
189+
},
190+
{
191+
"files": "contracts/v2/**/*.sol",
192+
"options": {
193+
"compiler": "0.8.4"
194+
}
195+
}
196+
]
197+
}
198+
```
199+
200+
| Default | CLI Override | API Override |
201+
| ------- | --------------------- | ---------------------- |
202+
| None | `--compiler <string>` | `compiler: "<string>"` |
203+
117204
## Integrations
118205

119206
### Vim

0 commit comments

Comments
 (0)