UNPKG

@nomiclabs/buidler

Version:

Buidler is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

39 lines 1.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const log = debug_1.default("buidler:core:solidity:imports"); function getImports(fileContent) { try { const parser = require("@solidity-parser/parser"); const ast = parser.parse(fileContent, { tolerant: true }); const importedFiles = []; parser.visit(ast, { ImportDirective: (node) => importedFiles.push(node.path), }); return importedFiles; } catch (error) { log("Failed to parse Solidity file to extract its imports\n", error); return findImportsWithRegexps(fileContent); } } exports.getImports = getImports; function findImportsWithRegexps(fileContent) { const importsRegexp = /import\s+(?:(?:"([^;]*)"|'([^;]*)')(?:;|\s+as\s+[^;]*;)|.+from\s+(?:"(.*)"|'(.*)');)/g; let imports = []; let result; while (true) { result = importsRegexp.exec(fileContent); if (result === null) { return imports; } imports = [ ...imports, ...result.slice(1).filter((m) => m !== undefined), ]; } } //# sourceMappingURL=imports.js.map