-
Notifications
You must be signed in to change notification settings - Fork 1
/
prefer-jsdoc.js
196 lines (179 loc) · 6.09 KB
/
prefer-jsdoc.js
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
'use strict';
const MESSAGE_ID_DEFAULT = 'default';
const getIndentStr = (sourceCode, line) => {
const lineText = sourceCode.lines[line - 1];
return lineText.slice(0, lineText.length - lineText.trimStart().length);
};
function isJsdoc(comment) {
return comment.type === 'Block' && comment.value.startsWith('*');
}
/**
* @param {RuleContext} ctx
* @param {TSPropertySignature[]} propertyNodeList
*/
function checkNodes(ctx, propertyNodeList) {
for (const propertyNode of propertyNodeList) {
checkCommentBeforeNode(ctx, propertyNode);
if (['TSPropertySignature', 'PropertyDefinition'].includes(propertyNode.type)) {
checkCommentAfterNode(ctx, propertyNode);
}
}
}
/** @type {RuleCreate} */
const create = (context) => {
return {
/**
* @param {TSInterfaceDeclaration} node
*/
TSInterfaceDeclaration(node) {
return checkNodes(context, node.body.body);
},
/**
* @param {TSTypeAliasDeclaration} node
*/
TSTypeAliasDeclaration(node) {
return (
Array.isArray(node.typeAnnotation.members) &&
checkNodes(context, node.typeAnnotation.members)
);
},
ClassDeclaration(node) {
return checkNodes(context, node.body.body);
},
FunctionDeclaration(node) {
return checkCommentBeforeNode(context, node);
},
VariableDeclaration(node) {
const declarator = node.declarations[0];
// const f = () => {}
// const f = function(){}
if (
node.declarations.length === 1 &&
['ArrowFunctionExpression', 'FunctionExpression'].includes(declarator.init?.type)
) {
return checkCommentBeforeNode(context, node);
}
},
};
};
/** @type {RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
schema: [],
fixable: 'whitespace',
messages: {
[MESSAGE_ID_DEFAULT]: '应该使用 jsdoc 注释而不是单行注释',
},
},
create,
};
/**
* @param {RuleContext} ctx
*/
function checkCommentBeforeNode(ctx, propertyNode) {
const { sourceCode } = ctx;
const commentsBefore = sourceCode.getCommentsBefore(propertyNode);
if (commentsBefore.length === 0) return;
// 注释和节点之间有空行就不处理
const textBetween = sourceCode.text.slice(
commentsBefore.at(-1).range[1],
propertyNode.range[0],
);
let eolCount = 0;
for (const char of textBetween) {
if (char === '\n') {
eolCount++;
if (eolCount === 2) return;
}
}
const lineCommentsBefore = [];
for (const comment of [...commentsBefore].reverse()) {
const commentContent = comment.value.trimStart();
if (
isJsdoc(comment) ||
commentContent.startsWith('@ts-') ||
commentContent.startsWith('eslint-') ||
commentContent.startsWith('prettier-')
) {
break;
} else {
lineCommentsBefore.push(comment);
}
}
lineCommentsBefore.reverse();
if (lineCommentsBefore.length > 0) {
const firstLineCommentBefore = lineCommentsBefore[0];
const lastLineCommentBefore = lineCommentsBefore.at(-1);
ctx.report({
loc: {
start: firstLineCommentBefore.loc.start,
end: lastLineCommentBefore.loc.end,
},
messageId: MESSAGE_ID_DEFAULT,
fix(fixer) {
let jsdoc;
if (
lineCommentsBefore.length === 1 &&
!['VariableDeclaration', 'FunctionDeclaration', 'MethodDefinition'].includes(
propertyNode.type,
)
) {
jsdoc = `/**${lineCommentsBefore[0].value.trimEnd()} */`;
} else {
const commentValueList = lineCommentsBefore.map(
(comment) => ` *${comment.value.trimEnd()}`,
);
const indentStr = getIndentStr(
sourceCode,
firstLineCommentBefore.loc.start.line,
);
jsdoc =
// eslint-disable-next-line prefer-template
'/**\n' +
[...commentValueList, ' */']
.map((text) => `${indentStr}${text}`)
.join('\n');
}
return fixer.replaceTextRange(
[firstLineCommentBefore.range[0], lastLineCommentBefore.range[1]],
jsdoc,
);
},
});
}
}
/**
* @param {RuleContext} ctx
*/
function checkCommentAfterNode(ctx, propertyNode) {
const { sourceCode } = ctx;
const commentsAfter = sourceCode.getCommentsAfter(propertyNode);
if (commentsAfter.length === 0) return;
// 注释和节点不在同一行不处理
const textBetween = sourceCode.text.slice(propertyNode.range[1], commentsAfter[0].range[0]);
if (textBetween.includes('\n')) {
return;
}
if (
commentsAfter.length > 0 &&
!isJsdoc(commentsAfter[0]) &&
commentsAfter[0].loc.start.line === propertyNode.loc.end.line
) {
const lineCommentAfter = commentsAfter[0];
ctx.report({
node: lineCommentAfter,
messageId: MESSAGE_ID_DEFAULT,
fix(fixer) {
const indentStr = getIndentStr(sourceCode, propertyNode.loc.start.line);
const jsdoc = `/**${lineCommentAfter.value.trimEnd()} */`;
const propertyText = sourceCode.getText(propertyNode);
const newPropertyText = `${jsdoc}\n${indentStr}${propertyText}`;
return [
fixer.removeRange([propertyNode.range[1], lineCommentAfter.range[1]]),
fixer.replaceText(propertyNode, newPropertyText),
];
},
});
}
}