Skip to content

Commit d1339c6

Browse files
committed
support storage layout
1 parent af7d896 commit d1339c6

6 files changed

Lines changed: 284 additions & 37 deletions

File tree

src/nodes/ContractDefinition.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,37 @@ import {
66
printSeparatedList
77
} from '../common/printer-helpers.js';
88

9-
const { group, line, hardline } = doc.builders;
9+
const { group, hardline, ifBreak, line, softline } = doc.builders;
1010

11-
const inheritance = (node, path, print) =>
12-
node.baseContracts.length > 0
13-
? [
14-
' is',
15-
printSeparatedList(path.map(print, 'baseContracts'), {
16-
firstSeparator: line
17-
})
18-
]
19-
: line;
11+
const specifiers = (node, path, print) => {
12+
const document = [];
13+
if (node.baseContracts.length > 0) {
14+
document.push([
15+
'is',
16+
printSeparatedList(path.map(print, 'baseContracts'), {
17+
firstSeparator: line
18+
})
19+
]);
20+
}
21+
if (node.storageLayout) {
22+
document.push([
23+
'layout at',
24+
printSeparatedItem(path.call(print, 'storageLayout'), {
25+
firstSeparator: line
26+
})
27+
]);
28+
}
29+
if (document.length === 0) return line;
30+
if (document.length === 1) return [' ', document];
31+
const groupId = Symbol('ContractSpecifiers.inheritance');
32+
return printSeparatedList(
33+
[group(document[0], { id: groupId }), document[1]],
34+
{
35+
firstSeparator: line,
36+
separator: ifBreak('', softline, { groupId })
37+
}
38+
);
39+
};
2040

