Skip to content

Commit

Permalink
Function calls set group (#524)
Browse files Browse the repository at this point in the history
* including the last ')' in the group so if the call reaches the charLimit with the ')' it will break

* function calls now set up a groupId and expose it via labels

* MemberAccess reacts accordingly to the function calls groupId
  • Loading branch information
Janther committed Jun 5, 2021
1 parent 562d00f commit 594c158
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 58 deletions.
73 changes: 46 additions & 27 deletions src/nodes/FunctionCall.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
const {
doc: {
builders: { group, line, softline }
builders: { label, line, softline }
}
} = require('prettier/standalone');

const printSeparatedList = require('./print-separated-list');

const printObject = (node, path, print, options) =>
group([
'{',
printSeparatedList(
path
.map(print, 'arguments')
.map((arg, index) => [node.names[index], ': ', arg]),
{ firstSeparator: options.bracketSpacing ? line : softline }
),
'}'
]);
let functionCallId = 0;

const printArguments = (node, path, print, options) => {
if (node.names && node.names.length > 0) {
return printObject(node, path, print, options);
}
if (node.arguments && node.arguments.length > 0) {
return printSeparatedList(path.map(print, 'arguments'));
}
return '';
};
const printObject = (node, path, print, options, groupId) => [
'{',
printSeparatedList(
path
.map(print, 'arguments')
.map((arg, index) => [node.names[index], ': ', arg]),
{
groupId,
firstSeparator: options.bracketSpacing ? line : softline,
lastSeparator: [options.bracketSpacing ? line : softline, '})']
}
)
];

const printArguments = (path, print, groupId) =>
printSeparatedList(path.map(print, 'arguments'), {
groupId,
lastSeparator: [softline, ')']
});

const FunctionCall = {
print: ({ node, path, print, options }) => [
path.call(print, 'expression'),
'(',
printArguments(node, path, print, options),
')'
]
print: ({ node, path, print, options }) => {
let argumentsDoc = ')';
const functionCallLabel = { type: 'FunctionCall', groupId: null };
if (node.arguments && node.arguments.length > 0) {
functionCallLabel.groupId = `FunctionCall-${functionCallId}`;
functionCallId += 1;
if (node.names && node.names.length > 0) {
argumentsDoc = printObject(
node,
path,
print,
options,
functionCallLabel.groupId
);
} else {
argumentsDoc = printArguments(path, print, functionCallLabel.groupId);
}
}

return label(JSON.stringify(functionCallLabel), [
path.call(print, 'expression'),
'(',
argumentsDoc
]);
}
};

module.exports = FunctionCall;
21 changes: 16 additions & 5 deletions src/nodes/MemberAccess.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
doc: {
builders: { group, indent, softline }
builders: { group, ifBreak, indent, softline }
}
} = require('prettier/standalone');

Expand Down Expand Up @@ -33,11 +33,22 @@ const shallIndent = (path) => {

const MemberAccess = {
print: ({ node, path, print }) => {
const expressionDoc = path.call(print, 'expression');
let separator = [softline, '.'];
let labelData;
if (expressionDoc.label) {
labelData = JSON.parse(expressionDoc.label);
}
if (labelData && labelData.groupId) {
separator = ifBreak('.', [softline, '.'], {
groupId: labelData.groupId
});
}

const doc = [
path.call(print, 'expression'),
shallIndent(path)
? indent([softline, '.', node.memberName])
: [softline, '.', node.memberName]
expressionDoc,
shallIndent(path) ? indent(separator) : separator,
node.memberName
];

return isBeginnigOfChain(path) ? group(doc) : doc;
Expand Down
8 changes: 6 additions & 2 deletions src/nodes/print-separated-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ const {
const printSeparatedList = (
list,
{
groupId = undefined,
firstSeparator = softline,
separator = [',', line],
lastSeparator = firstSeparator
} = {}
) => group([indent([firstSeparator, join(separator, list)]), lastSeparator]);

) =>
group(
[indent([firstSeparator, join(separator, list)]), lastSeparator],
groupId ? { id: groupId } : {}
);
module.exports = printSeparatedList;
36 changes: 12 additions & 24 deletions tests/FunctionCalls/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV2(
)._hashTypedDataV2(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -94,8 +93,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV3(
)._hashTypedDataV3(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -107,8 +105,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
.recover(signature);
).recover(signature);
signer = _hashTypedDataV1(
keccak256(
abi.encode(
Expand All @@ -121,8 +118,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV2(
)._hashTypedDataV2(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -134,8 +130,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV3(
)._hashTypedDataV3(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -147,8 +142,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
.recover(signature);
).recover(signature);
return _nonces[req.from] == req.nonce && signer == req.from;
}
}
Expand Down Expand Up @@ -236,8 +230,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV2(
)._hashTypedDataV2(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -249,8 +242,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV3(
)._hashTypedDataV3(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -262,8 +254,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
.recover(signature);
).recover(signature);
signer = _hashTypedDataV1(
keccak256(
abi.encode(
Expand All @@ -276,8 +267,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV2(
)._hashTypedDataV2(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -289,8 +279,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
._hashTypedDataV3(
)._hashTypedDataV3(
keccak256(
abi.encode(
TYPEHASH,
Expand All @@ -302,8 +291,7 @@ contract FunctionCalls {
keccak256(req.data)
)
)
)
.recover(signature);
).recover(signature);
return _nonces[req.from] == req.nonce && signer == req.from;
}
}
Expand Down

0 comments on commit 594c158

Please sign in to comment.