Skip to content

Commit 8bfaf3e

Browse files
committed
improving the error messages when the language inference fails because of the syntax.
1 parent 71682dc commit 8bfaf3e

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/slang-utils/create-parser.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,34 @@ export function createParser(
2525
options: ParserOptions<AstNode>
2626
): { parser: Parser; parseOutput: ParseOutput } {
2727
const compiler = maxSatisfying(supportedVersions, options.compiler);
28-
if (compiler) return parserAndOutput(text, compiler);
28+
if (compiler) {
29+
const result = parserAndOutput(text, compiler);
2930

31+
if (!result.parseOutput.isValid())
32+
throw new Error(
33+
'We encoutered the following syntax error:\n\n\t' +
34+
result.parseOutput.errors()[0].message +
35+
'\n\nBased on the compiler option provided, we inferred your code to be using Solidity version ' +
36+
result.parser.languageVersion +
37+
'. If you would like to change that, specify a different version in your `.prettierrc` file.'
38+
);
39+
40+
return result;
41+
}
3042
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);
3143

3244
const result = parserAndOutput(
3345
text,
3446
inferredRanges[inferredRanges.length - 1]
3547
);
48+
3649
if (!result.parseOutput.isValid())
3750
throw new Error(
38-
`Based on the pragma statements, we inferred your code to be using Solidity version ${
39-
result.parser.languageVersion
40-
}. If you would like to change that, update the pragmas in your source file, or specify a version in \`.prettierrc\` or VSCode's \`settings.json\`.`
51+
'We encoutered the following syntax error:\n\n\t' +
52+
result.parseOutput.errors()[0].message +
53+
'\n\nBased on the pragma statements, we inferred your code to be using Solidity version ' +
54+
result.parser.languageVersion +
55+
'. If you would like to change that, update the pragmas in your source file, or specify a version in your `.prettierrc` file.'
4156
);
4257

4358
return result;

src/slangSolidityParser.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ export default function parse(
1313
): AstNode {
1414
const { parser, parseOutput } = createParser(text, options);
1515

16-
if (parseOutput.isValid()) {
17-
// We update the compiler version by the inferred one.
18-
options.compiler = parser.languageVersion;
19-
const parsed = new SourceUnit(
20-
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
21-
options
22-
);
23-
clearOffsets();
24-
return parsed;
25-
}
26-
throw new Error(parseOutput.errors()[0].message);
16+
// We update the compiler version by the inferred one.
17+
options.compiler = parser.languageVersion;
18+
const parsed = new SourceUnit(
19+
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
20+
options
21+
);
22+
clearOffsets();
23+
return parsed;
2724
}

0 commit comments

Comments
 (0)