-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
executable file
·211 lines (189 loc) · 7.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
import { findAll } from "solidity-ast/utils";
import { SourceUnit } from "solidity-ast";
import TOML from "toml";
import fs from "fs";
import path from "path";
import {
renderArrayFunctions,
renderDeepTestContract,
renderEnumFunctions,
renderStructFunctions,
renderUserTypeFunctions,
} from "./templates";
type ForgeDeepConfig = { dest: string; artifacts: string; contracts: string[]; types: string[] };
const config: ForgeDeepConfig = TOML.parse(fs.readFileSync("forge-deep.toml").toString());
const artifacts: { [absolutePath: string]: string } = {};
function findArtifacts(basePath: string, files: string[]) {
for (const f of files) {
var newBasePath = path.join(basePath, f);
if (fs.statSync(newBasePath).isDirectory()) {
findArtifacts(newBasePath, fs.readdirSync(newBasePath));
} else {
if (f.substring(f.length - 5) === ".json") {
const artifact = JSON.parse(fs.readFileSync(newBasePath).toString());
if ("ast" in artifact) {
artifacts[artifact.ast.absolutePath] = newBasePath;
}
}
}
}
}
findArtifacts(config.artifacts, fs.readdirSync(config.artifacts));
const results: {
[name: string]: { fns: string; source: string };
} = {};
function findDefinitions(absolutePath: string, search?: Set<string>): boolean {
let found = false;
const references: Set<string> = new Set();
if (!(absolutePath in artifacts)) {
throw Error(`Compiler artifact for ${absolutePath} not found in ${config.artifacts}`);
}
const artifactPath = artifacts[absolutePath];
const artifact = JSON.parse(fs.readFileSync(artifactPath).toString());
const ast = artifact.ast as SourceUnit;
for (const enumDef of findAll("EnumDefinition", ast)) {
const { canonicalName } = enumDef;
if (search) {
if (search.delete(canonicalName)) {
found = true;
} else continue;
}
if (canonicalName in results) {
throw Error(
`${canonicalName} defined in both ${absolutePath} and ${results[canonicalName].source}`
);
}
results[canonicalName] = { fns: renderEnumFunctions(enumDef), source: absolutePath };
references.delete(canonicalName);
}
for (const userTypeDef of findAll("UserDefinedValueTypeDefinition", ast)) {
const canonicalName = userTypeDef.canonicalName || userTypeDef.name;
if (search) {
if (search.delete(canonicalName)) {
found = true;
} else continue;
}
if (canonicalName in results) {
throw Error(
`${canonicalName} defined in both ${absolutePath} and ${results[canonicalName].source}`
);
}
results[canonicalName] = {
fns: renderUserTypeFunctions(userTypeDef),
source: absolutePath,
};
references.delete(canonicalName);
}
for (const arrayType of findAll("ArrayTypeName", ast)) {
let {
typeDescriptions: { typeString },
baseType: {
nodeType: elementNodeType,
typeDescriptions: { typeString: elementTypeString },
},
} = arrayType;
// Remove "struct "/"enum " from typeString
[typeString] = typeString?.split(" ").slice(-1)!;
if (search) {
if (search.delete(typeString)) {
found = true;
} else continue;
}
if (typeString in results) continue;
results[typeString] = { fns: renderArrayFunctions(arrayType), source: absolutePath };
references.delete(typeString);
// Remove "struct "/"enum " from elementType
[elementTypeString] = elementTypeString?.split(" ").slice(-1)!;
switch (elementNodeType) {
case "ElementaryTypeName":
break;
case "UserDefinedTypeName":
if (!(elementTypeString in results)) {
references.add(elementTypeString);
}
break;
case "ArrayTypeName":
if (!(elementTypeString in results)) {
references.add(elementTypeString);
}
break;
default:
throw Error(
`Unexpected array element node type: ${elementNodeType} (${elementTypeString})`
);
}
}
for (const structDef of findAll("StructDefinition", ast)) {
const { canonicalName, members } = structDef;
if (search) {
if (search.delete(canonicalName)) {
found = true;
} else continue;
}
if (canonicalName in results) {
throw Error(
`${canonicalName} defined in both ${absolutePath} and ${results[canonicalName].source}`
);
}
results[canonicalName] = { fns: renderStructFunctions(structDef), source: absolutePath };
references.delete(canonicalName);
for (const member of members) {
let memberTypeString = member.typeDescriptions.typeString!;
[memberTypeString] = memberTypeString?.split(" ").slice(-1)!;
const memberNodeType = member.typeName?.nodeType;
if (memberTypeString in results) continue;
switch (memberNodeType) {
case "ElementaryTypeName":
break;
case "UserDefinedTypeName":
if (!(memberTypeString in results)) {
references.add(memberTypeString);
}
break;
case "ArrayTypeName":
if (!(memberTypeString in results)) {
references.add(memberTypeString);
}
break;
default:
throw Error(
`Unexpected struct member node type: ${memberTypeString} (${memberTypeString})`
);
}
}
}
if (references.size > 0) {
const imports = Object.keys(artifact.metadata.sources);
for (const importPath of imports) {
findDefinitions(importPath, references);
if (references.size === 0) break;
}
}
if (references.size > 0) {
throw Error(`Definitions for referenced types ${references.keys()} not found`);
}
return found;
}
for (const contract of config.contracts) {
findDefinitions(contract);
}
const filesToImport = new Set(config.contracts);
const typesToSearchFor = new Set(config.types.filter((t) => !(t in results)));
if (typesToSearchFor.size > 0) {
for (const absolutePath of Object.keys(artifacts)) {
if (findDefinitions(absolutePath, typesToSearchFor)) filesToImport.add(absolutePath);
if (typesToSearchFor.size === 0) break;
}
}
if (typesToSearchFor.size > 0) {
throw Error(`Types ${Array.from(typesToSearchFor.keys()).join(", ")} not found`);
}
const deepTestContract = renderDeepTestContract(
path.basename(config.dest).slice(0, -4),
Array.from(filesToImport),
Object.values(results)
.map(({ fns }) => fns)
.join("\n")
);
fs.writeFileSync(config.dest, deepTestContract);