Skip to content

Commit 136b30b

Browse files
authored
Merge pull request #801 from prettier-solidity/fix-empty-structs
Format empty structs correctly
2 parents 67c76ad + bc1b8d7 commit 136b30b

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/nodes/StructDefinition.js

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

99
const StructDefinition = {
10-
print: ({ node, path, print }) => [
11-
'struct ',
12-
node.name,
13-
' {',
14-
printSeparatedList(path.map(print, 'members'), {
15-
firstSeparator: hardline,
16-
separator: [';', hardline],
17-
lastSeparator: [';', hardline]
18-
}),
19-
'}'
20-
]
10+
print: ({ node, path, print }) => {
11+
const parts = ['struct ', node.name, ' {'];
12+
13+
if (node.members.length > 0) {
14+
parts.push(
15+
printSeparatedList(path.map(print, 'members'), {
16+
firstSeparator: hardline,
17+
separator: [';', hardline],
18+
lastSeparator: [';', hardline]
19+
})
20+
);
21+
}
22+
23+
parts.push('}');
24+
25+
return parts;
26+
}
2127
};
2228

2329
module.exports = StructDefinition;

tests/format/Issues/Issue799.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct EmptyStruct {}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,17 @@ contract Issue564 {
170170
171171
================================================================================
172172
`;
173+
174+
exports[`Issue799.sol format 1`] = `
175+
====================================options=====================================
176+
parsers: ["solidity-parse"]
177+
printWidth: 80
178+
| printWidth
179+
=====================================input======================================
180+
struct EmptyStruct {}
181+
182+
=====================================output=====================================
183+
struct EmptyStruct {}
184+
185+
================================================================================
186+
`;

0 commit comments

Comments
 (0)