UNPKG

@openzeppelin/upgrade-safe-transpiler

Version:

Solidity preprocessor used to generate OpenZeppelin Contracts Upgrade Safe.

45 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fixImportDirectives = void 0; const utils_1 = require("solidity-ast/utils"); const ast_utils_1 = require("../solc/ast-utils"); const rename_1 = require("../rename"); function fixImportDirectives(withPeerProject) { return function* (ast, { resolver, getData }) { for (const imp of (0, utils_1.findAll)('ImportDirective', ast)) { const referencedSourceUnit = resolver.resolveNode('SourceUnit', imp.sourceUnit); if (withPeerProject && imp.symbolAliases.length == 0) { throw new Error(`Transpile with peer doesn't support import without aliases in ${imp.absolutePath}`); } const imports = {}; for (const a of imp.symbolAliases) { const id = referencedSourceUnit.exportedSymbols[a.foreign.name]?.[0]; if (id === undefined) { throw new Error(`Can't find symbol imported in ${ast.absolutePath}`); } const node = resolver.resolveNode('*', id); const importFromPeer = getData(node).importFromPeer; const importPath = importFromPeer ?? (0, rename_1.renamePath)(imp.file); imports[importPath] ||= []; imports[importPath].push([ importFromPeer === undefined ? (0, rename_1.renameContract)(a.foreign.name) : a.foreign.name, [null, undefined, a.foreign.name].includes(a.local) ? '' : ` as ${a.local}`, ].join('')); } const statement = []; for (const [path, aliases] of Object.entries(imports)) { statement.push(`import {${aliases.join(', ')}} from "${path}";`); } if (imp.symbolAliases.length == 0) { statement.push(`import "${(0, rename_1.renamePath)(imp.file)}";`); } yield { kind: 'fix-import-directives', text: statement.join('\n'), ...(0, ast_utils_1.getNodeBounds)(imp), }; } }; } exports.fixImportDirectives = fixImportDirectives; //# sourceMappingURL=fix-import-directives.js.map