1
1
// Need the ts-nocheck to suppress the noUnusedLocals errors in the generated parser
2
2
// @ts -nocheck
3
3
import {
4
+ ASTNode ,
5
+ ASTNodeConstructor ,
4
6
ContractDefinition ,
5
7
DataLocation ,
6
8
EnumDefinition ,
@@ -33,30 +35,40 @@ import {
33
35
} from "../ast" ;
34
36
35
37
function getFunctionAttributes (
36
- rawDecorators : string [ ]
38
+ decorators : string [ ]
37
39
) : [ FunctionVisibility , FunctionStateMutability ] {
38
40
let visiblity : FunctionVisibility | undefined ;
39
41
let mutability : FunctionStateMutability | undefined ;
40
42
41
- for ( const decorator of rawDecorators ) {
42
- if ( [ "external" , "internal" ] . includes ( decorator ) ) {
43
+ const visiblities = new Set < string > ( [
44
+ FunctionVisibility . Internal ,
45
+ FunctionVisibility . External
46
+ ] ) ;
47
+
48
+ const mutabilities = new Set < string > ( [
49
+ FunctionStateMutability . Pure ,
50
+ FunctionStateMutability . View ,
51
+ FunctionStateMutability . NonPayable ,
52
+ FunctionStateMutability . Payable
53
+ ] ) ;
54
+
55
+ for ( const decorator of decorators ) {
56
+ if ( visiblities . has ( decorator ) ) {
43
57
if ( visiblity !== undefined ) {
44
58
throw new Error (
45
59
`Multiple visiblity decorators specified: ${ decorator } conflicts with ${ visiblity } `
46
60
) ;
47
61
}
48
62
49
- visiblity = decorator ;
50
- }
51
-
52
- if ( [ "pure" , "view" , "nonpayable" , "payable" ] . includes ( decorator ) ) {
63
+ visiblity = decorator as FunctionVisibility ;
64
+ } else if ( mutabilities . has ( decorator ) ) {
53
65
if ( mutability !== undefined ) {
54
66
throw new Error (
55
67
`Multiple mutability decorators specified: ${ decorator } conflicts with ${ mutability } `
56
68
) ;
57
69
}
58
70
59
- mutability = decorator ;
71
+ mutability = decorator as FunctionStateMutability ;
60
72
}
61
73
}
62
74
@@ -73,11 +85,38 @@ function getFunctionAttributes(
73
85
return [ visiblity , mutability ] ;
74
86
}
75
87
88
+ /**
89
+ * Return the `TypeNode` corresponding to `node`, where `node` is an AST node
90
+ * with a type string (`Expression` or `VariableDeclaration`).
91
+ *
92
+ * The function uses a parser to process the type string,
93
+ * while resolving and user-defined type references in the context of `node`.
94
+ *
95
+ * @param arg - an AST node with a type string (`Expression` or `VariableDeclaration`)
96
+ * @param version - compiler version to be used. Useful as resolution rules changed between 0.4.x and 0.5.x.
97
+ */
76
98
export function getNodeType ( node : Expression | VariableDeclaration , version : string ) : TypeNode {
77
99
return parse ( node . typeString , { ctx : node , version } ) as TypeNode ;
78
100
}
79
101
80
- function makeUserDefinedType < T extends ASNode > (
102
+ /**
103
+ * Return the `TypeNode` corresponding to `arg`, where `arg` is either a raw type string,
104
+ * or an AST node with a type string (`Expression` or `VariableDeclaration`).
105
+ *
106
+ * The function uses a parser to process the type string,
107
+ * while resolving and user-defined type references in the context of `ctx`.
108
+ *
109
+ * @param arg - either a type string, or a node with a type string (`Expression` or `VariableDeclaration`)
110
+ * @param version - compiler version to be used. Useful as resolution rules changed between 0.4.x and 0.5.x.
111
+ * @param ctx - `ASTNode` representing the context in which a type string is to be parsed
112
+ */
113
+ export function getNodeTypeInCtx ( arg : Expression | VariableDeclaration | string , version : string , ctx : ASTNode ) : TypeNode {
114
+ const typeString = typeof arg === "string" ? arg : arg . typeString ;
115
+
116
+ return parse ( typeString , { ctx, version } ) as TypeNode ;
117
+ }
118
+
119
+ function makeUserDefinedType < T extends ASTNode > (
81
120
name : string ,
82
121
constructor : ASTNodeConstructor < T > ,
83
122
version : string ,
0 commit comments