Skip to content

Commit 07351ab

Browse files
authored
solving the indentation at the beginning of a var declaration that splits (#565)
1 parent 24d7809 commit 07351ab

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/nodes/VariableDeclarationStatement.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
const {
22
doc: {
3-
builders: { group }
3+
builders: { group, ifBreak, indent }
44
}
55
} = require('prettier');
66

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

9-
const embraceVariables = (doc, embrace) => (embrace ? ['(', doc, ')'] : doc);
9+
const embraceVariables = (doc, embrace) =>
10+
embrace ? ['(', printSeparatedList(doc), ')'] : doc;
1011

1112
const initialValue = (node, path, print) =>
1213
node.initialValue ? [' = ', path.call(print, 'initialValue')] : '';
1314

15+
let groupIndex = 0;
1416
const VariableDeclarationStatement = {
1517
print: ({ node, path, print }) => {
1618
const startsWithVar =
1719
node.variables.filter((x) => x && x.typeName).length === 0;
1820

21+
const declarationDoc = group(
22+
[
23+
startsWithVar ? 'var ' : '',
24+
embraceVariables(
25+
path.map(print, 'variables'),
26+
node.variables.length > 1 || startsWithVar
27+
)
28+
],
29+
{ id: `VariableDeclarationStatement.variables-${groupIndex}` }
30+
);
31+
groupIndex += 1;
32+
const initialValueDoc = initialValue(node, path, print);
33+
1934
return group([
20-
startsWithVar ? 'var ' : '',
21-
embraceVariables(
22-
printSeparatedList(path.map(print, 'variables')),
23-
node.variables.length > 1 || startsWithVar
24-
),
25-
initialValue(node, path, print),
35+
declarationDoc,
36+
ifBreak(indent(initialValueDoc), initialValueDoc, {
37+
groupId: declarationDoc.id
38+
}),
2639
node.omitSemicolon ? '' : ';'
2740
]);
2841
}

tests/format/Issues/Issue564.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract Issue564 {
2+
function isAuthorized(
3+
bytes32 serviceId,
4+
address client
5+
) external view override returns (bool) {
6+
WhitelistStatus storage whitelistStatus = serviceIdToClientToWhitelistStatus[serviceId][client];
7+
return true;
8+
}
9+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,38 @@ contract Issue385 {
137137
138138
================================================================================
139139
`;
140+
141+
exports[`Issue564.sol format 1`] = `
142+
====================================options=====================================
143+
parsers: ["solidity-parse"]
144+
printWidth: 80
145+
| printWidth
146+
=====================================input======================================
147+
contract Issue564 {
148+
function isAuthorized(
149+
bytes32 serviceId,
150+
address client
151+
) external view override returns (bool) {
152+
WhitelistStatus storage whitelistStatus = serviceIdToClientToWhitelistStatus[serviceId][client];
153+
return true;
154+
}
155+
}
156+
157+
=====================================output=====================================
158+
contract Issue564 {
159+
function isAuthorized(bytes32 serviceId, address client)
160+
external
161+
view
162+
override
163+
returns (bool)
164+
{
165+
WhitelistStatus
166+
storage whitelistStatus = serviceIdToClientToWhitelistStatus[
167+
serviceId
168+
][client];
169+
return true;
170+
}
171+
}
172+
173+
================================================================================
174+
`;

0 commit comments

Comments
 (0)