Skip to content

Commit c8eb609

Browse files
bary12gajus
authored andcommitted
feat: improve RequireDescriptionCompleteSentence (#59)
* fix: Also check @return, @arg and @argument in requireDescriptionCompleteSentence * Allow other punctuation marks * ignore {}
1 parent 9ffa44c commit c8eb609

File tree

2 files changed

+140
-8
lines changed

2 files changed

+140
-8
lines changed

src/rules/requireDescriptionCompleteSentence.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ const extractParagraphs = (text) => {
66
};
77

88
const extractSentences = (text) => {
9-
return text.split(/\.\s+|\.$/).filter((sentence) => {
9+
return text
10+
11+
// Remove all {} tags.
12+
.replace(/\{[\s\S]*?\}\s*/g, '')
13+
.split(/[.?!:](?:\s+|$)/)
14+
1015
// Ignore sentences with only whitespaces.
11-
return !/^\s*$/.test(sentence);
12-
}).map((sentence) => {
16+
.filter((sentence) => {
17+
return !/^\s*$/.test(sentence);
18+
})
19+
1320
// Re-add the dot.
14-
return sentence + '.';
15-
});
21+
.map((sentence) => {
22+
return sentence + '.';
23+
});
1624
};
1725

1826
const isNewLinePrecededByAPeriod = (text) => {
@@ -52,7 +60,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode) => {
5260
const fix = (fixer) => {
5361
let text = sourceCode.getText(jsdocNode);
5462

55-
if (!_.endsWith(paragraph, '.')) {
63+
if (!/[.:?!]$/.test(paragraph)) {
5664
const line = _.last(paragraph.split('\n'));
5765

5866
text = text.replace(line, line + '.');
@@ -75,7 +83,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode) => {
7583
report('Sentence should start with an uppercase character.', fix);
7684
}
7785

78-
if (!/\.$/.test(paragraph)) {
86+
if (!/[.!?]$/.test(paragraph)) {
7987
report('Sentence must end with a period.', fix);
8088

8189
return true;
@@ -102,7 +110,7 @@ export default iterateJsdoc(({
102110
}
103111

104112
const tags = _.filter(jsdoc.tags, (tag) => {
105-
return _.includes(['param', 'returns'], tag.tag);
113+
return _.includes(['param', 'arg', 'argument', 'returns', 'return'], tag.tag);
106114
});
107115

108116
_.some(tags, (tag) => {

test/rules/assertions/requireDescriptionCompleteSentence.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,32 @@ export default {
171171
}
172172
`
173173
},
174+
{
175+
code: `
176+
/**
177+
* {@see Foo.bar} buz
178+
*/
179+
function quux (foo) {
180+
181+
}
182+
`,
183+
errors: [
184+
{
185+
message: 'Sentence should start with an uppercase character.'
186+
},
187+
{
188+
message: 'Sentence must end with a period.'
189+
}
190+
],
191+
output: `
192+
/**
193+
* {@see Foo.bar} Buz.
194+
*/
195+
function quux (foo) {
196+
197+
}
198+
`
199+
},
174200
{
175201
code: `
176202
/**
@@ -258,6 +284,78 @@ export default {
258284
*/
259285
function longDescription (foo) {
260286
287+
}
288+
`
289+
},
290+
{
291+
code: `
292+
/**
293+
* @arg {number} foo - Foo
294+
*/
295+
function quux (foo) {
296+
297+
}
298+
`,
299+
errors: [
300+
{
301+
message: 'Sentence must end with a period.'
302+
}
303+
],
304+
output: `
305+
/**
306+
* @arg {number} foo - Foo.
307+
*/
308+
function quux (foo) {
309+
310+
}
311+
`
312+
},
313+
{
314+
code: `
315+
/**
316+
* @argument {number} foo - Foo
317+
*/
318+
function quux (foo) {
319+
320+
}
321+
`,
322+
errors: [
323+
{
324+
message: 'Sentence must end with a period.'
325+
}
326+
],
327+
output: `
328+
/**
329+
* @argument {number} foo - Foo.
330+
*/
331+
function quux (foo) {
332+
333+
}
334+
`
335+
},
336+
{
337+
code: `
338+
/**
339+
* @return {number} foo
340+
*/
341+
function quux (foo) {
342+
343+
}
344+
`,
345+
errors: [
346+
{
347+
message: 'Sentence should start with an uppercase character.'
348+
},
349+
{
350+
message: 'Sentence must end with a period.'
351+
}
352+
],
353+
output: `
354+
/**
355+
* @return {number} Foo.
356+
*/
357+
function quux (foo) {
358+
261359
}
262360
`
263361
}
@@ -335,6 +433,32 @@ export default {
335433
*/
336434
function quux () {
337435
436+
}
437+
`
438+
},
439+
{
440+
code: `
441+
/**
442+
* Foo. {@see Math.sin}.
443+
*/
444+
function quux () {
445+
446+
}
447+
`
448+
},
449+
{
450+
code: `
451+
/**
452+
* Foo?
453+
*
454+
* Bar!
455+
*
456+
* Baz:
457+
* 1. Foo.
458+
* 2. Bar.
459+
*/
460+
function quux () {
461+
338462
}
339463
`
340464
}

0 commit comments

Comments
 (0)