Skip to content

Commit 92796f6

Browse files
committed
using the earliest version with the exception when there are no pragma statements
1 parent 8bfaf3e commit 92796f6

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/slang-utils/create-parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { Parser } from '@nomicfoundation/slang/parser';
33
import { LanguageFacts } from '@nomicfoundation/slang/utils';
4-
import { maxSatisfying } from 'semver';
4+
import { minSatisfying } from 'semver';
55

66
import type { ParseOutput } from '@nomicfoundation/slang/parser';
77
import type { ParserOptions } from 'prettier';
88
import type { AstNode } from '../slang-nodes/types.js';
99

1010
const supportedVersions = LanguageFacts.allVersions();
11+
const supportedLength = supportedVersions.length;
1112

1213
function parserAndOutput(
1314
text: string,
@@ -24,7 +25,7 @@ export function createParser(
2425
text: string,
2526
options: ParserOptions<AstNode>
2627
): { parser: Parser; parseOutput: ParseOutput } {
27-
const compiler = maxSatisfying(supportedVersions, options.compiler);
28+
const compiler = minSatisfying(supportedVersions, options.compiler);
2829
if (compiler) {
2930
const result = parserAndOutput(text, compiler);
3031

@@ -40,10 +41,11 @@ export function createParser(
4041
return result;
4142
}
4243
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);
44+
const inferredLength = inferredRanges.length;
4345

4446
const result = parserAndOutput(
4547
text,
46-
inferredRanges[inferredRanges.length - 1]
48+
inferredRanges[inferredLength === supportedLength ? inferredLength - 1 : 0]
4749
);
4850

4951
if (!result.parseOutput.isValid())

tests/unit/slang-utils/create-parser.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('inferLanguage', function () {
99
{
1010
description: 'Caret range',
1111
source: `pragma solidity ^0.7.0;`,
12-
version: '0.7.6'
12+
version: '0.7.0'
1313
},
1414
{
1515
description: 'Pinned version',
@@ -83,8 +83,10 @@ describe('inferLanguage', function () {
8383
{
8484
description:
8585
'should use the latest version if the range is outside the supported versions',
86-
source: `pragma solidity ^0.8.27;`,
87-
version: latestSupportedVersion
86+
source: `pragma solidity ^10.0.0;`,
87+
version: latestSupportedVersion,
88+
// TODO: unskip this test when slack fixes the error with ranges outside the supported versions.
89+
skip: true
8890
}
8991
];
9092

@@ -99,9 +101,6 @@ describe('inferLanguage', function () {
99101
let { parser } = createParser(`contract Foo {}`, options);
100102
expect(parser.languageVersion).toEqual(latestSupportedVersion);
101103

102-
({ parser } = createParser(`contract Foo {}`, options));
103-
expect(parser.languageVersion).toEqual(latestSupportedVersion);
104-
105104
// ({ parser } = createParser(`contract Foo {byte bar;}`, options));
106105
// expect(parser.languageVersion).toEqual('0.7.6');
107106
});
@@ -118,7 +117,7 @@ describe('inferLanguage', function () {
118117
expect(parser.languageVersion).toEqual('0.8.2');
119118

120119
({ parser } = createParser(`pragma solidity ^0.8.0;`, {}));
121-
expect(parser.languageVersion).toEqual(latestSupportedVersion);
120+
expect(parser.languageVersion).toEqual('0.8.0');
122121
});
123122

124123
test('should throw an error if there are incompatible ranges', function () {

0 commit comments

Comments
 (0)