prettier-plugin-solidity 2.0.0-beta → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/comments/handlers/index.js +0 -1
  2. package/dist/comments/handlers/index.js.map +1 -1
  3. package/dist/common/print-warning.js +12 -0
  4. package/dist/common/print-warning.js.map +1 -0
  5. package/dist/index.js +18 -16
  6. package/dist/index.js.map +1 -1
  7. package/dist/parser.js +2 -2
  8. package/dist/parser.js.map +1 -1
  9. package/dist/printer.js +2 -1
  10. package/dist/printer.js.map +1 -1
  11. package/dist/slang-comments/handlers/handle-contract-definition-comments.js +0 -7
  12. package/dist/slang-comments/handlers/handle-contract-definition-comments.js.map +1 -1
  13. package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js +27 -0
  14. package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js.map +1 -0
  15. package/dist/slang-comments/handlers/index.js +2 -0
  16. package/dist/slang-comments/handlers/index.js.map +1 -1
  17. package/dist/slang-nodes/ContractDefinition.js +1 -2
  18. package/dist/slang-nodes/ContractDefinition.js.map +1 -1
  19. package/dist/slang-nodes/FunctionDefinition.js +1 -2
  20. package/dist/slang-nodes/FunctionDefinition.js.map +1 -1
  21. package/dist/slang-nodes/ImportDeconstructionSymbols.js +1 -2
  22. package/dist/slang-nodes/ImportDeconstructionSymbols.js.map +1 -1
  23. package/dist/slang-nodes/ModifierInvocation.js +4 -2
  24. package/dist/slang-nodes/ModifierInvocation.js.map +1 -1
  25. package/dist/slang-nodes/RevertStatement.js +2 -2
  26. package/dist/slang-nodes/RevertStatement.js.map +1 -1
  27. package/dist/slang-nodes/{VersionSpecifiers.js → SimpleVersionLiteral.js} +3 -3
  28. package/dist/slang-nodes/SimpleVersionLiteral.js.map +1 -0
  29. package/dist/slang-nodes/SourceUnit.js +11 -2
  30. package/dist/slang-nodes/SourceUnit.js.map +1 -1
  31. package/dist/slang-nodes/VersionExpression.js +12 -25
  32. package/dist/slang-nodes/VersionExpression.js.map +1 -1
  33. package/dist/slang-nodes/VersionLiteral.js +24 -0
  34. package/dist/slang-nodes/VersionLiteral.js.map +1 -0
  35. package/dist/slang-nodes/VersionOperator.js +15 -0
  36. package/dist/slang-nodes/VersionOperator.js.map +1 -0
  37. package/dist/slang-nodes/VersionRange.js +5 -10
  38. package/dist/slang-nodes/VersionRange.js.map +1 -1
  39. package/dist/slang-nodes/VersionTerm.js +24 -0
  40. package/dist/slang-nodes/VersionTerm.js.map +1 -0
  41. package/dist/slang-utils/infer-language.js +62 -0
  42. package/dist/slang-utils/infer-language.js.map +1 -0
  43. package/dist/slang-utils/is-comment.js.map +1 -1
  44. package/dist/slang-utils/print-warning.js +12 -0
  45. package/dist/slang-utils/print-warning.js.map +1 -0
  46. package/dist/slangSolidityParser.js +13 -6
  47. package/dist/slangSolidityParser.js.map +1 -1
  48. package/package.json +8 -9
  49. package/src/comments/handlers/index.js +0 -1
  50. package/src/common/print-warning.js +12 -0
  51. package/src/index.ts +18 -16
  52. package/src/parser.js +4 -3
  53. package/src/printer.js +2 -1
  54. package/src/slang-comments/handlers/handle-contract-definition-comments.ts +0 -8
  55. package/src/slang-comments/handlers/handle-modifier-invocation-comments.ts +40 -0
  56. package/src/slang-comments/handlers/index.ts +2 -0
  57. package/src/slang-nodes/ContractDefinition.ts +1 -2
  58. package/src/slang-nodes/FunctionDefinition.ts +1 -2
  59. package/src/slang-nodes/ImportDeconstructionSymbols.ts +1 -2
  60. package/src/slang-nodes/ModifierInvocation.ts +4 -2
  61. package/src/slang-nodes/RevertStatement.ts +2 -2
  62. package/src/slang-nodes/{VersionSpecifiers.ts → SimpleVersionLiteral.ts} +3 -3
  63. package/src/slang-nodes/SourceUnit.ts +11 -2
  64. package/src/slang-nodes/VersionExpression.ts +19 -37
  65. package/src/slang-nodes/VersionLiteral.ts +42 -0
  66. package/src/slang-nodes/VersionOperator.ts +29 -0
  67. package/src/slang-nodes/VersionRange.ts +7 -14
  68. package/src/slang-nodes/VersionTerm.ts +41 -0
  69. package/src/slang-nodes/index.d.ts +8 -4
  70. package/src/slang-utils/infer-language.ts +94 -0
  71. package/src/slang-utils/is-comment.ts +1 -1
  72. package/src/slang-utils/print-warning.ts +12 -0
  73. package/src/slangSolidityParser.ts +17 -8
  74. package/src/types.d.ts +4 -2
  75. package/dist/slang-nodes/VersionComparator.js +0 -19
  76. package/dist/slang-nodes/VersionComparator.js.map +0 -1
  77. package/dist/slang-nodes/VersionSpecifiers.js.map +0 -1
  78. package/src/slang-nodes/VersionComparator.ts +0 -36
