UNPKG

@gnus.ai/upgrade-safe-transpiler-diamond

Version:

Solidity preprocessor used to generate OpenZeppelin Contracts Upgrade Safe using Diamond Pattern (EIP-2535).

75 lines 3.58 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"); // 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 supported. function* fixNewStatement(sourceUnit, tools) { var _a; const { resolver, getData } = tools; for (const statement of (0, utils_1.findAll)(['ExpressionStatement', 'VariableDeclarationStatement'], sourceUnit)) { let rightExpression; let leftExpression; let leftAssignment = ''; if (statement.nodeType === 'ExpressionStatement') { const { expression } = statement; if (expression.nodeType === 'Assignment') { rightExpression = expression.rightHandSide; leftExpression = expression.leftHandSide; } } else if (statement.nodeType === 'VariableDeclarationStatement') { rightExpression = statement.initialValue || undefined; // get right most variable that was assigned in the VariableDeclarationStatement leftAssignment = ((_a = statement.declarations[statement.declarations.length - 1]) === null || _a === void 0 ? void 0 : _a.name) || ''; } if (rightExpression && rightExpression.nodeType === 'FunctionCall' && rightExpression.expression.nodeType === 'NewExpression') { const { typeName } = rightExpression.expression; if (typeName.nodeType === 'UserDefinedTypeName') { 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), leftExpression ? helper.read(leftExpression) : leftAssignment, '.initialize', '(', rightExpression.arguments.map(a => helper.read(a)).join(', '), ')', ].join(''), }; if (rightExpression.arguments.length > 0) { const { start } = (0, ast_utils_1.getNodeBounds)(rightExpression.arguments[0]); const [lastArg] = rightExpression.arguments.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