|
1 | | -import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast'; |
2 | | -import { NonterminalKind, Query } from '@nomicfoundation/slang/cst'; |
| 1 | +import { NonterminalKind } from '@nomicfoundation/slang/cst'; |
3 | 2 | import { Parser } from '@nomicfoundation/slang/parser'; |
4 | 3 | import { LanguageFacts } from '@nomicfoundation/slang/utils'; |
5 | | -import { |
6 | | - maxSatisfying, |
7 | | - minSatisfying, |
8 | | - minor, |
9 | | - major, |
10 | | - satisfies, |
11 | | - validRange |
12 | | -} from 'semver'; |
13 | | -import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js'; |
| 4 | +import { minSatisfying } from 'semver'; |
14 | 5 |
|
15 | 6 | import type { ParseOutput } from '@nomicfoundation/slang/parser'; |
16 | 7 | import type { ParserOptions } from 'prettier'; |
17 | 8 | import type { AstNode } from '../slang-nodes/types.js'; |
18 | 9 |
|
19 | 10 | const supportedVersions = LanguageFacts.allVersions(); |
| 11 | +const supportedLength = supportedVersions.length; |
20 | 12 |
|
21 | | -const milestoneVersions = Array.from( |
22 | | - supportedVersions.reduce((minorRanges, version) => { |
23 | | - minorRanges.add(`^${major(version)}.${minor(version)}.0`); |
24 | | - return minorRanges; |
25 | | - }, new Set<string>()) |
26 | | -) |
27 | | - .reverse() |
28 | | - .reduce((versions: string[], range) => { |
29 | | - versions.push(maxSatisfying(supportedVersions, range)!); |
30 | | - versions.push(minSatisfying(supportedVersions, range)!); |
31 | | - return versions; |
32 | | - }, []); |
33 | | - |
34 | | -const queries = [ |
35 | | - Query.create('[VersionPragma @versionRanges [VersionExpressionSets]]') |
36 | | -]; |
37 | | - |
38 | | -let parser: Parser; |
| 13 | +function parserAndOutput( |
| 14 | + text: string, |
| 15 | + version: string |
| 16 | +): { parser: Parser; parseOutput: ParseOutput } { |
| 17 | + const parser = Parser.create(version); |
| 18 | + return { |
| 19 | + parser, |
| 20 | + parseOutput: parser.parseNonterminal(NonterminalKind.SourceUnit, text) |
| 21 | + }; |
| 22 | +} |
39 | 23 |
|
40 | 24 | export function createParser( |
41 | 25 | text: string, |
42 | 26 | options: ParserOptions<AstNode> |
43 | | -): [Parser, ParseOutput] { |
44 | | - const compiler = maxSatisfying(supportedVersions, options.compiler); |
| 27 | +): { parser: Parser; parseOutput: ParseOutput } { |
| 28 | + const compiler = minSatisfying(supportedVersions, options.compiler); |
45 | 29 | if (compiler) { |
46 | | - if (!parser || parser.languageVersion !== compiler) { |
47 | | - parser = Parser.create(compiler); |
48 | | - } |
49 | | - return [parser, parser.parseNonterminal(NonterminalKind.SourceUnit, text)]; |
50 | | - } |
51 | | - |
52 | | - let isCachedParser = false; |
53 | | - if (parser) { |
54 | | - isCachedParser = true; |
55 | | - } else { |
56 | | - parser = Parser.create(milestoneVersions[0]); |
57 | | - } |
58 | | - |
59 | | - let parseOutput; |
60 | | - let inferredRanges: string[] = []; |
61 | | - |
62 | | - try { |
63 | | - parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text); |
64 | | - inferredRanges = tryToCollectPragmas(parseOutput, parser, isCachedParser); |
65 | | - } catch { |
66 | | - for ( |
67 | | - let i = isCachedParser ? 0 : 1; |
68 | | - i <= milestoneVersions.length; |
69 | | - i += 1 |
70 | | - ) { |
71 | | - try { |
72 | | - const version = milestoneVersions[i]; |
73 | | - parser = Parser.create(version); |
74 | | - parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text); |
75 | | - inferredRanges = tryToCollectPragmas(parseOutput, parser); |
76 | | - break; |
77 | | - } catch { |
78 | | - continue; |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - const satisfyingVersions = inferredRanges.reduce( |
84 | | - (versions, inferredRange) => { |
85 | | - if (!validRange(inferredRange)) { |
86 | | - throw new Error( |
87 | | - `Couldn't infer any version from the ranges in the pragmas${options.filepath ? ` for file ${options.filepath}` : ''}` |
88 | | - ); |
89 | | - } |
90 | | - return versions.filter((supportedVersion) => |
91 | | - satisfies(supportedVersion, inferredRange) |
| 30 | + const result = parserAndOutput(text, compiler); |
| 31 | + |
| 32 | + 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 ${ |
| 37 | + result.parser.languageVersion |
| 38 | + }. If you would like to change that, specify a different version in your \`.prettierrc\` file.` |
92 | 39 | ); |
93 | | - }, |
94 | | - supportedVersions |
95 | | - ); |
96 | | - |
97 | | - const inferredVersion = |
98 | | - satisfyingVersions.length > 0 |
99 | | - ? satisfyingVersions[satisfyingVersions.length - 1] |
100 | | - : supportedVersions[supportedVersions.length - 1]; |
101 | 40 |
|
102 | | - if (inferredVersion !== parser.languageVersion) { |
103 | | - parser = Parser.create(inferredVersion); |
104 | | - parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text); |
| 41 | + return result; |
105 | 42 | } |
| 43 | + const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text); |
| 44 | + const inferredLength = inferredRanges.length; |
106 | 45 |
|
107 | | - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion |
108 | | - return [parser, parseOutput!]; |
109 | | -} |
110 | | - |
111 | | -function tryToCollectPragmas( |
112 | | - parseOutput: ParseOutput, |
113 | | - parser: Parser, |
114 | | - isCachedParser = false |
115 | | -): string[] { |
116 | | - const matches = parseOutput.createTreeCursor().query(queries); |
117 | | - const ranges: string[] = []; |
118 | | - |
119 | | - let match; |
120 | | - while ((match = matches.next())) { |
121 | | - const versionRange = new SlangVersionExpressionSets( |
122 | | - match.captures.versionRanges[0].node.asNonterminalNode()! |
123 | | - ); |
124 | | - ranges.push( |
125 | | - // Replace all comments that could be in the expression with whitespace |
126 | | - new VersionExpressionSets(versionRange).comments.reduce( |
127 | | - (range, comment) => range.replace(comment.value, ' '), |
128 | | - versionRange.cst.unparse() |
129 | | - ) |
| 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.` |
130 | 49 | ); |
131 | 50 | } |
| 51 | + const result = parserAndOutput( |
| 52 | + text, |
| 53 | + inferredRanges[inferredLength === supportedLength ? inferredLength - 1 : 0] |
| 54 | + ); |
132 | 55 |
|
133 | | - if (ranges.length === 0) { |
134 | | - // If we didn't find pragmas but succeeded parsing the source we keep it. |
135 | | - if (!isCachedParser && parseOutput.isValid()) { |
136 | | - return [parser.languageVersion]; |
137 | | - } |
138 | | - // Otherwise we throw. |
| 56 | + if (!result.parseOutput.isValid()) |
139 | 57 | throw new Error( |
140 | | - `Using version ${parser.languageVersion} did not find any pragma statement and does not parse without errors.` |
| 58 | + `We encountered the following syntax error:\n\n\t${ |
| 59 | + result.parseOutput.errors()[0].message |
| 60 | + }\n\nBased on the pragma statements, we inferred your code to be using Solidity version ${ |
| 61 | + result.parser.languageVersion |
| 62 | + }. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.` |
141 | 63 | ); |
142 | | - } |
143 | 64 |
|
144 | | - return ranges; |
| 65 | + return result; |
145 | 66 | } |
0 commit comments