Skip to content

Commit af3230a

Browse files
committed
avoid calling coerce multiple times and only do it once at the beginning of the parse
1 parent 32affa7 commit af3230a

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

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';
@@ -44,10 +44,9 @@ export class ContractDefinition implements SlangNode {
4444
this.cleanModifierInvocationArguments(options);
4545
}
4646

47-
cleanModifierInvocationArguments(options: ParserOptions<AstNode>): void {
47+
cleanModifierInvocationArguments({ compiler }: 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);
5150
if (compiler && !satisfies(compiler, '>=0.5.0')) {
5251
for (const member of this.members.items) {
5352
if (

src/slang-nodes/FunctionDefinition.ts

Lines changed: 2 additions & 2 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,7 +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);
57+
const compiler = options.compiler;
5858
if (compiler && satisfies(compiler, '>=0.5.0')) {
5959
this.cleanModifierInvocationArguments();
6060
}

src/slang-nodes/ImportDeconstructionSymbols.ts

Lines changed: 2 additions & 2 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';
@@ -40,7 +40,7 @@ export class ImportDeconstructionSymbols implements SlangNode {
4040
print: PrintFunction,
4141
options: ParserOptions<AstNode>
4242
): Doc {
43-
const compiler = coerce(options.compiler);
43+
const compiler = options.compiler;
4444
return printSeparatedList(
4545
path.map(print, 'items'),
4646
compiler && satisfies(compiler, '>=0.7.4')

src/slang-utils/create-parser.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,25 @@ export function createParser(
3434
text: string,
3535
options: ParserOptions<AstNode>
3636
): { parser: Parser; parseOutput: ParseOutput } {
37-
const compiler = minSatisfying(supportedVersions, options.compiler);
37+
const compiler = options.compiler;
3838
if (compiler) {
39-
const result = parserAndOutput(text, compiler);
39+
const version = minSatisfying(
40+
supportedVersions,
41+
typeof compiler === 'string' ? compiler : compiler.version
42+
);
43+
if (version) {
44+
const result = parserAndOutput(text, version);
4045

41-
if (!result.parseOutput.isValid())
42-
throw createError(
43-
result,
44-
`Based on the compiler option provided, we inferred your code to be using Solidity version ${
45-
result.parser.languageVersion
46-
}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
47-
);
46+
if (!result.parseOutput.isValid())
47+
throw createError(
48+
result,
49+
`Based on the compiler option provided, we inferred your code to be using Solidity version ${
50+
result.parser.languageVersion
51+
}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
52+
);
4853

49-
return result;
54+
return result;
55+
}
5056
}
5157

5258
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);

src/slangSolidityParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// https://prettier.io/docs/en/plugins.html#parsers
22
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast';
3+
import { coerce } from 'semver';
34
import { clearOffsets } from './slang-utils/metadata.js';
45
import { createParser } from './slang-utils/create-parser.js';
56
import { SourceUnit } from './slang-nodes/SourceUnit.js';
@@ -14,7 +15,7 @@ export default function parse(
1415
const { parser, parseOutput } = createParser(text, options);
1516

1617
// We update the compiler version by the inferred one.
17-
options.compiler = parser.languageVersion;
18+
options.compiler = coerce(parser.languageVersion);
1819
const parsed = new SourceUnit(
1920
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
2021
options

src/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { NonterminalKind, TerminalKind } from '@nomicfoundation/slang/cst';
22
import type * as ast from '@nomicfoundation/slang/ast';
33
import type { AstPath, Doc, ParserOptions } from 'prettier';
4+
import type { SemVer } from 'semver';
45
import type { AstNode, Comment, StrictAstNode } from './slang-nodes/types.d.ts';
56

67
// Adding our own options to prettier's `ParserOptions` interface.
78
declare module 'prettier' {
89
interface ParserOptions {
9-
compiler: string;
10+
compiler: string | SemVer | null;
1011
}
1112
}
1213

0 commit comments

Comments
 (0)