solidity-ast
Version:
Solidity AST schema and type definitions
47 lines • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.curry2 = exports.astDereferencer = void 0;
const find_all_1 = require("../utils/find-all");
// An ASTDereferencer is a function that looks up an AST node given its id, in all of the source files involved in a
// solc run. It will generally be used together with the AST property `referencedDeclaration` (found in Identifier,
// UserDefinedTypeName, etc.) to look up a variable definition or type definition.
function astDereferencer(solcOutput) {
const asts = Array.from(Object.values(solcOutput.sources), s => s.ast);
const cache = new Map();
function deref(nodeType, id) {
if (!isArray(nodeType)) {
nodeType = [nodeType];
}
const cached = cache.get(id);
if (cached) {
if (nodeType.includes(cached.nodeType)) {
return cached;
}
}
for (const ast of asts) {
for (const node of find_all_1.findAll(nodeType, ast)) {
if (node.id === id) {
cache.set(id, node);
return node;
}
}
}
throw new Error(`No node with id ${id} of type ${nodeType}`);
}
return curry2(deref);
}
exports.astDereferencer = astDereferencer;
function curry2(fn) {
function curried(a, ...b) {
if (b.length === 0) {
return b => fn(a, b);
}
else {
return fn(a, ...b);
}
}
return curried;
}
exports.curry2 = curry2;
const isArray = Array.isArray;
//# sourceMappingURL=ast-dereferencer.js.map