diff --git a/package-lock.json b/package-lock.json index c0e5010df..37b04eb26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.1.2", "license": "MIT", "dependencies": { - "@solidity-parser/parser": "^0.15.0", + "@solidity-parser/parser": "^0.16.0", "semver": "^7.3.8", "solidity-comments-extractor": "^0.0.7" }, @@ -1234,9 +1234,9 @@ } }, "node_modules/@solidity-parser/parser": { - "version": "0.15.0", - "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.15.0.tgz", - "integrity": "sha512-5UFJJTzWi1hgFk6aGCZ5rxG2DJkCJOzJ74qg7UkWSNCDSigW+CJLoYUb5bLiKrtI34Nr9rpFSUNHfkqtlL+N/w==", + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.16.0.tgz", + "integrity": "sha512-ESipEcHyRHg4Np4SqBCfcXwyxxna1DgFVz69bgpLV8vzl/NP1DtcKsJ4dJZXWQhY/Z4J2LeKBiOkOVZn9ct33Q==", "dependencies": { "antlr4ts": "^0.5.0-alpha.4" } diff --git a/package.json b/package.json index 353e5e076..e35d5f017 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/nodes/UsingForDeclaration.js b/src/nodes/UsingForDeclaration.js index 76a709bec..d855ac364 100644 --- a/src/nodes/UsingForDeclaration.js +++ b/src/nodes/UsingForDeclaration.js @@ -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; diff --git a/tests/format/AllSolidityFeatures/AllSolidityFeatures.sol b/tests/format/AllSolidityFeatures/AllSolidityFeatures.sol index 2974b4f27..7f299ad03 100644 --- a/tests/format/AllSolidityFeatures/AllSolidityFeatures.sol +++ b/tests/format/AllSolidityFeatures/AllSolidityFeatures.sol @@ -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 ; diff --git a/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap b/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap index cbdbb4bf7..cbb241d61 100644 --- a/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap @@ -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. @@ -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; + ================================================================================ `;