forked from gajus/eslint-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
requireDescriptionCompleteSentence.js
196 lines (155 loc) · 5.08 KB
/
requireDescriptionCompleteSentence.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
import _ from 'lodash';
import {RegExtras} from 'regextras/dist/main-umd';
import iterateJsdoc from '../iterateJsdoc';
const extractParagraphs = (text) => {
return text.split(/\n\n/);
};
const extractSentences = (text) => {
const txt = text
// Remove all {} tags.
.replace(/\{[\s\S]*?\}\s*/g, '');
const sentenceEndGrouping = /([.?!])(?:\s+|$)/;
const puncts = RegExtras(sentenceEndGrouping).map(txt, (punct) => {
return punct;
});
return txt
.split(/[.?!](?:\s+|$)/)
// Re-add the dot.
.map((sentence, idx) => {
return /^\s*$/.test(sentence) ? sentence : `${sentence}${puncts[idx] || ''}`;
});
};
const isNewLinePrecededByAPeriod = (text) => {
let lastLineEndsSentence;
const lines = text.split('\n');
return !lines.some((line) => {
if (typeof lastLineEndsSentence === 'boolean' && !lastLineEndsSentence && /^[A-Z]/.test(line)) {
return true;
}
lastLineEndsSentence = /[.:?!]$/.test(line);
return false;
});
};
const isCapitalized = (str) => {
return str[0] === str[0].toUpperCase();
};
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
const validateDescription = (description, reportOrig, jsdocNode, sourceCode, tag) => {
if (!description) {
return false;
}
const paragraphs = extractParagraphs(description);
return paragraphs.some((paragraph, parIdx) => {
const sentences = extractSentences(paragraph);
const fix = (fixer) => {
let text = sourceCode.getText(jsdocNode);
if (!/[.:?!]$/.test(paragraph)) {
const line = _.last(paragraph.split('\n'));
text = text.replace(new RegExp(`${_.escapeRegExp(line)}$`, 'm'), `${line}.`);
}
for (const sentence of sentences.filter((sentence_) => {
return !(/^\s*$/).test(sentence_) && !isCapitalized(sentence_);
})) {
const beginning = sentence.split('\n')[0];
if (tag.tag) {
const reg = new RegExp(`(@${_.escapeRegExp(tag.tag)}.*)${_.escapeRegExp(beginning)}`);
text = text.replace(reg, ($0, $1) => {
return $1 + capitalize(beginning);
});
} else {
text = text.replace(beginning, capitalize(beginning));
}
}
return fixer.replaceText(jsdocNode, text);
};
const report = (msg, fixer, tagObj) => {
tagObj.line += parIdx * 2;
// Avoid errors if old column doesn't exist here
tagObj.column = 0;
reportOrig(msg, fixer, tagObj);
};
if (sentences.some((sentence) => {
return !(/^\s*$/).test(sentence) && !isCapitalized(sentence);
})) {
report('Sentence should start with an uppercase character.', fix, tag);
}
if (!/[.!?]$/.test(paragraph)) {
report('Sentence must end with a period.', fix, tag);
return true;
}
if (!isNewLinePrecededByAPeriod(paragraph)) {
report('A line of text is started with an uppercase character, but preceding line does not end the sentence.', null, tag);
return true;
}
return false;
});
};
export default iterateJsdoc(({
sourceCode,
jsdoc,
report,
jsdocNode,
context,
utils
}) => {
if (!jsdoc.tags ||
validateDescription(jsdoc.description, report, jsdocNode, sourceCode, {
line: jsdoc.line + 1
})
) {
return;
}
utils.forEachPreferredTag('description', (matchingJsdocTag) => {
const description = `${matchingJsdocTag.name} ${matchingJsdocTag.description}`.trim();
validateDescription(description, report, jsdocNode, sourceCode, matchingJsdocTag);
}, true);
const options = context.options[0] || {};
const hasOptionTag = (tagName) => {
return Boolean(options.tags && options.tags.includes(tagName));
};
const {tagsWithNames} = utils.getTagsByType(jsdoc.tags);
const tagsWithoutNames = utils.filterTags(({tag: tagName}) => {
return [
// 'copyright' and 'see' might be good addition, but as the former may be
// sensitive text, and the latter may have just a link, they are not
// included by default
'summary', 'file', 'fileoverview', 'overview', 'classdesc', 'todo',
'deprecated', 'throws', 'exception', 'yields', 'yield'
].includes(tagName) ||
hasOptionTag(tagName) && !tagsWithNames.some(({tag}) => {
// If user accidentally adds tags with names (or like `returns`
// get parsed as having names), do not add to this list
return tag === tagName;
});
});
tagsWithNames.some((tag) => {
const description = _.trimStart(tag.description, '- ');
return validateDescription(description, report, jsdocNode, sourceCode, tag);
});
tagsWithoutNames.some((tag) => {
const description = `${tag.name} ${tag.description}`.trim();
return validateDescription(description, report, jsdocNode, sourceCode, tag);
});
}, {
iterateAllJsdocs: true,
meta: {
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
tags: {
items: {
type: 'string'
},
type: 'array'
}
},
type: 'object'
}
],
type: 'suggestion'
}
});