2141
const body = (node, path, options, print) => {
2242
const comments = printComments(node, path, options);
@@ -34,7 +54,7 @@ export const ContractDefinition = {
3454
node.kind === 'abstract' ? 'abstract contract' : node.kind,
3555
' ',
3656
node.name,
37-
inheritance(node, path, print),
57+
specifiers(node, path, print),
3858
'{'
3959
]),
4060
body(node, path, options, print),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
11
contract ContractDefinition is Contract1, Contract2, Contract3, Contract4, Contract5 {
22
}
3+
4+
contract StorageLayoutSpecifier layout at 123 {
5+
}
6+
7+
contract StorageLayoutSpecifier1 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {
8+
}
9+
10+
contract StorageLayoutSpecifier2 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890) {
11+
}
12+
13+
contract StorageLayoutSpecifier3 is Contract1 layout at 123 {
14+
}
15+
16+
contract StorageLayoutSpecifier4 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at 123{
17+
}
18+
19+
contract StorageLayoutSpecifier5 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890{
20+
}
21+
22+
contract StorageLayoutSpecifier6 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){
23+
}
24+
25+
contract StorageLayoutSpecifier7 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at 123{
26+
}
27+
28+
contract StorageLayoutSpecifier8 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890{
29+
}
30+
31+
contract StorageLayoutSpecifier9 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){
32+
}
33+
34+
contract InheritanceSpecifier1 is SomeOtherContract(1234,false) {}
35+
36+
contract InheritanceSpecifier2 is SomeOtherContract(1234,false) layout at 123 {}
37+
38+
contract InheritanceSpecifier3 is SomeOtherContract(1234,false) layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {}
39+
40+
contract InheritanceSpecifier4 is SomeOtherContract(1234,false) layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890) {}
41+
42+
contract LongInheritanceSpecifier1 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) {}
43+
44+
contract LongInheritanceSpecifier2 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at 123 {}
45+
46+
contract LongInheritanceSpecifier3 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {}
47+
48+
contract LongInheritanceSpecifier4 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){}

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

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,52 @@ printWidth: 80
99
contract ContractDefinition is Contract1, Contract2, Contract3, Contract4, Contract5 {
1010
}
1111
12+
contract StorageLayoutSpecifier layout at 123 {
13+
}
14+
15+
contract StorageLayoutSpecifier1 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {
16+
}
17+
18+
contract StorageLayoutSpecifier2 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890) {
19+
}
20+
21+
contract StorageLayoutSpecifier3 is Contract1 layout at 123 {
22+
}
23+
24+
contract StorageLayoutSpecifier4 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at 123{
25+
}
26+
27+
contract StorageLayoutSpecifier5 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890{
28+
}
29+
30+
contract StorageLayoutSpecifier6 is Contract1, Contract2, Contract3, Contract4, Contract5 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){
31+
}
32+
33+
contract StorageLayoutSpecifier7 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at 123{
34+
}
35+
36+
contract StorageLayoutSpecifier8 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at 1234567890 * 1234567890 - 1234567890 / 1234567890{
37+
}
38+
39+
contract StorageLayoutSpecifier9 is Contract1, Contract2, Contract3, Contract4, Contract5, Contract6, Contract7 layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){
40+
}
41+
42+
contract InheritanceSpecifier1 is SomeOtherContract(1234,false) {}
43+
44+
contract InheritanceSpecifier2 is SomeOtherContract(1234,false) layout at 123 {}
45+
46+
contract InheritanceSpecifier3 is SomeOtherContract(1234,false) layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {}
47+
48+
contract InheritanceSpecifier4 is SomeOtherContract(1234,false) layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890) {}
49+
50+
contract LongInheritanceSpecifier1 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) {}
51+
52+
contract LongInheritanceSpecifier2 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at 123 {}
53+
54+
contract LongInheritanceSpecifier3 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at 1234567890 * 1234567890 - 1234567890 / 1234567890 {}
55+
56+
contract LongInheritanceSpecifier4 is SomeOtherContract(123467890,false,0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c) layout at veryVeryLongFunction(12345678901234567890 * 12345678901234567890 - 12345678901234567890 / 12345678901234567890 + 12345678901234567890 - 12345678901234567890){}
57+
1258
=====================================output=====================================
1359
contract ContractDefinition is
1460
Contract1,
@@ -18,5 +64,166 @@ contract ContractDefinition is
1864
Contract5
1965
{}
2066
67+
contract StorageLayoutSpecifier layout at 123 {}
68+
69+
contract StorageLayoutSpecifier1 layout at
70+
1234567890 * 1234567890 - 1234567890 / 1234567890
71+
{}
72+
73+
contract StorageLayoutSpecifier2 layout at
74+
veryVeryLongFunction(
75+
12345678901234567890 *
76+
12345678901234567890 -
77+
12345678901234567890 /
78+
12345678901234567890 +
79+
12345678901234567890 -
80+
12345678901234567890
81+
)
82+
{}
83+
84+
contract StorageLayoutSpecifier3 is Contract1 layout at 123 {}
85+
86+
contract StorageLayoutSpecifier4
87+
is Contract1, Contract2, Contract3, Contract4, Contract5
88+
layout at 123
89+
{}
90+
91+
contract StorageLayoutSpecifier5
92+
is Contract1, Contract2, Contract3, Contract4, Contract5
93+
layout at 1234567890 * 1234567890 - 1234567890 / 1234567890
94+
{}
95+
96+
contract StorageLayoutSpecifier6
97+
is Contract1, Contract2, Contract3, Contract4, Contract5
98+
layout at
99+
veryVeryLongFunction(
100+
12345678901234567890 *
101+
12345678901234567890 -
102+
12345678901234567890 /
103+
12345678901234567890 +
104+
12345678901234567890 -
105+
12345678901234567890
106+
)
107+
108+
{}
109+
110+
contract StorageLayoutSpecifier7
111+
is
112+
Contract1,
113+
Contract2,
114+
Contract3,
115+
Contract4,
116+
Contract5,
117+
Contract6,
118+
Contract7
119+
layout at 123
120+
{}
121+
122+
contract StorageLayoutSpecifier8
123+
is
124+
Contract1,
125+
Contract2,
126+
Contract3,
127+
Contract4,
128+
Contract5,
129+
Contract6,
130+
Contract7
131+
layout at 1234567890 * 1234567890 - 1234567890 / 1234567890
132+
{}
133+
134+
contract StorageLayoutSpecifier9
135+
is
136+
Contract1,
137+
Contract2,
138+
Contract3,
139+
Contract4,
140+
Contract5,
141+
Contract6,
142+
Contract7
143+
layout at
144+
veryVeryLongFunction(
145+
12345678901234567890 *
146+
12345678901234567890 -
147+
12345678901234567890 /
148+
12345678901234567890 +
149+
12345678901234567890 -
150+
12345678901234567890
151+
)
152+
153+
{}
154+
155+
contract InheritanceSpecifier1 is SomeOtherContract(1234, false) {}
156+
157+
contract InheritanceSpecifier2
158+
is SomeOtherContract(1234, false)
159+
layout at 123
160+
{}
161+
162+
contract InheritanceSpecifier3
163+
is SomeOtherContract(1234, false)
164+
layout at 1234567890 * 1234567890 - 1234567890 / 1234567890
165+
{}
166+
167+
contract InheritanceSpecifier4
168+
is SomeOtherContract(1234, false)
169+
layout at
170+
veryVeryLongFunction(
171+
12345678901234567890 *
172+
12345678901234567890 -
173+
12345678901234567890 /
174+
12345678901234567890 +
175+
12345678901234567890 -
176+
12345678901234567890
177+
)
178+
179+
{}
180+
181+
contract LongInheritanceSpecifier1 is
182+
SomeOtherContract(
183+
123467890,
184+
false,
185+
0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
186+
)
187+
{}
188+
189+
contract LongInheritanceSpecifier2
190+
is
191+
SomeOtherContract(
192+
123467890,
193+
false,
194+
0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
195+
)
196+
layout at 123
197+
{}
198+
199+
contract LongInheritanceSpecifier3
200+
is
201+
SomeOtherContract(
202+
123467890,
203+
false,
204+
0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
205+
)
206+
layout at 1234567890 * 1234567890 - 1234567890 / 1234567890
207+
{}
208+
209+
contract LongInheritanceSpecifier4
210+
is
211+
SomeOtherContract(
212+
123467890,
213+
false,
214+
0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
215+
)
216+
layout at
217+
veryVeryLongFunction(
218+
12345678901234567890 *
219+
12345678901234567890 -
220+
12345678901234567890 /
221+
12345678901234567890 +
222+
12345678901234567890 -
223+
12345678901234567890
224+
)
225+
226+
{}
227+
21228
================================================================================
22229
`;

tests/format/InheritanceSpecifier/InheritanceSpecifier.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/format/InheritanceSpecifier/jsfmt.spec.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)