Skip to content

Commit

Permalink
Correctly escape strings inserted into RegExps
Browse files Browse the repository at this point in the history
Fixes: gajus#62
  • Loading branch information
cscott committed Feb 27, 2018
1 parent 647dc04 commit 8f10f2b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rules/requireDescriptionCompleteSentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
if (!/[.:?!]$/.test(paragraph)) {
const line = _.last(paragraph.split('\n'));

text = text.replace(new RegExp(line + '$', 'm'), line + '.');
text = text.replace(new RegExp(_.escapeRegExp(line) + '$', 'm'), line + '.');
}

for (const sentence of sentences.filter((sentence_) => {
Expand All @@ -72,7 +72,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
const beginning = sentence.split('\n')[0];

if (tag) {
const reg = new RegExp('(@' + tag + '.*)' + _.escapeRegExp(beginning));
const reg = new RegExp('(@' + _.escapeRegExp(tag) + '.*)' + _.escapeRegExp(beginning));

text = text.replace(reg, ($0, $1) => {
return $1 + capitalize(beginning);
Expand Down
23 changes: 23 additions & 0 deletions test/rules/assertions/requireDescriptionCompleteSentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ export default {
}
`
},
{
code: `
/**
* Foo)
*/
function quux () {
}
`,
errors: [
{
message: 'Sentence must end with a period.'
}
],
output: `
/**
* Foo).
*/
function quux () {
}
`
},
{
code: `
/**
Expand Down

0 comments on commit 8f10f2b

Please sign in to comment.