@@ -1,9 +1,7 @@
1
1
  import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
- import { TerminalNode } from '@nomicfoundation/slang/cst/index.js';
3
2
  import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
4
3
  import { VersionRange } from './VersionRange.js';
5
- import { VersionComparator } from './VersionComparator.js';
6
- import { VersionSpecifiers } from './VersionSpecifiers.js';
4
+ import { VersionTerm } from './VersionTerm.js';
7
5
 
8
6
  import type * as ast from '@nomicfoundation/slang/ast';
9
7
  import type { AstPath, Doc } from 'prettier';
@@ -16,52 +14,36 @@ export class VersionExpression implements SlangNode {
16
14
 
17
15
  loc;
18
16
 
19
- variant: VersionRange | VersionComparator | VersionSpecifiers | string;
17
+ variant: VersionRange | VersionTerm;
20
18
 
21
19
  constructor(ast: ast.VersionExpression, offset: number) {
22
20
  let metadata = getNodeMetadata(ast, offset);
23
21
  const { offsets } = metadata;
24
22
 
25
- if (ast.variant instanceof TerminalNode) {
26
- // TODO: test whether this is a Identifier
27
- this.variant = ast.variant.text;
28
- } else {
29
- switch (ast.variant.cst.kind) {
30
- case NonterminalKind.VersionRange:
31
- this.variant = new VersionRange(
32
- ast.variant as ast.VersionRange,
33
- offsets[0]
34
- );
35
- break;
36
- case NonterminalKind.VersionComparator:
37
- this.variant = new VersionComparator(
38
- ast.variant as ast.VersionComparator,
39
- offsets[0]
40
- );
41
- break;
42
- case NonterminalKind.VersionSpecifiers:
43
- this.variant = new VersionSpecifiers(
44
- ast.variant as ast.VersionSpecifiers,
45
- offsets[0]
46
- );
47
- break;
48
- default:
49
- throw new Error(`Unexpected variant: ${ast.variant.cst.kind}`);
50
- }
23
+ switch (ast.variant.cst.kind) {
24
+ case NonterminalKind.VersionRange:
25
+ this.variant = new VersionRange(
26
+ ast.variant as ast.VersionRange,
27
+ offsets[0]
28
+ );
29
+ break;
30
+ case NonterminalKind.VersionTerm:
31
+ this.variant = new VersionTerm(
32
+ ast.variant as ast.VersionTerm,
33
+ offsets[0]
34
+ );
35
+ break;
36
+ default:
37
+ throw new Error(`Unexpected variant: ${ast.variant.cst.kind}`);
51
38
  }
52
39
 
53
- metadata = updateMetadata(
54
- metadata,
55
- typeof this.variant === 'string' ? [] : [this.variant]
56
- );
40
+ metadata = updateMetadata(metadata, [this.variant]);
57
41
 
58
42
  this.comments = metadata.comments;
59
43
  this.loc = metadata.loc;
60
44
  }
61
45
 
62
46
  print(path: AstPath<VersionExpression>, print: PrintFunction): Doc {
63
- return typeof this.variant === 'string'
64
- ? this.variant
65
- : path.call(print, 'variant');
47
+ return path.call(print, 'variant');
66
48
  }
67
49
  }
@@ -0,0 +1,42 @@
1
+ import { TerminalNode } from '@nomicfoundation/slang/cst/index.js';
2
+ import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
3
+ import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
4
+ import { SimpleVersionLiteral } from './SimpleVersionLiteral.js';
5
+
6
+ import type * as ast from '@nomicfoundation/slang/ast';
7
+ import type { AstPath, Doc } from 'prettier';
8
+ import type { PrintFunction, SlangNode } from '../types';
9
+
10
+ export class VersionLiteral implements SlangNode {
11
+ readonly kind = NonterminalKind.VersionLiteral;
12
+
13
+ comments;
14
+
15
+ loc;
16
+
17
+ variant: SimpleVersionLiteral | string;
18
+
19
+ constructor(ast: ast.VersionLiteral, offset: number) {
20
+ let metadata = getNodeMetadata(ast, offset);
21
+ const { offsets } = metadata;
22
+
23
+ this.variant =
24
+ ast.variant instanceof TerminalNode
25
+ ? ast.variant.text
26
+ : new SimpleVersionLiteral(ast.variant, offsets[0]);
27
+
28
+ metadata = updateMetadata(
29
+ metadata,
30
+ typeof this.variant === 'string' ? [] : [this.variant]
31
+ );
32
+
33
+ this.comments = metadata.comments;
34
+ this.loc = metadata.loc;
35
+ }
36
+
37
+ print(path: AstPath<VersionLiteral>, print: PrintFunction): Doc {
38
+ return typeof this.variant === 'string'
39
+ ? this.variant
40
+ : path.call(print, 'variant');
41
+ }
42
+ }
@@ -0,0 +1,29 @@
1
+ import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
+ import { getNodeMetadata } from '../slang-utils/metadata.js';
3
+
4
+ import type * as ast from '@nomicfoundation/slang/ast';
5
+ import type { Doc } from 'prettier';
6
+ import type { SlangNode } from '../types';
7
+
8
+ export class VersionOperator implements SlangNode {
9
+ readonly kind = NonterminalKind.VersionOperator;
10
+
11
+ comments;
12
+
13
+ loc;
14
+
15
+ variant: string;
16
+
17
+ constructor(ast: ast.VersionOperator, offset: number) {
18
+ const metadata = getNodeMetadata(ast, offset);
19
+
20
+ this.variant = ast.variant.text;
21
+
22
+ this.comments = metadata.comments;
23
+ this.loc = metadata.loc;
24
+ }
25
+
26
+ print(): Doc {
27
+ return this.variant;
28
+ }
29
+ }
@@ -1,6 +1,6 @@
1
1
  import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
2
  import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3
- import { VersionExpression } from './VersionExpression.js';
3
+ import { VersionLiteral } from './VersionLiteral.js';
4
4
 
5
5
  import type * as ast from '@nomicfoundation/slang/ast';
6
6
  import type { AstPath, Doc } from 'prettier';
@@ -13,31 +13,24 @@ export class VersionRange implements SlangNode {
13
13
 
14
14
  loc;
15
15
 
16
- leftOperand: VersionExpression;
16
+ start: VersionLiteral;
17
17
 
18
- operator: string;
19
-
20
- rightOperand: VersionExpression;
18
+ end: VersionLiteral;
21
19
 
22
20
  constructor(ast: ast.VersionRange, offset: number) {
23
21
  let metadata = getNodeMetadata(ast, offset);
24
22
  const { offsets } = metadata;
25
23
 
26
- this.leftOperand = new VersionExpression(ast.leftOperand, offsets[0]);
27
- this.operator = ast.operator.text;
28
- this.rightOperand = new VersionExpression(ast.rightOperand, offsets[1]);
24
+ this.start = new VersionLiteral(ast.start, offsets[0]);
25
+ this.end = new VersionLiteral(ast.end, offsets[1]);
29
26
 
30
- metadata = updateMetadata(metadata, [this.leftOperand, this.rightOperand]);
27
+ metadata = updateMetadata(metadata, [this.start, this.end]);
31
28
 
32
29
  this.comments = metadata.comments;
33
30
  this.loc = metadata.loc;
34
31
  }
35
32
 
36
33
  print(path: AstPath<VersionRange>, print: PrintFunction): Doc {
37
- return [
38
- path.call(print, 'leftOperand'),
39
- ` ${this.operator} `,
40
- path.call(print, 'rightOperand')
41
- ];
34
+ return [path.call(print, 'start'), ' - ', path.call(print, 'end')];
42
35
  }
43
36
  }
@@ -0,0 +1,41 @@
1
+ import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
+ import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3
+ import { VersionOperator } from './VersionOperator.js';
4
+ import { VersionLiteral } from './VersionLiteral.js';
5
+
6
+ import type * as ast from '@nomicfoundation/slang/ast';
7
+ import type { AstPath, Doc } from 'prettier';
8
+ import type { PrintFunction, SlangNode } from '../types';
9
+
10
+ export class VersionTerm implements SlangNode {
11
+ readonly kind = NonterminalKind.VersionTerm;
12
+
13
+ comments;
14
+
15
+ loc;
16
+
17
+ operator?: VersionOperator;
18
+
19
+ literal: VersionLiteral;
20
+
21
+ constructor(ast: ast.VersionTerm, offset: number) {
22
+ let metadata = getNodeMetadata(ast, offset);
23
+ const { offsets } = metadata;
24
+
25
+ let i = 0;
26
+ if (ast.operator) {
27
+ this.operator = new VersionOperator(ast.operator, offsets[i]);
28
+ i += 1;
29
+ }
30
+ this.literal = new VersionLiteral(ast.literal, offsets[i]);
31
+
32
+ metadata = updateMetadata(metadata, [this.operator, this.literal]);
33
+
34
+ this.comments = metadata.comments;
35
+ this.loc = metadata.loc;
36
+ }
37
+
38
+ print(path: AstPath<VersionTerm>, print: PrintFunction): Doc {
39
+ return [path.call(print, 'operator'), path.call(print, 'literal')];
40
+ }
41
+ }
@@ -127,6 +127,7 @@ import type { ReturnsDeclaration } from './ReturnsDeclaration';
127
127
  import type { ReturnStatement } from './ReturnStatement';
128
128
  import type { RevertStatement } from './RevertStatement';
129
129
  import type { ShiftExpression } from './ShiftExpression';
130
+ import type { SimpleVersionLiteral } from './SimpleVersionLiteral';
130
131
  import type { SingleLineComment } from './SingleLineComment';
131
132
  import type { SingleLineNatSpecComment } from './SingleLineNatSpecComment';
132
133
  import type { SourceUnit } from './SourceUnit';
@@ -176,13 +177,14 @@ import type { UsingTarget } from './UsingTarget';
176
177
  import type { VariableDeclarationStatement } from './VariableDeclarationStatement';
177
178
  import type { VariableDeclarationType } from './VariableDeclarationType';
178
179
  import type { VariableDeclarationValue } from './VariableDeclarationValue';
179
- import type { VersionComparator } from './VersionComparator';
180
180
  import type { VersionExpression } from './VersionExpression';
181
181
  import type { VersionExpressionSet } from './VersionExpressionSet';
182
182
  import type { VersionExpressionSets } from './VersionExpressionSets';
183
+ import type { VersionOperator } from './VersionOperator';
184
+ import type { VersionLiteral } from './VersionLiteral';
183
185
  import type { VersionPragma } from './VersionPragma';
184
186
  import type { VersionRange } from './VersionRange';
185
- import type { VersionSpecifiers } from './VersionSpecifiers';
187
+ import type { VersionTerm } from './VersionTerm';
186
188
  import type { WhileStatement } from './WhileStatement';
187
189
  import type { YulArguments } from './YulArguments';
188
190
  import type { YulAssignmentOperator } from './YulAssignmentOperator';
@@ -243,7 +245,7 @@ export type StrictAstNode =
243
245
  | ExperimentalPragma
244
246
  | VersionPragma
245
247
  | VersionRange
246
- | VersionComparator
248
+ | VersionTerm
247
249
  | ImportDirective
248
250
  | PathImport
249
251
  | NamedImport
@@ -371,6 +373,8 @@ export type StrictAstNode =
371
373
  | Pragma
372
374
  | ExperimentalFeature
373
375
  | VersionExpression
376
+ | VersionOperator
377
+ | VersionLiteral
374
378
  | ImportClause
375
379
  | UsingClause
376
380
  | UsingOperator
@@ -431,7 +435,7 @@ export type StrictAstNode =
431
435
  | YulStatements
432
436
  | YulSwitchCases
433
437
  | VersionExpressionSets
434
- | VersionSpecifiers
438
+ | SimpleVersionLiteral
435
439
  | ImportDeconstructionSymbols
436
440
  | UsingDeconstructionSymbols
437
441
  | InheritanceTypes
@@ -0,0 +1,94 @@
1
+ import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast/index.js';
2
+ import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
3
+ import { Language } from '@nomicfoundation/slang/language/index.js';
4
+ import { Query } from '@nomicfoundation/slang/query/index.js';
5
+ import {
6
+ maxSatisfying,
7
+ minSatisfying,
8
+ minor,
9
+ major,
10
+ minVersion,
11
+ validRange
12
+ } from 'semver';
13
+ import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js';
14
+
15
+ import type { NonterminalNode } from '@nomicfoundation/slang/cst';
16
+
17
+ const supportedVersions = Language.supportedVersions();
18
+
19
+ const milestoneVersions = Array.from(
20
+ supportedVersions.reduce((minorRanges, version) => {
21
+ minorRanges.add(`^${major(version)}.${minor(version)}.0`);
22
+ return minorRanges;
23
+ }, new Set<string>())
24
+ )
25
+ .reverse()
26
+ .reduce((versions: string[], range) => {
27
+ versions.push(maxSatisfying(supportedVersions, range)!);
28
+ versions.push(minSatisfying(supportedVersions, range)!);
29
+ return versions;
30
+ }, []);
31
+
32
+ export function inferLanguage(text: string): Language {
33
+ let inferredRange = '';
34
+
35
+ for (const version of milestoneVersions) {
36
+ try {
37
+ inferredRange = tryToCollectPragmas(text, version);
38
+ break;
39
+ } catch {}
40
+ }
41
+
42
+ if (!minVersion(inferredRange)) {
43
+ throw new Error(
44
+ "Couldn't infer any version from the ranges in the pragmas."
45
+ );
46
+ }
47
+
48
+ const maxSatisfyingVersion = maxSatisfying(supportedVersions, inferredRange);
49
+
50
+ return maxSatisfyingVersion
51
+ ? new Language(maxSatisfyingVersion)
52
+ : new Language(supportedVersions[supportedVersions.length - 1]);
53
+ }
54
+
55
+ function tryToCollectPragmas(text: string, version: string): string {
56
+ const language = new Language(version);
57
+ const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
58
+ const query = Query.parse(
59
+ '[VersionPragma @versionRanges [VersionExpressionSets]]'
60
+ );
61
+ const matches = parseOutput.createTreeCursor().query([query]);
62
+ const ranges: string[] = [];
63
+
64
+ let match;
65
+ while ((match = matches.next())) {
66
+ const versionRange = new SlangVersionExpressionSets(
67
+ match.captures.versionRanges[0].node() as NonterminalNode
68
+ );
69
+ ranges.push(
70
+ // Replace all comments that could be in the expression with whitespace
71
+ new VersionExpressionSets(versionRange, 0).comments.reduce(
72
+ (range, comment) => range.replace(comment.value, ' '),
73
+ versionRange.cst.unparse()
74
+ )
75
+ );
76
+ }
77
+
78
+ // Check if we found pragmas.
79
+ if (ranges.length === 0) {
80
+ // If we didn't find pragmas but succeeded parsing the source we keep it.
81
+ if (parseOutput.isValid) {
82
+ return version;
83
+ }
84
+ // Otherwise we throw.
85
+ throw new Error(
86
+ `Using version ${version} did not find any pragma statement and does not parse without errors.`
87
+ );
88
+ }
89
+
90
+ // validRange rewrites `0.5.0 - 0.6.0` as `>=0.5.0 <=0.6.0` but it returns
91
+ // null if the range is not valid. We have to coerce null to 'null' so it
92
+ // fails the `minVersion(inferredRange)` call.
93
+ return ranges.map((range) => `${validRange(range)}`).join(' ');
94
+ }
@@ -7,7 +7,7 @@ import type { AstNode, BlockComment, Comment } from '../slang-nodes';
7
7
  export const isBlockComment = createKindCheckFunction([
8
8
  TerminalKind.MultiLineComment,
9
9
  TerminalKind.MultiLineNatSpecComment
10
- ]) as (node: AstNode) => node is BlockComment;
10
+ ]) as (node: AstNode | Node) => node is BlockComment;
11
11
 
