Skip to content

Commit a782b28

Browse files
authored
avoid calling coerce multiple times and only do it once (#1185)
* avoid calling coerce multiple times and only do it once at the beginning of the parse * fail check faster * updating README.md
1 parent a787a1a commit a782b28

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@ The Solidity versions taken into consideration during formatting are:
143143

144144
```Solidity
145145
// Input
146-
import { Foo as Bar } from "/an/extremely/long/location";
146+
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";
147147
148-
// "compiler": undefined
149-
import { Foo as Bar } from "/an/extremely/long/location";
148+
// "compiler": undefined, parser: "antlr"
149+
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";
150150
151151
// "compiler": "0.7.3" (or lesser)
152-
import { Foo as Bar } from "/an/extremely/long/location";
152+
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";
153153
154154
// "compiler": "0.7.4" (or greater)
155155
import {
156-
Foo as Bar
156+
Foo as Bar,
157+
Baz as Qux
157158
} from "/an/extremely/long/location";
158159
```
159160

@@ -208,9 +209,9 @@ You might have a multi-version project, where different files are compiled with
208209
}
209210
```
210211

211-
| Default | CLI Override | API Override |
212-
| ------- | --------------------- | ---------------------- |
213-
| None | `--compiler <string>` | `compiler: "<string>"` |
212+
| Default | CLI Override | API Override |
213+
| --------------------------------------------------------------------------------------------- | --------------------- | ---------------------- |
214+
| Inferred from pragma statements when using parser `slang`<br/> None when using parser `antlr` | `--compiler <string>` | `compiler: "<string>"` |
214215

215216
### Parser
216217

src/slang-nodes/ContractDefinition.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { doc } from 'prettier';
2-
import { coerce, satisfies } from 'semver';
2+
import { satisfies } from 'semver';
33
import { NonterminalKind } from '@nomicfoundation/slang/cst';
44
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
55
import { Identifier } from './Identifier.js';
@@ -47,8 +47,7 @@ export class ContractDefinition implements SlangNode {
4747
cleanModifierInvocationArguments(options: ParserOptions<AstNode>): void {
4848
// Older versions of Solidity defined a constructor as a function having
4949
// the same name as the contract.
50-
const compiler = coerce(options.compiler);
51-
if (compiler && !satisfies(compiler, '>=0.5.0')) {
50+
if (!satisfies(options.compiler, '>=0.5.0')) {
5251
for (const { variant: member } of this.members.items) {
5352
if (
5453
member.kind === NonterminalKind.FunctionDefinition &&

src/slang-nodes/FunctionDefinition.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { coerce, satisfies } from 'semver';
1+
import { satisfies } from 'semver';
22
import { NonterminalKind } from '@nomicfoundation/slang/cst';
33
import { printFunction } from '../slang-printers/print-function.js';
44
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
@@ -54,8 +54,7 @@ export class FunctionDefinition implements SlangNode {
5454

5555
// Older versions of Solidity defined a constructor as a function having
5656
// the same name as the contract.
57-
const compiler = coerce(options.compiler);
58-
if (compiler && satisfies(compiler, '>=0.5.0')) {
57+
if (satisfies(options.compiler, '>=0.5.0')) {
5958
this.cleanModifierInvocationArguments();
6059
}
6160
}

src/slang-nodes/ImportDeconstructionSymbols.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { doc } from 'prettier';
2-
import { coerce, satisfies } from 'semver';
2+
import { satisfies } from 'semver';
33
import { NonterminalKind } from '@nomicfoundation/slang/cst';
44
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
55
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
@@ -38,22 +38,22 @@ export class ImportDeconstructionSymbols implements SlangNode {
3838
print(
3939
path: AstPath<ImportDeconstructionSymbols>,
4040
print: PrintFunction,
41-
options: ParserOptions<AstNode>
41+
{ compiler, bracketSpacing }: ParserOptions<AstNode>
4242
): Doc {
43-
const compiler = coerce(options.compiler);
43+
const items = path.map(print, 'items');
4444
return printSeparatedList(
45-
path.map(print, 'items'),
46-
compiler && satisfies(compiler, '>=0.7.4') && this.items.length > 1
45+
items,
46+
items.length > 1 && satisfies(compiler, '>=0.7.4')
4747
? {
4848
// if the compiler exists and is greater than or equal to 0.7.4 we will
4949
// split the ImportDirective.
50-
firstSeparator: options.bracketSpacing ? line : softline,
50+
firstSeparator: bracketSpacing ? line : softline,
5151
separator: [',', line]
5252
}
5353
: {
5454
// if the compiler is not given or is lower than 0.7.4 we will not
5555
// split the ImportDirective.
56-
firstSeparator: options.bracketSpacing ? ' ' : '',
56+
firstSeparator: bracketSpacing ? ' ' : '',
5757
separator: ', '
5858
}
5959
);

0 commit comments

Comments
 (0)