From eaba8653ef3956c279e0531fe0bb40dc5dae6ec2 Mon Sep 17 00:00:00 2001 From: Klaus Date: Sat, 28 Jun 2025 11:20:32 +0100 Subject: [PATCH 1/3] avoid calling coerce multiple times and only do it once at the beginning of the parse --- src/slang-nodes/ContractDefinition.ts | 5 ++--- src/slang-nodes/FunctionDefinition.ts | 5 ++--- src/slang-nodes/ImportDeconstructionSymbols.ts | 14 +++++++------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/slang-nodes/ContractDefinition.ts b/src/slang-nodes/ContractDefinition.ts index cfea4cf2c..c158eb0bf 100644 --- a/src/slang-nodes/ContractDefinition.ts +++ b/src/slang-nodes/ContractDefinition.ts @@ -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'; @@ -47,8 +47,7 @@ export class ContractDefinition implements SlangNode { cleanModifierInvocationArguments(options: ParserOptions): 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 && diff --git a/src/slang-nodes/FunctionDefinition.ts b/src/slang-nodes/FunctionDefinition.ts index 572f995ba..95eaefd89 100644 --- a/src/slang-nodes/FunctionDefinition.ts +++ b/src/slang-nodes/FunctionDefinition.ts @@ -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'; @@ -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(); } } diff --git a/src/slang-nodes/ImportDeconstructionSymbols.ts b/src/slang-nodes/ImportDeconstructionSymbols.ts index 10f271afc..1c77cf76c 100644 --- a/src/slang-nodes/ImportDeconstructionSymbols.ts +++ b/src/slang-nodes/ImportDeconstructionSymbols.ts @@ -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'; @@ -38,22 +38,22 @@ export class ImportDeconstructionSymbols implements SlangNode { print( path: AstPath, print: PrintFunction, - options: ParserOptions + { compiler, bracketSpacing }: ParserOptions ): 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, + satisfies(compiler, '>=0.7.4') && items.length > 1 ? { // 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: ', ' } ); From 66d715314e35e8294c94ad3f8207c10ec9c1d39b Mon Sep 17 00:00:00 2001 From: Klaus Date: Fri, 11 Jul 2025 17:19:53 +0100 Subject: [PATCH 2/3] fail check faster --- src/slang-nodes/ImportDeconstructionSymbols.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slang-nodes/ImportDeconstructionSymbols.ts b/src/slang-nodes/ImportDeconstructionSymbols.ts index 1c77cf76c..738f89629 100644 --- a/src/slang-nodes/ImportDeconstructionSymbols.ts +++ b/src/slang-nodes/ImportDeconstructionSymbols.ts @@ -43,7 +43,7 @@ export class ImportDeconstructionSymbols implements SlangNode { const items = path.map(print, 'items'); return printSeparatedList( items, - satisfies(compiler, '>=0.7.4') && items.length > 1 + 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. From e15a6b8b7476998b247c2ff29318b6e0aeb04aed Mon Sep 17 00:00:00 2001 From: Klaus Date: Fri, 11 Jul 2025 18:05:25 +0100 Subject: [PATCH 3/3] updating README.md --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 401c035cd..58be86ef3 100644 --- a/README.md +++ b/README.md @@ -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"; ``` @@ -208,9 +209,9 @@ You might have a multi-version project, where different files are compiled with } ``` -| Default | CLI Override | API Override | -| ------- | --------------------- | ---------------------- | -| None | `--compiler ` | `compiler: ""` | +| Default | CLI Override | API Override | +| --------------------------------------------------------------------------------------------- | --------------------- | ---------------------- | +| Inferred from pragma statements when using parser `slang`
None when using parser `antlr` | `--compiler ` | `compiler: ""` | ### Parser