12
12
  export const isComment = createKindCheckFunction([
13
13
  TerminalKind.MultiLineComment,
@@ -0,0 +1,12 @@
1
+ export function printWarning(message: string): void {
2
+ // Prettier prints some temporary messages while formatting, and this warning
3
+ // can mess with that output. We clear the line and move the cursor to the
4
+ // beginning of the line to avoid this.
5
+ //
6
+ // \x1b: Escape character
7
+ // [2K: Escape code to clear the entire line
8
+ // \r: Carriage return
9
+ const clearLine = '\x1b[2K\r';
10
+
11
+ console.warn(`${clearLine}[prettier-solidity] ${message}`);
12
+ }
@@ -3,30 +3,39 @@
3
3
  import { Language } from '@nomicfoundation/slang/language/index.js';
4
4
  import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
5
5
  import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast/index.js';
6
- import coerce from 'semver/functions/coerce.js';
6
+ import { maxSatisfying } from 'semver';
7
+ import { inferLanguage } from './slang-utils/infer-language.js';
8
+ import { printWarning } from './slang-utils/print-warning.js';
7
9
  import { SourceUnit } from './slang-nodes/SourceUnit.js';
8
10
 
9
11
  import type { NonterminalNode } from '@nomicfoundation/slang/cst';
10
12
  import type { Parser, ParserOptions } from 'prettier';
11
13
  import type { AstNode } from './slang-nodes/index.js';
12
14
 
15
+ const supportedVersions = Language.supportedVersions();
16
+
13
17
  export default function parse(
14
18
  text: string,
15
19
  _parsers: Parser[] | ParserOptions<AstNode>,
16
20
  options = _parsers as ParserOptions<AstNode>
17
21
  ): AstNode {
18
- const compiler = coerce(options.compiler);
19
- const supportedVersions = Language.supportedVersions();
22
+ const compiler = maxSatisfying(supportedVersions, options.compiler);
20
23
 
21
- const language = new Language(
22
- compiler && supportedVersions.includes(compiler.version)
23
- ? compiler.version
24
- : supportedVersions[supportedVersions.length - 1]
25
- );
24
+ const language =
25
+ compiler && supportedVersions.includes(compiler)
26
+ ? new Language(compiler)
27
+ : inferLanguage(text);
26
28
 
27
29
  const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
30
+ printWarning(
31
+ compiler
32
+ ? `Using version ${language.version} based on the compiler option provided.`
33
+ : `Inferred version ${language.version} based on the pragma statements in the code.`
34
+ );
28
35
 
29
36
  if (parseOutput.isValid) {
37
+ // We update the compiler version by the inferred one.
38
+ options.compiler = language.version;
30
39
  return new SourceUnit(
31
40
  new SlangSourceUnit(parseOutput.tree() as NonterminalNode),
32
41
  0,
package/src/types.d.ts CHANGED
@@ -69,7 +69,7 @@ export type SlangAstNode =
69
69
  | ast.ExperimentalPragma
70
70
  | ast.VersionPragma
71
71
  | ast.VersionRange
72
- | ast.VersionComparator
72
+ | ast.VersionTerm
73
73
  | ast.ImportDirective
74
74
  | ast.PathImport
75
75
  | ast.NamedImport
@@ -197,6 +197,8 @@ export type SlangAstNode =
197
197
  | ast.Pragma
198
198
  | ast.ExperimentalFeature
199
199
  | ast.VersionExpression
200
+ | ast.VersionOperator
201
+ | ast.VersionLiteral
200
202
  | ast.ImportClause
201
203
  | ast.UsingClause
202
204
  | ast.UsingOperator
@@ -257,7 +259,7 @@ export type SlangAstNode =
257
259
  | ast.YulStatements
258
260
  | ast.YulSwitchCases
259
261
  | ast.VersionExpressionSets
260
- | ast.VersionSpecifiers
262
+ | ast.SimpleVersionLiteral
261
263
  | ast.ImportDeconstructionSymbols
262
264
  | ast.UsingDeconstructionSymbols
263
265
  | ast.InheritanceTypes
@@ -1,19 +0,0 @@
1
- import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
- import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3
- import { VersionExpression } from './VersionExpression.js';
4
- export class VersionComparator {
5
- constructor(ast, offset) {
6
- this.kind = NonterminalKind.VersionComparator;
7
- let metadata = getNodeMetadata(ast, offset);
8
- const { offsets } = metadata;
9
- this.operator = ast.operator.text;
10
- this.operand = new VersionExpression(ast.operand, offsets[0]);
11
- metadata = updateMetadata(metadata, [this.operand]);
12
- this.comments = metadata.comments;
13
- this.loc = metadata.loc;
14
- }
15
- print(path, print) {
16
- return [this.operator, path.call(print, 'operand')];
17
- }
18
- }
19
- //# sourceMappingURL=VersionComparator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"VersionComparator.js","sourceRoot":"","sources":["../../src/slang-nodes/VersionComparator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAM3D,MAAM,OAAO,iBAAiB;IAW5B,YAAY,GAA0B,EAAE,MAAc;QAV7C,SAAI,GAAG,eAAe,CAAC,iBAAiB,CAAC;QAWhD,IAAI,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC5C,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;QAE7B,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAiB,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QAE9D,QAAQ,GAAG,cAAc,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAEpD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC;IAC1B,CAAC;IAED,KAAK,CAAC,IAAgC,EAAE,KAAoB;QAC1D,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;IACtD,CAAC;CACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"VersionSpecifiers.js","sourceRoot":"","sources":["../../src/slang-nodes/VersionSpecifiers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAM7D,MAAM,OAAO,iBAAiB;IAW5B,YAAY,GAA0B,EAAE,MAAc;QAV7C,SAAI,GAAG,eAAe,CAAC,iBAAiB,CAAC;QAWhD,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAEpD,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChD,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAEpE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC;IAC1B,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CACpC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CACxD,CAAC;IACJ,CAAC;CACF"}
@@ -1,36 +0,0 @@
1
- import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2
- import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3
- import { VersionExpression } from './VersionExpression.js';
4
-
5
- import type * as ast from '@nomicfoundation/slang/ast';
6
- import type { AstPath, Doc } from 'prettier';
7
- import type { PrintFunction, SlangNode } from '../types';
8
-
9
- export class VersionComparator implements SlangNode {
10
- readonly kind = NonterminalKind.VersionComparator;
11
-
12
- comments;
13
-
14
- loc;
15
-
16
- operator: string;
17
-
18
- operand: VersionExpression;
19
-
20
- constructor(ast: ast.VersionComparator, offset: number) {
21
- let metadata = getNodeMetadata(ast, offset);
22
- const { offsets } = metadata;
23
-
24
- this.operator = ast.operator.text;
25
- this.operand = new VersionExpression(ast.operand, offsets[0]);
26
-
27
- metadata = updateMetadata(metadata, [this.operand]);
28
-
29
- this.comments = metadata.comments;
30
- this.loc = metadata.loc;
31
- }
32
-
33
- print(path: AstPath<VersionComparator>, print: PrintFunction): Doc {
34
- return [this.operator, path.call(print, 'operand')];
35
- }
36
- }