Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ The Solidity versions taken into consideration during formatting are:

```Solidity
// Input
import { Foo as Bar } from "/an/extremely/long/location";
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";

// "compiler": undefined
import { Foo as Bar } from "/an/extremely/long/location";
// "compiler": undefined, parser: "antlr"
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";

// "compiler": "0.7.3" (or lesser)
import { Foo as Bar } from "/an/extremely/long/location";
import { Foo as Bar, Baz as Qux } from "/an/extremely/long/location";

// "compiler": "0.7.4" (or greater)
import {
Foo as Bar
Foo as Bar,
Baz as Qux
} from "/an/extremely/long/location";
```

Expand Down Expand Up @@ -208,9 +209,9 @@ You might have a multi-version project, where different files are compiled with
}
```

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

### Parser

Expand Down
5 changes: 2 additions & 3 deletions src/slang-nodes/ContractDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { doc } from 'prettier';
import { coerce, satisfies } from 'semver';
import { satisfies } from 'semver';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
import { Identifier } from './Identifier.js';
Expand Down Expand Up @@ -47,8 +47,7 @@ export class ContractDefinition implements SlangNode {
cleanModifierInvocationArguments(options: ParserOptions<AstNode>): void {
// Older versions of Solidity defined a constructor as a function having
// the same name as the contract.
const compiler = coerce(options.compiler);
if (compiler && !satisfies(compiler, '>=0.5.0')) {
if (!satisfies(options.compiler, '>=0.5.0')) {
for (const { variant: member } of this.members.items) {
if (
member.kind === NonterminalKind.FunctionDefinition &&
Expand Down
5 changes: 2 additions & 3 deletions src/slang-nodes/FunctionDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coerce, satisfies } from 'semver';
import { satisfies } from 'semver';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { printFunction } from '../slang-printers/print-function.js';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
Expand Down Expand Up @@ -54,8 +54,7 @@ export class FunctionDefinition implements SlangNode {

// Older versions of Solidity defined a constructor as a function having
// the same name as the contract.
const compiler = coerce(options.compiler);
if (compiler && satisfies(compiler, '>=0.5.0')) {
if (satisfies(options.compiler, '>=0.5.0')) {
this.cleanModifierInvocationArguments();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/slang-nodes/ImportDeconstructionSymbols.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { doc } from 'prettier';
import { coerce, satisfies } from 'semver';
import { satisfies } from 'semver';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
Expand Down Expand Up @@ -38,22 +38,22 @@ export class ImportDeconstructionSymbols implements SlangNode {
print(
path: AstPath<ImportDeconstructionSymbols>,
print: PrintFunction,
options: ParserOptions<AstNode>
{ compiler, bracketSpacing }: ParserOptions<AstNode>
): Doc {
const compiler = coerce(options.compiler);
const items = path.map(print, 'items');
return printSeparatedList(
path.map(print, 'items'),
compiler && satisfies(compiler, '>=0.7.4') && this.items.length > 1
items,
items.length > 1 && satisfies(compiler, '>=0.7.4')
? {
// if the compiler exists and is greater than or equal to 0.7.4 we will
// split the ImportDirective.
firstSeparator: options.bracketSpacing ? line : softline,
firstSeparator: bracketSpacing ? line : softline,
separator: [',', line]
}
: {
// if the compiler is not given or is lower than 0.7.4 we will not
// split the ImportDirective.
firstSeparator: options.bracketSpacing ? ' ' : '',
firstSeparator: bracketSpacing ? ' ' : '',
separator: ', '
}
);
Expand Down