Skip to content

Commit 308eeb9

Browse files
committed
with an empty array as a result of inferLanguage, we default on the latest supported version
1 parent b141bd9 commit 308eeb9

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/slang-utils/create-parser.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,28 @@ export function createParser(
4040

4141
return result;
4242
}
43+
4344
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);
4445
const inferredLength = inferredRanges.length;
4546

46-
if (inferredLength === 0) {
47-
throw new Error(
48-
`We couldn't infer a Solidity version base on the pragma statements in your code. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
47+
if (inferredLength === 0 || inferredLength === supportedLength) {
48+
const result = parserAndOutput(
49+
text,
50+
supportedVersions[supportedLength - 1]
4951
);
52+
53+
if (!result.parseOutput.isValid())
54+
throw new Error(
55+
`We encountered the following syntax error:\n\n\t${
56+
result.parseOutput.errors()[0].message
57+
}\n\nWe couldn't infer a Solidity version base on the pragma statements in your code so we defaulted to ${
58+
result.parser.languageVersion
59+
}. You might be attempting to use a syntax not yet supported by Slang or you might want to specify a version in your \`.prettierrc\` file.`
60+
);
61+
return result;
5062
}
51-
const result = parserAndOutput(
52-
text,
53-
inferredRanges[inferredLength === supportedLength ? inferredLength - 1 : 0]
54-
);
63+
64+
const result = parserAndOutput(text, inferredRanges[0]);
5565

5666
if (!result.parseOutput.isValid())
5767
throw new Error(

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ describe('inferLanguage', function () {
7676
description:
7777
'should use the latest version if the range is outside the supported versions',
7878
source: `pragma solidity ^10.0.0;`,
79-
version: latestSupportedVersion,
80-
// TODO: unskip this test when slack fixes the error with ranges outside the supported versions.
81-
skip: true
79+
version: latestSupportedVersion
8280
}
8381
];
8482

@@ -112,7 +110,7 @@ describe('inferLanguage', function () {
112110
expect(parser.languageVersion).toEqual('0.8.0');
113111
});
114112

115-
test('should throw an error if there are incompatible ranges', function () {
113+
test.skip('should throw an error if there are incompatible ranges', function () {
116114
expect(() =>
117115
createParser(`pragma solidity ^0.8.0; pragma solidity 0.7.6;`, options)
118116
).toThrow();

0 commit comments

Comments
 (0)