Skip to content

Commit 456206e

Browse files
committed
fix(require-description-complete-sentence): let fixer work with question and exclamation marks at sentence end (fixes part of #16)
1 parent 7722f55 commit 456206e

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,14 @@ function quux () {
35423542
}
35433543
// Message: Sentence should start with an uppercase character.
35443544

3545+
/**
3546+
* foo?
3547+
*/
3548+
function quux () {
3549+
3550+
}
3551+
// Message: Sentence should start with an uppercase character.
3552+
35453553
/**
35463554
* @description foo.
35473555
*/

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"escape-regex-string": "^1.0.6",
1111
"flat-map-polyfill": "^0.3.8",
1212
"jsdoctypeparser": "5.0.1",
13-
"lodash": "^4.17.11"
13+
"lodash": "^4.17.11",
14+
"regextras": "^0.6.1"
1415
},
1516
"description": "JSDoc linting rules for ESLint.",
1617
"devDependencies": {

src/rules/requireDescriptionCompleteSentence.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import _ from 'lodash';
2+
import {RegExtras} from 'regextras/dist/main-umd';
23
import iterateJsdoc from '../iterateJsdoc';
34

45
const extractParagraphs = (text) => {
56
return text.split(/\n\n/);
67
};
78

89
const extractSentences = (text) => {
9-
return text
10+
const txt = text
1011

1112
// Remove all {} tags.
12-
.replace(/\{[\s\S]*?\}\s*/g, '')
13-
.split(/[.?!](?:\s+|$)/)
13+
.replace(/\{[\s\S]*?\}\s*/g, '');
14+
15+
const sentenceEndGrouping = /([.?!])(?:\s+|$)/;
16+
const puncts = RegExtras(sentenceEndGrouping).map(txt, (punct) => {
17+
return punct;
18+
});
1419

15-
// Ignore sentences with only whitespaces.
16-
.filter((sentence) => {
17-
return !/^\s*$/.test(sentence);
18-
})
20+
return txt
21+
22+
.split(/[.?!](?:\s+|$)/)
1923

2024
// Re-add the dot.
21-
.map((sentence) => {
22-
return `${sentence}.`;
25+
.map((sentence, idx) => {
26+
return /^\s*$/.test(sentence) ? sentence : `${sentence}${puncts[idx] || ''}`;
2327
});
2428
};
2529

@@ -67,7 +71,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
6771
}
6872

6973
for (const sentence of sentences.filter((sentence_) => {
70-
return !isCapitalized(sentence_);
74+
return !(/^\s*$/).test(sentence_) && !isCapitalized(sentence_);
7175
})) {
7276
const beginning = sentence.split('\n')[0];
7377

@@ -86,7 +90,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
8690
};
8791

8892
if (sentences.some((sentence) => {
89-
return !isCapitalized(sentence);
93+
return !(/^\s*$/).test(sentence) && !isCapitalized(sentence);
9094
})) {
9195
report('Sentence should start with an uppercase character.', fix);
9296
}

test/rules/assertions/requireDescriptionCompleteSentence.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ export default {
2323
}
2424
`
2525
},
26+
{
27+
code: `
28+
/**
29+
* foo?
30+
*/
31+
function quux () {
32+
33+
}
34+
`,
35+
errors: [
36+
{
37+
message: 'Sentence should start with an uppercase character.'
38+
}
39+
],
40+
output: `
41+
/**
42+
* Foo?
43+
*/
44+
function quux () {
45+
46+
}
47+
`
48+
},
2649
{
2750
code: `
2851
/**

0 commit comments

Comments
 (0)