hardhat-output-validator
Version:
Zero-config Hardhat plugin to check the build output of Solidity contracts
104 lines • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkIfStateVar = exports.checkIsExternalDependency = exports.findByName = void 0;
const findByName = (searchInObj, entityName) => {
if (!searchInObj || !entityName) {
return;
}
const key = Object.keys(searchInObj).find((methodSigniture) => methodSigniture === entityName);
if (!key) {
return;
}
return searchInObj[key];
};
exports.findByName = findByName;
const checkIsExternalDependency = (filePath, contractName, entity, buildInfo) => {
let found = false;
// find in sources
buildInfo.forEach((bi) => {
const key = Object.keys(bi.output.sources).find((k) => k === filePath);
if (key) {
const ast = bi.output.sources[key].ast;
ast.nodes
.filter((n) => n.nodeType === 'ImportDirective')
.forEach((n) => {
var _a, _b, _c;
if (!n.file.startsWith('./') ||
!n.absolutePath.startsWith('contracts')) {
const refId = n.sourceUnit;
let contractDef;
for (const k in bi.output.sources) {
if (((_a = bi.output.sources[k].ast) === null || _a === void 0 ? void 0 : _a.id) === refId) {
const defs = (_b = bi.output.sources[k].ast.nodes) === null || _b === void 0 ? void 0 : _b.filter((nn) => nn.nodeType === 'ContractDefinition');
if (defs.length > 1) {
contractDef = defs.find((d) => d.canonicalName === contractName);
}
else {
contractDef = defs[0];
}
}
if (contractDef) {
break;
}
}
if (contractDef) {
const func = (_c = contractDef.nodes) === null || _c === void 0 ? void 0 : _c.find((nn) => nn.name === entity.name.split('(')[0]);
if (func) {
found = true;
}
}
}
});
}
});
// check for external ImportDirective
//
return found;
};
exports.checkIsExternalDependency = checkIsExternalDependency;
const checkIfStateVar = (filePath, contractName, funcName, buildInfo) => {
const tryFindFunc = (nodes) => {
const funInfo = nodes.find((nn) => nn.name === funcName);
if (funInfo) {
ref = funInfo;
}
};
// sources[contractPath].ast.nodes[]
// .find(n => n.nodeType === "ContractDefinition")
// ?.nodes[].find(nn => nn.name === "varName")?.stateVariable
let ref;
buildInfo.forEach((bi) => {
var _a;
const key = Object.keys(bi.output.sources).find((k) => k === filePath);
if (key) {
const ast = bi.output.sources[key].ast;
if (ast) {
const contractDef = (_a = ast.nodes) === null || _a === void 0 ? void 0 : _a.find((n) => n.nodeType === 'ContractDefinition' &&
n.canonicalName === contractName);
if (contractDef) {
const { baseContracts } = contractDef;
if (baseContracts && baseContracts.length > 0) {
const refIds = baseContracts.map((bc) => bc.baseName.referencedDeclaration);
refIds.forEach((refId) => {
var _a, _b;
let baseContractNode;
for (const k in bi.output.sources) {
baseContractNode = (_b = (_a = bi.output.sources[k].ast) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.find((n) => n.id === refId);
if (baseContractNode) {
break;
}
}
tryFindFunc(baseContractNode === null || baseContractNode === void 0 ? void 0 : baseContractNode.nodes);
});
}
tryFindFunc(contractDef === null || contractDef === void 0 ? void 0 : contractDef.nodes);
}
}
}
});
if (ref) {
return ref === null || ref === void 0 ? void 0 : ref.stateVariable;
}
};
exports.checkIfStateVar = checkIfStateVar;
//# sourceMappingURL=utils.js.map