Skip to content

Commit

Permalink
Merge pull request #814 from prettier-solidity/user-defined-operators
Browse files Browse the repository at this point in the history
User defined operators
  • Loading branch information
fvictorio authored Mar 1, 2023
2 parents 136b30b + 838e1d9 commit a63b0b7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"webpack-cli": "^5.0.1"
},
"dependencies": {
"@solidity-parser/parser": "^0.15.0",
"@solidity-parser/parser": "^0.16.0",
"semver": "^7.3.8",
"solidity-comments-extractor": "^0.0.7"
},
Expand Down
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 a63b0b7

Please sign in to comment.