Skip to content

Commit d579850

Browse files
bary12gajus
authored andcommitted
fix: cover the edge cases in requireDescriptionCompleteSentence (#61)
* fix: Edge cases and tests for them * revert unnecessary change
1 parent c8eb609 commit d579850

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/rules/requireDescriptionCompleteSentence.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const isNewLinePrecededByAPeriod = (text) => {
3333
return true;
3434
}
3535

36-
lastLineEndsSentence = /\.$/.test(line);
36+
lastLineEndsSentence = /[.:?!]$/.test(line);
3737

3838
return false;
3939
});
@@ -47,7 +47,7 @@ const capitalize = (str) => {
4747
return str.charAt(0).toUpperCase() + str.slice(1);
4848
};
4949

50-
const validateDescription = (description, report, jsdocNode, sourceCode) => {
50+
const validateDescription = (description, report, jsdocNode, sourceCode, tag) => {
5151
if (!description) {
5252
return false;
5353
}
@@ -63,15 +63,23 @@ const validateDescription = (description, report, jsdocNode, sourceCode) => {
6363
if (!/[.:?!]$/.test(paragraph)) {
6464
const line = _.last(paragraph.split('\n'));
6565

66-
text = text.replace(line, line + '.');
66+
text = text.replace(new RegExp(line + '$', 'm'), line + '.');
6767
}
6868

6969
for (const sentence of sentences.filter((sentence_) => {
7070
return !isCapitalized(sentence_);
7171
})) {
7272
const beginning = sentence.split('\n')[0];
7373

74-
text = text.replace(beginning, capitalize(beginning));
74+
if (tag) {
75+
const reg = new RegExp('(@' + tag + '.*)' + _.escapeRegExp(beginning));
76+
77+
text = text.replace(reg, ($0, $1) => {
78+
return $1 + capitalize(beginning);
79+
});
80+
} else {
81+
text = text.replace(beginning, capitalize(beginning));
82+
}
7583
}
7684

7785
return fixer.replaceText(jsdocNode, text);
@@ -116,6 +124,6 @@ export default iterateJsdoc(({
116124
_.some(tags, (tag) => {
117125
const description = _.trimStart(tag.description, '- ');
118126

119-
return validateDescription(description, report, jsdocNode, sourceCode);
127+
return validateDescription(description, report, jsdocNode, sourceCode, tag.tag);
120128
});
121129
});

test/rules/assertions/requireDescriptionCompleteSentence.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,36 @@ export default {
356356
*/
357357
function quux (foo) {
358358
359+
}
360+
`
361+
},
362+
{
363+
code: `
364+
/**
365+
* Returns bar.
366+
*
367+
* @return {number} bar
368+
*/
369+
function quux (foo) {
370+
371+
}
372+
`,
373+
errors: [
374+
{
375+
message: 'Sentence should start with an uppercase character.'
376+
},
377+
{
378+
message: 'Sentence must end with a period.'
379+
}
380+
],
381+
output: `
382+
/**
383+
* Returns bar.
384+
*
385+
* @return {number} Bar.
386+
*/
387+
function quux (foo) {
388+
359389
}
360390
`
361391
}
@@ -446,6 +476,16 @@ export default {
446476
}
447477
`
448478
},
479+
{
480+
code: `
481+
/**
482+
* Foo {@see Math.sin} bar.
483+
*/
484+
function quux () {
485+
486+
}
487+
`
488+
},
449489
{
450490
code: `
451491
/**
@@ -459,6 +499,17 @@ export default {
459499
*/
460500
function quux () {
461501
502+
}
503+
`
504+
},
505+
{
506+
code: `
507+
/**
508+
* Hello:
509+
* World.
510+
*/
511+
function quux () {
512+
462513
}
463514
`
464515
}

0 commit comments

Comments
 (0)