Skip to content

Commit

Permalink
Add support for user-defined operators
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Mar 1, 2023
1 parent 7875618 commit 838e1d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/nodes/UsingForDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@ const {
const { printSeparatedList } = require('../common/printer-helpers');

const UsingForDeclaration = {
print: ({ node, path, print, options }) => [
'using ',
node.functions && node.functions.length
? [
'{',
printSeparatedList(node.functions, {
firstSeparator: options.bracketSpacing ? line : softline
}),
'}'
]
: node.libraryName,
' for ',
node.typeName ? path.call(print, 'typeName') : '*',
node.isGlobal ? ' global;' : ';'
]
print: ({ node, path, print, options }) => {
const parts = ['using '];

if (node.functions && node.functions.length) {
const importedFunctions = [];
for (let i = 0; i < node.functions.length; i += 1) {
const fun = node.functions[i];
const operator = node.operators[i];

if (operator) {
importedFunctions.push(`${fun} as ${operator}`);
} else {
importedFunctions.push(fun);
}
}

parts.push('{');
parts.push(
printSeparatedList(importedFunctions, {
firstSeparator: options.bracketSpacing ? line : softline
})
);
parts.push('}');
} else {
parts.push(node.libraryName);
}

parts.push(' for ');
parts.push(node.typeName ? path.call(print, 'typeName') : '*');
parts.push(node.isGlobal ? ' global;' : ';');

return parts;
}
};

module.exports = UsingForDeclaration;
6 changes: 6 additions & 0 deletions tests/format/AllSolidityFeatures/AllSolidityFeatures.sol
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,9 @@ contract WithUncheckedBlock {
unchecked { return x * y; }
}
}

// user-defined operators
using { add as + } for Fixed18 global ;
using { add as + , sub as - } for Fixed18 global ;
using { add , sub as - } for Fixed18 global ;
using { add as + , sub } for Fixed18 global ;
12 changes: 12 additions & 0 deletions tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ contract WithUncheckedBlock {
}
}
// user-defined operators
using { add as + } for Fixed18 global ;
using { add as + , sub as - } for Fixed18 global ;
using { add , sub as - } for Fixed18 global ;
using { add as + , sub } for Fixed18 global ;
=====================================output=====================================
// Examples taken from the Solidity documentation online.
Expand Down Expand Up @@ -1136,5 +1142,11 @@ contract WithUncheckedBlock {
}
}
// user-defined operators
using {add as +} for Fixed18 global;
using {add as +, sub as -} for Fixed18 global;
using {add, sub as -} for Fixed18 global;
using {add as +, sub} for Fixed18 global;
================================================================================
`;

0 comments on commit 838e1d9

Please sign in to comment.