forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomments.js
More file actions
308 lines (272 loc) · 8.91 KB
/
Copy pathcomments.js
File metadata and controls
308 lines (272 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import findLast from "lodash/findLast.js";
/**
* Search where is the position of the comment in the token array by
* using dichotomic search.
* @param {*} tokens ordered array of tokens
* @param {*} comment comment token
* @return the position of the token next to the comment
*/
function findUpperBoundToken(tokens, comment) {
let diff;
let i;
let current;
let len = tokens.length;
i = 0;
while (len) {
diff = len >>> 1;
current = i + diff;
if (tokens[current].startOffset > comment.startOffset) {
len = diff;
} else {
i = current + 1;
len -= diff + 1;
}
}
return i;
}
function isPrettierIgnoreComment(comment) {
return comment.image.match(
/(\/\/(\s*)prettier-ignore(\s*))|(\/\*(\s*)prettier-ignore(\s*)\*\/)/gm
);
}
function isFormatterOffOnComment(comment) {
return comment.image.match(
/(\/\/(\s*)@formatter:(off|on)(\s*))|(\/\*(\s*)@formatter:(off|on)(\s*)\*\/)/gm
);
}
/**
* Pre-processing of tokens in order to
* complete the parser's mostEnclosiveCstNodeByStartOffset and mostEnclosiveCstNodeByEndOffset structures.
*
* @param {ITokens[]} tokens - array of tokens
* @param {{[startOffset: number]: CSTNode}} mostEnclosiveCstNodeByStartOffset
* @param {{[endOffset: number]: CSTNode}} mostEnclosiveCstNodeByEndOffset
*/
function completeMostEnclosiveCSTNodeByOffset(
tokens,
mostEnclosiveCstNodeByStartOffset,
mostEnclosiveCstNodeByEndOffset
) {
tokens.forEach(token => {
if (mostEnclosiveCstNodeByStartOffset[token.startOffset] === undefined) {
mostEnclosiveCstNodeByStartOffset[token.startOffset] = token;
}
if (mostEnclosiveCstNodeByEndOffset[token.endOffset] === undefined) {
mostEnclosiveCstNodeByEndOffset[token.endOffset] = token;
}
});
}
function extendRangeOffset(comments, tokens) {
let position;
comments.forEach(comment => {
position = findUpperBoundToken(tokens, comment);
const extendedStartOffset =
position - 1 < 0 ? comment.startOffset : tokens[position - 1].endOffset;
const extendedEndOffset =
position == tokens.length
? comment.endOffset
: tokens[position].startOffset;
comment.extendedOffset = {
startOffset: extendedStartOffset,
endOffset: extendedEndOffset
};
});
}
/**
* Create two data structures we use to know at which offset a comment can be attached.
* - commentsByExtendedStartOffset: map a comment by the endOffset of the previous token.
* - commentsByExtendedEndOffset: map a comment by the startOffset of the next token
*
* @param {ITokens[]} tokens - array of tokens
*
* @return {{commentsByExtendedStartOffset: {[extendedStartOffset: number]: Comment[]}, commentsByExtendedEndOffset: {[extendedEndOffset: number]: Comment[]}}}
*/
function mapCommentsByExtendedRange(comments) {
const commentsByExtendedEndOffset = {};
const commentsByExtendedStartOffset = {};
comments.forEach(comment => {
const extendedStartOffset = comment.extendedOffset.startOffset;
const extendedEndOffset = comment.extendedOffset.endOffset;
if (commentsByExtendedEndOffset[extendedEndOffset] === undefined) {
commentsByExtendedEndOffset[extendedEndOffset] = [comment];
} else {
commentsByExtendedEndOffset[extendedEndOffset].push(comment);
}
if (commentsByExtendedStartOffset[extendedStartOffset] === undefined) {
commentsByExtendedStartOffset[extendedStartOffset] = [comment];
} else {
commentsByExtendedStartOffset[extendedStartOffset].push(comment);
}
});
return { commentsByExtendedEndOffset, commentsByExtendedStartOffset };
}
/**
* Determine if a comment should be attached as a trailing comment to a specific node.
* A comment should be trailing if it is on the same line than the previous token and
* not on the same line than the next token
*
* @param {*} comment
* @param {CSTNode} node
* @param {{[startOffset: number]: CSTNode}} mostEnclosiveCstNodeByStartOffset
*/
function shouldAttachTrailingComments(
comment,
node,
mostEnclosiveCstNodeByStartOffset
) {
if (isPrettierIgnoreComment(comment)) {
return false;
}
const nextNode =
mostEnclosiveCstNodeByStartOffset[comment.extendedOffset.endOffset];
// Last node of the file
if (nextNode === undefined) {
return true;
}
const nodeEndLine =
node.location !== undefined ? node.location.endLine : node.endLine;
if (comment.startLine !== nodeEndLine) {
return false;
}
const nextNodeStartLine =
nextNode.location !== undefined
? nextNode.location.startLine
: nextNode.startLine;
return comment.endLine !== nextNodeStartLine;
}
/**
* Attach comments to the most enclosive CSTNode (node or token)
*
* @param {ITokens[]} tokens
* @param {*} comments
* @param {{[startOffset: number]: CSTNode}} mostEnclosiveCstNodeByStartOffset
* @param {{[endOffset: number]: CSTNode}} mostEnclosiveCstNodeByEndOffset
*/
export function attachComments(
tokens,
comments,
mostEnclosiveCstNodeByStartOffset,
mostEnclosiveCstNodeByEndOffset
) {
// Edge case: only comments in the file
if (tokens.length === 0) {
mostEnclosiveCstNodeByStartOffset[NaN].leadingComments = comments;
return;
}
// Pre-processing phase to complete the data structures we need to attach
// a comment to the right place
completeMostEnclosiveCSTNodeByOffset(
tokens,
mostEnclosiveCstNodeByStartOffset,
mostEnclosiveCstNodeByEndOffset
);
extendRangeOffset(comments, tokens);
const { commentsByExtendedStartOffset, commentsByExtendedEndOffset } =
mapCommentsByExtendedRange(comments);
/*
This set is here to ensure that we attach comments only once
If a comment is attached to a node or token, we remove it from this set
*/
const commentsToAttach = new Set(comments);
// Attach comments as trailing comments if desirable
Object.keys(mostEnclosiveCstNodeByEndOffset).forEach(endOffset => {
// We look if some comments is directly following this node/token
if (commentsByExtendedStartOffset[endOffset] !== undefined) {
const nodeTrailingComments = commentsByExtendedStartOffset[
endOffset
].filter(comment => {
return (
shouldAttachTrailingComments(
comment,
mostEnclosiveCstNodeByEndOffset[endOffset],
mostEnclosiveCstNodeByStartOffset
) && commentsToAttach.has(comment)
);
});
if (nodeTrailingComments.length > 0) {
mostEnclosiveCstNodeByEndOffset[endOffset].trailingComments =
nodeTrailingComments;
}
nodeTrailingComments.forEach(comment => {
commentsToAttach.delete(comment);
});
}
});
// Attach rest of comments as leading comments
Object.keys(mostEnclosiveCstNodeByStartOffset).forEach(startOffset => {
// We look if some comments is directly preceding this node/token
if (commentsByExtendedEndOffset[startOffset] !== undefined) {
const nodeLeadingComments = commentsByExtendedEndOffset[
startOffset
].filter(comment => commentsToAttach.has(comment));
if (nodeLeadingComments.length > 0) {
mostEnclosiveCstNodeByStartOffset[startOffset].leadingComments =
nodeLeadingComments;
}
// prettier ignore support
for (let i = 0; i < nodeLeadingComments.length; i++) {
if (isPrettierIgnoreComment(nodeLeadingComments[i])) {
const node = mostEnclosiveCstNodeByStartOffset[startOffset];
const ignoreNode =
node.name === "blockStatements"
? node.children.blockStatement[0]
: node;
ignoreNode.ignore = true;
break;
}
}
}
});
}
/**
* Create pairs of formatter:off and formatter:on
* @param comments
* @returns pairs of formatter:off and formatter:on
*/
export function matchFormatterOffOnPairs(comments) {
const onOffComments = comments.filter(comment =>
isFormatterOffOnComment(comment)
);
let isPreviousCommentOff = false;
let isCurrentCommentOff = true;
const pairs = [];
let paired = {};
onOffComments.forEach(comment => {
isCurrentCommentOff = comment.image.slice(-3) === "off";
if (!isPreviousCommentOff) {
if (isCurrentCommentOff) {
paired.off = comment;
}
} else {
if (!isCurrentCommentOff) {
paired.on = comment;
pairs.push(paired);
paired = {};
}
}
isPreviousCommentOff = isCurrentCommentOff;
});
if (onOffComments.length > 0 && isCurrentCommentOff) {
paired.on = undefined;
pairs.push(paired);
}
return pairs;
}
/**
* Check if the node is between formatter:off and formatter:on and change his ignore state
* @param node
* @param commentPairs
*/
export function shouldNotFormat(node, commentPairs) {
const matchingPair = findLast(
commentPairs,
comment => comment.off.endOffset < node.location.startOffset
);
if (
matchingPair !== undefined &&
(matchingPair.on === undefined ||
matchingPair.on.startOffset > node.location.endOffset)
) {
node.ignore = true;
}
}