|
1 | 1 | const {
|
2 | 2 | doc: {
|
3 |
| - builders: { group, indent, label, softline } |
| 3 | + builders: { group, hardline, indent, label, softline } |
4 | 4 | }
|
5 | 5 | } = require('prettier');
|
| 6 | +const printComments = require('./print-comments'); |
6 | 7 |
|
7 | 8 | const isEndOfChain = (node, path) => {
|
8 | 9 | let i = 0;
|
@@ -94,33 +95,68 @@ const isEndOfChain = (node, path) => {
|
94 | 95 | * be printed.
|
95 | 96 | */
|
96 | 97 | 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( |
98 | 107 | (element) => element.label === 'separator'
|
99 | 108 | );
|
100 | 109 | // The doc[] before the first separator
|
101 |
| - const firstExpression = chain.slice(0, firstSeparatorIndex); |
| 110 | + const firstExpression = chainContent.slice(0, firstSeparatorIndex); |
102 | 111 | // The doc[] containing the rest of the chain
|
103 |
| - const restOfChain = group(indent(chain.slice(firstSeparatorIndex))); |
| 112 | + const restOfChain = group(indent(chainContent.slice(firstSeparatorIndex))); |
104 | 113 |
|
105 | 114 | // We wrap the expression in a label in case there is an IndexAccess or
|
106 | 115 | // a FunctionCall following this MemberAccess.
|
107 |
| - return label('MemberAccessChain', group([firstExpression, restOfChain])); |
| 116 | + return label('MemberAccessChain', [ |
| 117 | + comments, |
| 118 | + group([firstExpression, restOfChain]) |
| 119 | + ]); |
108 | 120 | };
|
109 | 121 |
|
110 | 122 | 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 | + |
112 | 143 | 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]; |
116 | 147 |
|
117 | 148 | const doc = [
|
118 |
| - expressionDoc, |
| 149 | + comments, |
| 150 | + ...expressionDoc, |
119 | 151 | label('separator', [softline, '.']),
|
120 | 152 | node.memberName
|
121 | 153 | ].flat();
|
122 | 154 |
|
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; |
124 | 160 | }
|
125 | 161 | };
|
126 | 162 |
|
|
0 commit comments