Skip to content

Commit a63b0b7

Browse files
authored
Merge pull request #814 from prettier-solidity/user-defined-operators
User defined operators
2 parents 136b30b + 838e1d9 commit a63b0b7

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"webpack-cli": "^5.0.1"
103103
},
104104
"dependencies": {
105-
"@solidity-parser/parser": "^0.15.0",
105+
"@solidity-parser/parser": "^0.16.0",
106106
"semver": "^7.3.8",
107107
"solidity-comments-extractor": "^0.0.7"
108108
},

src/nodes/UsingForDeclaration.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,39 @@ const {
77
const { printSeparatedList } = require('../common/printer-helpers');
88

99
const UsingForDeclaration = {
10-
print: ({ node, path, print, options }) => [
11-
'using ',
12-
node.functions && node.functions.length
13-
? [
14-
'{',
15-
printSeparatedList(node.functions, {
16-
firstSeparator: options.bracketSpacing ? line : softline
17-
}),
18-
'}'
19-
]
20-
: node.libraryName,
21-
' for ',
22-
node.typeName ? path.call(print, 'typeName') : '*',
23-
node.isGlobal ? ' global;' : ';'
24-
]
10+
print: ({ node, path, print, options }) => {
11+
const parts = ['using '];
12+
13+
if (node.functions && node.functions.length) {
14+
const importedFunctions = [];
15+
for (let i = 0; i < node.functions.length; i += 1) {
16+
const fun = node.functions[i];
17+
const operator = node.operators[i];
18+
19+
if (operator) {
20+
importedFunctions.push(`${fun} as ${operator}`);
21+
} else {
22+
importedFunctions.push(fun);
23+
}
24+
}
25+
26+
parts.push('{');
27+
parts.push(
28+
printSeparatedList(importedFunctions, {
29+
firstSeparator: options.bracketSpacing ? line : softline
30+
})
31+
);
32+
parts.push('}');
33+
} else {
34+
parts.push(node.libraryName);
35+
}
36+
37+
parts.push(' for ');
38+
parts.push(node.typeName ? path.call(print, 'typeName') : '*');
39+
parts.push(node.isGlobal ? ' global;' : ';');
40+
41+
return parts;
42+
}
2543
};
2644

2745
module.exports = UsingForDeclaration;

tests/format/AllSolidityFeatures/AllSolidityFeatures.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,9 @@ contract WithUncheckedBlock {
534534
unchecked { return x * y; }
535535
}
536536
}
537+
538+
// user-defined operators
539+
using { add as + } for Fixed18 global ;
540+
using { add as + , sub as - } for Fixed18 global ;
541+
using { add , sub as - } for Fixed18 global ;
542+
using { add as + , sub } for Fixed18 global ;

tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ contract WithUncheckedBlock {
543543
}
544544
}
545545
546+
// user-defined operators
547+
using { add as + } for Fixed18 global ;
548+
using { add as + , sub as - } for Fixed18 global ;
549+
using { add , sub as - } for Fixed18 global ;
550+
using { add as + , sub } for Fixed18 global ;
551+
546552
=====================================output=====================================
547553
// Examples taken from the Solidity documentation online.
548554
@@ -1136,5 +1142,11 @@ contract WithUncheckedBlock {
11361142
}
11371143
}
11381144
1145+
// user-defined operators
1146+
using {add as +} for Fixed18 global;
1147+
using {add as +, sub as -} for Fixed18 global;
1148+
using {add, sub as -} for Fixed18 global;
1149+
using {add as +, sub} for Fixed18 global;
1150+
11391151
================================================================================
11401152
`;

0 commit comments

Comments
 (0)