Skip to content

Commit

Permalink
Adding an extra rule for splitting params if the list is longer than …
Browse files Browse the repository at this point in the history
…2 independent of the length on the line (#257)
  • Loading branch information
Janther committed Apr 12, 2020
1 parent b1c4e12 commit 66bf27f
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 70 deletions.
12 changes: 10 additions & 2 deletions src/nodes/FunctionDefinition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
doc: {
builders: { concat, dedent, group, indent, join, line }
builders: { concat, dedent, group, hardline, indent, join, line }
}
} = require('prettier/standalone');

Expand All @@ -25,7 +25,15 @@ const functionName = (node, options) => {

const parameters = (parametersType, node, path, print) =>
node[parametersType] && node[parametersType].length > 0
? printSeparatedList(path.map(print, parametersType))
? printSeparatedList(path.map(print, parametersType), {
separator: concat([
',',
// To keep consistency any list of parameters will split if it's longer than 2.
// For more information see:
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
node[parametersType].length > 2 ? hardline : line
])
})
: '';

const visibility = (node) =>
Expand Down
16 changes: 14 additions & 2 deletions src/nodes/ModifierDefinition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
doc: {
builders: { concat }
builders: { concat, hardline, line }
}
} = require('prettier/standalone');

Expand All @@ -9,7 +9,19 @@ const printSeparatedList = require('./print-separated-list');
const modifierParameters = (node, path, print) => {
if (node.parameters) {
return node.parameters.length > 0
? concat(['(', printSeparatedList(path.map(print, 'parameters')), ')'])
? concat([
'(',
printSeparatedList(path.map(print, 'parameters'), {
separator: concat([
',',
// To keep consistency any list of parameters will split if it's longer than 2.
// For more information see:
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
node.parameters.length > 2 ? hardline : line
])
}),
')'
])
: '()';
}

Expand Down
18 changes: 15 additions & 3 deletions tests/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,11 @@ contract DualIndex {
admin = msg.sender;
}
function set(uint256 key1, uint256 key2, uint256 value) restricted {
function set(
uint256 key1,
uint256 key2,
uint256 value
) restricted {
uint256[2][4] memory defaults; // "memory" broke things at one time.
data[key1][key2] = value;
}
Expand Down Expand Up @@ -870,13 +874,21 @@ contract Ballot {
function foobar()
payable
owner(myPrice)
returns (uint256[], address myAdd, string[] names)
returns (
uint256[],
address myAdd,
string[] names
)
{}
function foobar()
payable
owner(myPrice)
returns (uint256[], address myAdd, string[] names);
returns (
uint256[],
address myAdd,
string[] names
);
Voter you = Voter(1, true);
Expand Down
48 changes: 43 additions & 5 deletions tests/ExplicitVariableTypes/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ contract VariableTypesMixed {
uint256 _e,
int256 _f,
bytes1 _g
) public returns (uint256, int256, bytes1, uint256, int256, bytes1) {
)
public
returns (
uint256,
int256,
bytes1,
uint256,
int256,
bytes1
)
{
emit Event(_a, _b, _c, _e, _f, _g);
return (_a, _b, _c, _e, _f, _g);
}
Expand Down Expand Up @@ -130,9 +140,23 @@ contract VariableTypesMixed {
event Event(uint _a, int _b, byte _c, uint _e, int _f, byte _g);
function func(uint _a, int _b, byte _c, uint _e, int _f, byte _g)
function func(
uint _a,
int _b,
byte _c,
uint _e,
int _f,
byte _g
)
public
returns (uint, int, byte, uint, int, byte)
returns (
uint,
int,
byte,
uint,
int,
byte
)
{
emit Event(_a, _b, _c, _e, _f, _g);
return (_a, _b, _c, _e, _f, _g);
Expand Down Expand Up @@ -195,9 +219,23 @@ contract VariableTypesMixed {
event Event(uint _a, int256 _b, bytes1 _c, uint256 _e, int _f, byte _g);
function func(uint256 _a, int256 _b, byte _c, uint _e, int _f, bytes1 _g)
function func(
uint256 _a,
int256 _b,
byte _c,
uint _e,
int _f,
bytes1 _g
)
public
returns (uint, int256, byte, uint256, int, bytes1)
returns (
uint,
int256,
byte,
uint256,
int,
bytes1
)
{
emit Event(_a, _b, _c, _e, _f, _g);
return (_a, _b, _c, _e, _f, _g);
Expand Down
106 changes: 83 additions & 23 deletions tests/FunctionDefinitions/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,32 @@ interface FunctionInterfaces {
uint256 y10
);
function someParamsSomeModifiers(uint256 x1, uint256 x2, uint256 x3)
modifier1
modifier2
modifier3;
function someParamsSomeModifiers(
uint256 x1,
uint256 x2,
uint256 x3
) modifier1 modifier2 modifier3;
function someParamsSomeReturns(uint256 x1, uint256 x2, uint256 x3)
returns (uint256 y1, uint256 y2, uint256 y3);
function someParamsSomeReturns(
uint256 x1,
uint256 x2,
uint256 x3
)
returns (
uint256 y1,
uint256 y2,
uint256 y3
);
function someModifiersSomeReturns()
modifier1
modifier2
modifier3
returns (uint256 y1, uint256 y2, uint256 y3);
returns (
uint256 y1,
uint256 y2,
uint256 y3
);
function someParamSomeModifiersSomeReturns(
uint256 x1,
Expand All @@ -190,9 +203,17 @@ interface FunctionInterfaces {
modifier1
modifier2
modifier3
returns (uint256 y1, uint256 y2, uint256 y3);
returns (
uint256 y1,
uint256 y2,
uint256 y3
);
function someParamsManyModifiers(uint256 x1, uint256 x2, uint256 x3)
function someParamsManyModifiers(
uint256 x1,
uint256 x2,
uint256 x3
)
modifier1
modifier2
modifier3
Expand All @@ -204,7 +225,11 @@ interface FunctionInterfaces {
modifier9
modifier10;
function someParamsManyReturns(uint256 x1, uint256 x2, uint256 x3)
function someParamsManyReturns(
uint256 x1,
uint256 x2,
uint256 x3
)
returns (
uint256 y1,
uint256 y2,
Expand Down Expand Up @@ -242,7 +267,12 @@ interface FunctionInterfaces {
uint256 x8,
uint256 x9,
uint256 x10
) returns (uint256 y1, uint256 y2, uint256 y3);
)
returns (
uint256 y1,
uint256 y2,
uint256 y3
);
function manyParamsManyModifiers(
uint256 x1,
Expand Down Expand Up @@ -403,16 +433,24 @@ contract FunctionDefinitions {
a = 1;
}
function someParamsSomeModifiers(uint256 x1, uint256 x2, uint256 x3)
modifier1
modifier2
modifier3
{
function someParamsSomeModifiers(
uint256 x1,
uint256 x2,
uint256 x3
) modifier1 modifier2 modifier3 {
a = 1;
}
function someParamsSomeReturns(uint256 x1, uint256 x2, uint256 x3)
returns (uint256 y1, uint256 y2, uint256 y3)
function someParamsSomeReturns(
uint256 x1,
uint256 x2,
uint256 x3
)
returns (
uint256 y1,
uint256 y2,
uint256 y3
)
{
a = 1;
}
Expand All @@ -421,7 +459,11 @@ contract FunctionDefinitions {
modifier1
modifier2
modifier3
returns (uint256 y1, uint256 y2, uint256 y3)
returns (
uint256 y1,
uint256 y2,
uint256 y3
)
{
a = 1;
}
Expand All @@ -434,12 +476,20 @@ contract FunctionDefinitions {
modifier1
modifier2
modifier3
returns (uint256 y1, uint256 y2, uint256 y3)
returns (
uint256 y1,
uint256 y2,
uint256 y3
)
{
a = 1;
}
function someParamsManyModifiers(uint256 x1, uint256 x2, uint256 x3)
function someParamsManyModifiers(
uint256 x1,
uint256 x2,
uint256 x3
)
modifier1
modifier2
modifier3
Expand All @@ -454,7 +504,11 @@ contract FunctionDefinitions {
a = 1;
}
function someParamsManyReturns(uint256 x1, uint256 x2, uint256 x3)
function someParamsManyReturns(
uint256 x1,
uint256 x2,
uint256 x3
)
returns (
uint256 y1,
uint256 y2,
Expand Down Expand Up @@ -497,7 +551,13 @@ contract FunctionDefinitions {
uint256 x8,
uint256 x9,
uint256 x10
) returns (uint256 y1, uint256 y2, uint256 y3) {
)
returns (
uint256 y1,
uint256 y2,
uint256 y3
)
{
a = 1;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ModifierDefinitions/ModifierDefinitions.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
contract ModifierDefinitions {
modifier noParams() {}
modifier oneParam(uint a) {}
modifier twoParams(uint a,uint b) {}
modifier threeParams(uint a,uint b ,uint c) {}
}
22 changes: 22 additions & 0 deletions tests/ModifierDefinitions/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ModifierDefinitions.sol 1`] = `
contract ModifierDefinitions {
modifier noParams() {}
modifier oneParam(uint a) {}
modifier twoParams(uint a,uint b) {}
modifier threeParams(uint a,uint b ,uint c) {}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contract ModifierDefinitions {
modifier noParams() {}
modifier oneParam(uint256 a) {}
modifier twoParams(uint256 a, uint256 b) {}
modifier threeParams(
uint256 a,
uint256 b,
uint256 c
) {}
}
`;
1 change: 1 addition & 0 deletions tests/ModifierDefinitions/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname);
9 changes: 5 additions & 4 deletions tests/SplittableCommodity/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ contract SplittableCommodity is MintableCommodity {
bytes operatorData
);
function split(uint256 _tokenId, address _to, uint256 _amount)
public
whenNotPaused
{
function split(
uint256 _tokenId,
address _to,
uint256 _amount
) public whenNotPaused {
address supplierProxy = IContractRegistry(contractRegistry)
.getLatestProxyAddr("Supplier");
require(
Expand Down
Loading

0 comments on commit 66bf27f

Please sign in to comment.