Repository navigation
Expand file tree
/
Copy pathcomments.js
More file actions
147 lines (125 loc) · 3.47 KB
/
Copy pathcomments.js
File metadata and controls
147 lines (125 loc) · 3.47 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
"use strict";
const {
addLeadingComment,
addDanglingComment,
addTrailingComment,
} = require("prettier").util;
const { concat, join, indent, hardline } = require("prettier").doc.builders;
const { isValidIdentifier, isExpression } = require("./util");
// https://github.com/prettier/prettier/blob/c052d9c0866025c6af1cf2e5ef086f53dc3effff/src/main/comments.js#L335
function printComment(commentPath, options) {
const comment = commentPath.getValue();
comment.printed = true;
return comment.raw;
}
// https://github.com/prettier/prettier/blob/c052d9c0866025c6af1cf2e5ef086f53dc3effff/src/main/comments.js#L440
function printDanglingComments(path, options, sameIndent, filter) {
const parts = [];
const node = path.getValue();
if (!node || !node.comments) {
return "";
}
path.each((commentPath) => {
const comment = commentPath.getValue();
if (
comment &&
!comment.leading &&
!comment.trailing &&
(!filter || filter(comment))
) {
parts.push(printComment(commentPath, options));
}
}, "comments");
if (parts.length === 0) {
return "";
}
if (sameIndent) {
return join(hardline, parts);
}
return indent(concat([hardline, join(hardline, parts)]));
}
/*
Comment functions are meant to inspect various edge cases using given comment nodes,
with information about where those comment nodes exist in the tree (ie enclosingNode,
previousNode, followingNode), and then either call the built in functions to handle
certain cases (ie addLeadingComment, addTrailingComment, addDanglingComment), or just
let prettier core handle them. To signal that the plugin is taking over, the comment
handler function should return true, otherwise returning false signals that prettier
core should handle the comment
args:
comment
text
options
ast
isLastComment
*/
const handleComments = {
ownLine(comment, text, options, ast, isLastComment) {
return (
handleCommentInEmptyBody(comment, text, options, ast, isLastComment) ||
false
);
},
endOfLine(comment, text, options, ast, isLastComment) {
return false;
},
remaining(comment, text, options, ast, isLastComment) {
return false;
},
};
function handleCommentInEmptyBody(comment, text, options, ast, isLastComment) {
if (
comment.enclosingNode &&
comment.enclosingNode.body &&
comment.enclosingNode.body.length === 0
) {
addDanglingComment(comment.enclosingNode, comment);
return true;
}
return false;
}
function canAttachComment(node) {
return true;
}
function getCommentChildNodes(node) {
const children = [];
Object.keys(node).forEach((key) => {
const value = node[key];
if (value == null) return;
if (key === "range") return;
if (key === "comments") return;
if (typeof value === "object") {
if (value.type) {
children.push(value);
} else if (Array.isArray(value)) {
value.forEach((node, key) => {
children.push(node);
});
}
}
});
return children;
}
function isDanglingComment(comment) {
return !comment.leading && !comment.trailing;
}
function isLeadingComment(comment) {
return comment.leading;
}
function isTrailingComment(comment) {
return comment.trailing;
}
function isBlockComment(comment) {
return /^\-\-\[=*\[/.test(comment.raw);
}
module.exports = {
handleComments,
printDanglingComments,
getCommentChildNodes,
canAttachComment,
printComment,
isDanglingComment,
isLeadingComment,
isTrailingComment,
isBlockComment,
};