Skip to content

Commit

Permalink
fix: issue with complete sentence fixer (#57)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bary12 authored and gajus committed Jan 6, 2018
1 parent 0ef73eb commit 9ffa44c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/rules/requireDescriptionCompleteSentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const extractParagraphs = (text) => {
};

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

if (_.some(sentences, (sentence) => {
return !isCapitalized(sentence);
})) {
report('Sentence should start with an uppercase character.', (fixer) => {
let text = sourceCode.getText(jsdocNode);
const fix = (fixer) => {
let text = sourceCode.getText(jsdocNode);

if (!_.endsWith(paragraph, '.')) {
const line = _.last(paragraph.split('\n'));

text = text.replace(line, line + '.');
}

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

text = text.replace(beginning, capitalize(beginning));
}
text = text.replace(beginning, capitalize(beginning));
}

return fixer.replaceText(jsdocNode, text);
});
return fixer.replaceText(jsdocNode, text);
};

if (_.some(sentences, (sentence) => {
return !isCapitalized(sentence);
})) {
report('Sentence should start with an uppercase character.', fix);
}

if (!/\.$/.test(paragraph)) {
report('Sentence must end with a period.', (fixer) => {
const line = _.last(paragraph.split('\n'));
const replacement = sourceCode.getText(jsdocNode).replace(line, line + '.');

return fixer.replaceText(jsdocNode, replacement);
});
report('Sentence must end with a period.', fix);

return true;
}
Expand Down
60 changes: 60 additions & 0 deletions test/rules/assertions/requireDescriptionCompleteSentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,66 @@ export default {
}
`
},
{
code: `
/**
* Foo.
*
* @param foo bar
*/
function quux (foo) {
}
`,
errors: [
{
message: 'Sentence should start with an uppercase character.'
},
{
message: 'Sentence must end with a period.'
}
],
output: `
/**
* Foo.
*
* @param foo Bar.
*/
function quux (foo) {
}
`
},
{
code: `
/**
* Foo.
*
* @returns {number} foo
*/
function quux (foo) {
}
`,
errors: [
{
message: 'Sentence should start with an uppercase character.'
},
{
message: 'Sentence must end with a period.'
}
],
output: `
/**
* Foo.
*
* @returns {number} Foo.
*/
function quux (foo) {
}
`
},
{
code: `
/**
Expand Down

0 comments on commit 9ffa44c

Please sign in to comment.