UNPKG

@solvprotocol/upgrade-safe-transpiler

Version:

Solidity preprocessor used to generate OpenZeppelin Contracts Upgrade Safe.

56 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fixNewStatement = void 0; const utils_1 = require("solidity-ast/utils"); const ast_utils_1 = require("../solc/ast-utils"); const new_expression_1 = require("../utils/new-expression"); // Finds statements of the form: // - x = new Foo(...); // - x = address(new Foo(...)); // and transforms them to use initializers: // x = new Foo(); // x.initialize(...); // Note that these are variable assignments. // Variable declarations are not supported. function* fixNewStatement(sourceUnit, tools) { const { resolver, getData } = tools; for (const statement of (0, utils_1.findAll)('ExpressionStatement', sourceUnit)) { const { expression } = statement; if (expression.nodeType === 'Assignment') { const newExpr = (0, new_expression_1.parseNewExpression)(expression.rightHandSide); if (newExpr) { const { typeName, args, initializeCall } = newExpr; const contract = resolver.resolveContract(typeName.referencedDeclaration); if (contract) { getData(contract).isUsedInNewStatement = true; const stBounds = (0, ast_utils_1.getNodeBounds)(statement); const afterStatement = stBounds.start + stBounds.length; yield { start: afterStatement, length: 0, kind: 'fix-new-statement', transform: (_, helper) => [ ';\n', ' '.repeat(4 * 2), initializeCall(helper.read(expression.leftHandSide), helper), ].join(''), }; if (args.length > 0) { const { start } = (0, ast_utils_1.getNodeBounds)(args[0]); const [lastArg] = args.slice(-1); const lastArgBounds = (0, ast_utils_1.getNodeBounds)(lastArg); const length = lastArgBounds.start + lastArgBounds.length - start; yield { start, length, kind: 'fix-new-statement-remove-args', text: '', }; } } } } } } exports.fixNewStatement = fixNewStatement; //# sourceMappingURL=fix-new-statement.js.map