@@ -21,6 +21,15 @@ function parserAndOutput(
2121 } ;
2222}
2323
24+ function createError (
25+ { parseOutput } : { parseOutput : ParseOutput } ,
26+ reason : string
27+ ) : Error {
28+ return new Error (
29+ `We encountered the following syntax error:\n\n\t${ parseOutput . errors ( ) [ 0 ] . message } \n\n${ reason } `
30+ ) ;
31+ }
32+
2433export function createParser (
2534 text : string ,
2635 options : ParserOptions < AstNode >
@@ -30,10 +39,9 @@ export function createParser(
3039 const result = parserAndOutput ( text , compiler ) ;
3140
3241 if ( ! result . parseOutput . isValid ( ) )
33- throw new Error (
34- `We encountered the following syntax error:\n\n\t${
35- result . parseOutput . errors ( ) [ 0 ] . message
36- } \n\nBased on the compiler option provided, we inferred your code to be using Solidity version ${
42+ throw createError (
43+ result ,
44+ `Based on the compiler option provided, we inferred your code to be using Solidity version ${
3745 result . parser . languageVersion
3846 } . If you would like to change that, specify a different version in your \`.prettierrc\` file.`
3947 ) ;
@@ -51,10 +59,9 @@ export function createParser(
5159 ) ;
5260
5361 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 ${
62+ throw createError (
63+ result ,
64+ `We couldn't infer a Solidity version based on the pragma statements in your code so we defaulted to ${
5865 result . parser . languageVersion
5966 } . 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.`
6067 ) ;
@@ -64,10 +71,9 @@ export function createParser(
6471 const result = parserAndOutput ( text , inferredRanges [ 0 ] ) ;
6572
6673 if ( ! result . parseOutput . isValid ( ) )
67- throw new Error (
68- `We encountered the following syntax error:\n\n\t${
69- result . parseOutput . errors ( ) [ 0 ] . message
70- } \n\nBased on the pragma statements, we inferred your code to be using Solidity version ${
74+ throw createError (
75+ result ,
76+ `Based on the pragma statements, we inferred your code to be using Solidity version ${
7177 result . parser . languageVersion
7278 } . If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
7379 ) ;
0 commit comments