Skip to content

Commit 9ffa44c

Browse files
bary12gajus
authored andcommitted
fix: issue with complete sentence fixer (#57)
* Fix issue with complete sentence fixer * fix ESlint * Only periods with at least one whitespace are considered new sentences * Fix a problem with the regex
1 parent 0ef73eb commit 9ffa44c

File tree

2 files changed

+83
-20
lines changed

2 files changed

+83
-20
lines changed

src/rules/requireDescriptionCompleteSentence.js

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

88
const extractSentences = (text) => {
9-
return text.split(/\.\s*/).filter((sentence) => {
9+
return text.split(/\.\s+|\.$/).filter((sentence) => {
1010
// Ignore sentences with only whitespaces.
1111
return !/^\s*$/.test(sentence);
1212
}).map((sentence) => {
@@ -49,31 +49,34 @@ const validateDescription = (description, report, jsdocNode, sourceCode) => {
4949
return _.some(paragraphs, (paragraph) => {
5050
const sentences = extractSentences(paragraph);
5151

52-
if (_.some(sentences, (sentence) => {
53-
return !isCapitalized(sentence);
54-
})) {
55-
report('Sentence should start with an uppercase character.', (fixer) => {
56-
let text = sourceCode.getText(jsdocNode);
52+
const fix = (fixer) => {
53+
let text = sourceCode.getText(jsdocNode);
54+
55+
if (!_.endsWith(paragraph, '.')) {
56+
const line = _.last(paragraph.split('\n'));
57+
58+
text = text.replace(line, line + '.');
59+
}
5760

58-
for (const sentence of sentences.filter((sentence_) => {
59-
return !isCapitalized(sentence_);
60-
})) {
61-
const beginning = sentence.split(/\n/)[0];
61+
for (const sentence of sentences.filter((sentence_) => {
62+
return !isCapitalized(sentence_);
63+
})) {
64+
const beginning = sentence.split('\n')[0];
6265

63-
text = text.replace(beginning, capitalize(beginning));
64-
}
66+
text = text.replace(beginning, capitalize(beginning));
67+
}
6568

66-
return fixer.replaceText(jsdocNode, text);
67-
});
69+
return fixer.replaceText(jsdocNode, text);
70+
};
71+
72+
if (_.some(sentences, (sentence) => {
73+
return !isCapitalized(sentence);
74+
})) {
75+
report('Sentence should start with an uppercase character.', fix);
6876
}
6977

7078
if (!/\.$/.test(paragraph)) {
71-
report('Sentence must end with a period.', (fixer) => {
72-
const line = _.last(paragraph.split('\n'));
73-
const replacement = sourceCode.getText(jsdocNode).replace(line, line + '.');
74-
75-
return fixer.replaceText(jsdocNode, replacement);
76-
});
79+
report('Sentence must end with a period.', fix);
7780

7881
return true;
7982
}

test/rules/assertions/requireDescriptionCompleteSentence.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,66 @@ export default {
141141
}
142142
`
143143
},
144+
{
145+
code: `
146+
/**
147+
* Foo.
148+
*
149+
* @param foo bar
150+
*/
151+
function quux (foo) {
152+
153+
}
154+
`,
155+
errors: [
156+
{
157+
message: 'Sentence should start with an uppercase character.'
158+
},
159+
{
160+
message: 'Sentence must end with a period.'
161+
}
162+
],
163+
output: `
164+
/**
165+
* Foo.
166+
*
167+
* @param foo Bar.
168+
*/
169+
function quux (foo) {
170+
171+
}
172+
`
173+
},
174+
{
175+
code: `
176+
/**
177+
* Foo.
178+
*
179+
* @returns {number} foo
180+
*/
181+
function quux (foo) {
182+
183+
}
184+
`,
185+
errors: [
186+
{
187+
message: 'Sentence should start with an uppercase character.'
188+
},
189+
{
190+
message: 'Sentence must end with a period.'
191+
}
192+
],
193+
output: `
194+
/**
195+
* Foo.
196+
*
197+
* @returns {number} Foo.
198+
*/
199+
function quux (foo) {
200+
201+
}
202+
`
203+
},
144204
{
145205
code: `
146206
/**

0 commit comments

Comments
 (0)