Skip to content

Commit 291e558

Browse files
authored
Merge pull request #37 from ConsenSys/0.5.0_fixes
Export type AST from root and the entry point to parse type strings with explicit context
2 parents 987d208 + c9c8635 commit 291e558

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Diff for: src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./ast";
22
export * from "./compile";
33
export * from "./misc";
4+
export * from "./types";

Diff for: src/types/typeStrings/typeString_parser_header.ts

+48-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Need the ts-nocheck to suppress the noUnusedLocals errors in the generated parser
22
// @ts-nocheck
33
import {
4+
ASTNode,
5+
ASTNodeConstructor,
46
ContractDefinition,
57
DataLocation,
68
EnumDefinition,
@@ -33,30 +35,40 @@ import {
3335
} from "../ast";
3436

3537
function getFunctionAttributes(
36-
rawDecorators: string[]
38+
decorators: string[]
3739
): [FunctionVisibility, FunctionStateMutability] {
3840
let visiblity: FunctionVisibility | undefined;
3941
let mutability: FunctionStateMutability | undefined;
4042

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)) {
4357
if (visiblity !== undefined) {
4458
throw new Error(
4559
`Multiple visiblity decorators specified: ${decorator} conflicts with ${visiblity}`
4660
);
4761
}
4862

49-
visiblity = decorator;
50-
}
51-
52-
if (["pure", "view", "nonpayable", "payable"].includes(decorator)) {
63+
visiblity = decorator as FunctionVisibility;
64+
} else if (mutabilities.has(decorator)) {
5365
if (mutability !== undefined) {
5466
throw new Error(
5567
`Multiple mutability decorators specified: ${decorator} conflicts with ${mutability}`
5668
);
5769
}
5870

59-
mutability = decorator;
71+
mutability = decorator as FunctionStateMutability;
6072
}
6173
}
6274

@@ -73,11 +85,38 @@ function getFunctionAttributes(
7385
return [visiblity, mutability];
7486
}
7587

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+
*/
7698
export function getNodeType(node: Expression | VariableDeclaration, version: string): TypeNode {
7799
return parse(node.typeString, { ctx: node, version }) as TypeNode;
78100
}
79101

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>(
81120
name: string,
82121
constructor: ASTNodeConstructor<T>,
83122
version: string,

0 commit comments

Comments
 (0)