-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment-autocorrect.js
45 lines (41 loc) · 1.29 KB
/
comment-autocorrect.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
'use strict';
const autocorrect = require('autocorrect-node');
const MESSAGE_ID_DEFAULT = 'default';
/** @type {RuleCreate} */
const create = (context) => {
const { sourceCode } = context;
return {
Program() {
const comments = sourceCode.getAllComments();
comments.forEach((comment) => {
const correctCommentContent = autocorrect.format(comment.value);
if (correctCommentContent !== comment.value) {
context.report({
node: comment,
messageId: MESSAGE_ID_DEFAULT,
fix(fixer) {
return fixer.replaceText(
comment,
comment.type === 'Line'
? `//${correctCommentContent}`
: `/*${correctCommentContent}*/`,
);
},
});
}
});
},
};
};
/** @type {RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
schema: [],
fixable: 'whitespace',
messages: {
[MESSAGE_ID_DEFAULT]: '中文注释排版不规范',
},
},
create,
};