Skip to content

Commit f03ebc4

Browse files
committed
Grouping all comments in a chain at the beginning of it
1 parent 966101f commit f03ebc4

File tree

6 files changed

+231
-13
lines changed

6 files changed

+231
-13
lines changed

src/comments/handler.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
} = require('../prettier-comments/language-js/comments');
77

88
const handleContractDefinitionComments = require('./handlers/ContractDefinition');
9+
const handleMemberAccessComments = require('./handlers/MemberAccess');
910

1011
function solidityHandleOwnLineComment(
1112
comment,
@@ -26,6 +27,7 @@ function solidityHandleOwnLineComment(
2627

2728
if (
2829
handleContractDefinitionComments(...handlerArguments) ||
30+
handleMemberAccessComments(...handlerArguments) ||
2931
handleOwnLineComment(comment, text, options, ast, isLastComment)
3032
) {
3133
return true;
@@ -52,6 +54,7 @@ function solidityHandleEndOfLineComment(
5254

5355
if (
5456
handleContractDefinitionComments(...handlerArguments) ||
57+
handleMemberAccessComments(...handlerArguments) ||
5558
handleEndOfLineComment(comment, text, options, ast, isLastComment)
5659
) {
5760
return true;
@@ -78,6 +81,7 @@ function solidityHandleRemainingComment(
7881

7982
if (
8083
handleContractDefinitionComments(...handlerArguments) ||
84+
handleMemberAccessComments(...handlerArguments) ||
8185
handleRemainingComment(comment, text, options, ast, isLastComment)
8286
) {
8387
return true;

src/comments/handlers/MemberAccess.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const {
2+
util: { addDanglingComment }
3+
} = require('prettier');
4+
5+
function handleMemberAccessComments(
6+
text,
7+
precedingNode,
8+
enclosingNode,
9+
followingNode,
10+
comment
11+
) {
12+
if (enclosingNode && enclosingNode.type === 'MemberAccess') {
13+
addDanglingComment(enclosingNode, comment);
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
module.exports = handleMemberAccessComments;

src/nodes/MemberAccess.js

+47-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const {
22
doc: {
3-
builders: { group, indent, label, softline }
3+
builders: { group, hardline, indent, label, softline }
44
}
55
} = require('prettier');
6+
const printComments = require('./print-comments');
67

78
const isEndOfChain = (node, path) => {
89
let i = 0;
@@ -94,33 +95,68 @@ const isEndOfChain = (node, path) => {
9495
* be printed.
9596
*/
9697
const processChain = (chain) => {
97-
const firstSeparatorIndex = chain.findIndex(
98+
// Extract comments and reverse the order of print.
99+
const comments = chain
100+
.filter((element) => element.label && element.label === 'comments')
101+
.reverse();
102+
const chainContent = chain.filter(
103+
(element) => !element.label || element.label !== 'comments'
104+
);
105+
106+
const firstSeparatorIndex = chainContent.findIndex(
98107
(element) => element.label === 'separator'
99108
);
100109
// The doc[] before the first separator
101-
const firstExpression = chain.slice(0, firstSeparatorIndex);
110+
const firstExpression = chainContent.slice(0, firstSeparatorIndex);
102111
// The doc[] containing the rest of the chain
103-
const restOfChain = group(indent(chain.slice(firstSeparatorIndex)));
112+
const restOfChain = group(indent(chainContent.slice(firstSeparatorIndex)));
104113

105114
// We wrap the expression in a label in case there is an IndexAccess or
106115
// a FunctionCall following this MemberAccess.
107-
return label('MemberAccessChain', group([firstExpression, restOfChain]));
116+
return label('MemberAccessChain', [
117+
comments,
118+
group([firstExpression, restOfChain])
119+
]);
108120
};
109121

110122
const MemberAccess = {
111-
print: ({ node, path, print }) => {
123+
print: ({ node, path, print, options }) => {
124+
let comments;
125+
const extractComments = (expressionPath) => {
126+
const expression = expressionPath.getValue();
127+
if (expression.type === 'FunctionCall') {
128+
expressionPath.call(extractComments, 'expression');
129+
return;
130+
}
131+
if (expression.type === 'IndexAccess') {
132+
expressionPath.call(extractComments, 'base');
133+
return;
134+
}
135+
comments = printComments(expression, path, options);
136+
if (comments) {
137+
comments = label('comments', [comments, hardline]);
138+
}
139+
};
140+
141+
path.call(extractComments, 'expression');
142+
112143
let expressionDoc = path.call(print, 'expression');
113-
if (Array.isArray(expressionDoc)) {
114-
expressionDoc = expressionDoc.flat();
115-
}
144+
expressionDoc = Array.isArray(expressionDoc)
145+
? expressionDoc.flat()
146+
: [expressionDoc];
116147

117148
const doc = [
118-
expressionDoc,
149+
comments,
150+
...expressionDoc,
119151
label('separator', [softline, '.']),
120152
node.memberName
121153
].flat();
122154

123-
return isEndOfChain(node, path) ? processChain(doc) : doc;
155+
if (isEndOfChain(node, path)) {
156+
extractComments(path);
157+
return processChain([comments, doc].flat());
158+
}
159+
return doc;
124160
}
125161
};
126162

tests/config/format-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
2222

2323
// Here we add files that will not be the same when formatting a second time.
2424
const unstableTests = new Map(
25-
["Comments/Comments.sol"].map((fixture) => {
25+
["Comments/Comments.sol", "MemberAccess/MemberAccess.sol"].map((fixture) => {
2626
const [file, isUnstable = () => true] = Array.isArray(fixture)
2727
? fixture
2828
: [fixture];
@@ -32,7 +32,7 @@ const unstableTests = new Map(
3232

3333
// Here we add files that will not have the same AST after being formatted.
3434
const unstableAstTests = new Map(
35-
[].map((fixture) => {
35+
["MemberAccess/MemberAccess.sol"].map((fixture) => {
3636
const [file, isAstUnstable = () => true] = Array.isArray(fixture)
3737
? fixture
3838
: [fixture];

tests/format/MemberAccess/MemberAccess.sol

+53
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,59 @@ contract MemberAccess {
5757
path,
5858
address(this, aoeu, aoeueu, aoeu)
5959
);
60+
61+
// Comment before chain
62+
game.
63+
// CONFIG
64+
config.
65+
// Comment 1
66+
// Comment 2
67+
resolveWindow.
68+
// Comment 3
69+
functionCall(/* inside function comment */).
70+
// Comment 4
71+
array[/* inside array comment */ 1].
72+
// Comment 5
73+
call{value: 10, gas: 800}();
74+
75+
76+
// Comment before chain
77+
game.
78+
// CONFIG
79+
config.
80+
// Comment 1
81+
// Comment 2
82+
resolveWindow.
83+
// Comment 3
84+
functionCall(/* inside function comment */).
85+
// Comment 4
86+
array[/* inside array comment */ 1].
87+
// Comment 5
88+
endOfChain;
89+
90+
91+
// Comment before chain
92+
game.
93+
// CONFIG
94+
config.
95+
// Comment 1
96+
// Comment 2
97+
resolveWindow.
98+
// Comment 3
99+
functionCall(/* inside function comment */).
100+
// Comment 4
101+
endOfChain;
102+
103+
104+
// Comment before chain
105+
game.
106+
// CONFIG
107+
config.
108+
// Comment 1
109+
// Comment 2
110+
resolveWindow.
111+
// Comment 3
112+
endOfChain;
60113
}
61114
}
62115

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

+106
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,59 @@ contract MemberAccess {
6565
path,
6666
address(this, aoeu, aoeueu, aoeu)
6767
);
68+
69+
// Comment before chain
70+
game.
71+
// CONFIG
72+
config.
73+
// Comment 1
74+
// Comment 2
75+
resolveWindow.
76+
// Comment 3
77+
functionCall(/* inside function comment */).
78+
// Comment 4
79+
array[/* inside array comment */ 1].
80+
// Comment 5
81+
call{value: 10, gas: 800}();
82+
83+
84+
// Comment before chain
85+
game.
86+
// CONFIG
87+
config.
88+
// Comment 1
89+
// Comment 2
90+
resolveWindow.
91+
// Comment 3
92+
functionCall(/* inside function comment */).
93+
// Comment 4
94+
array[/* inside array comment */ 1].
95+
// Comment 5
96+
endOfChain;
97+
98+
99+
// Comment before chain
100+
game.
101+
// CONFIG
102+
config.
103+
// Comment 1
104+
// Comment 2
105+
resolveWindow.
106+
// Comment 3
107+
functionCall(/* inside function comment */).
108+
// Comment 4
109+
endOfChain;
110+
111+
112+
// Comment before chain
113+
game.
114+
// CONFIG
115+
config.
116+
// Comment 1
117+
// Comment 2
118+
resolveWindow.
119+
// Comment 3
120+
endOfChain;
68121
}
69122
}
70123
@@ -195,6 +248,59 @@ contract MemberAccess {
195248
path,
196249
address(this, aoeu, aoeueu, aoeu)
197250
);
251+
252+
// Comment before chain
253+
// CONFIG
254+
// Comment 1
255+
// Comment 2
256+
// Comment 3
257+
// Comment 4
258+
// Comment 5
259+
game
260+
.config
261+
.resolveWindow
262+
.functionCall() /* inside function comment */
263+
.array[
264+
/* inside array comment */
265+
1
266+
]
267+
.call{value: 10, gas: 800}();
268+
269+
// Comment before chain
270+
// CONFIG
271+
// Comment 1
272+
// Comment 2
273+
// Comment 3
274+
// Comment 4
275+
// Comment 5
276+
game
277+
.config
278+
.resolveWindow
279+
.functionCall() /* inside function comment */
280+
.array[
281+
/* inside array comment */
282+
1
283+
]
284+
.endOfChain;
285+
286+
// Comment before chain
287+
// CONFIG
288+
// Comment 1
289+
// Comment 2
290+
// Comment 3
291+
// Comment 4
292+
game
293+
.config
294+
.resolveWindow
295+
.functionCall() /* inside function comment */
296+
.endOfChain;
297+
298+
// Comment before chain
299+
// CONFIG
300+
// Comment 1
301+
// Comment 2
302+
// Comment 3
303+
game.config.resolveWindow.endOfChain;
198304
}
199305
}
200306

0 commit comments

Comments
 (0)