Skip to content

Commit fab6471

Browse files
cscottgajus
authored andcommitted
Correctly escape strings inserted into RegExps (fixes #62 #64)
Fixes: #62
1 parent 647dc04 commit fab6471

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/rules/requireDescriptionCompleteSentence.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
6363
if (!/[.:?!]$/.test(paragraph)) {
6464
const line = _.last(paragraph.split('\n'));
6565

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

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

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

7777
text = text.replace(reg, ($0, $1) => {
7878
return $1 + capitalize(beginning);

test/rules/assertions/requireDescriptionCompleteSentence.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ export default {
2525
}
2626
`
2727
},
28+
{
29+
code: `
30+
/**
31+
* Foo)
32+
*/
33+
function quux () {
34+
35+
}
36+
`,
37+
errors: [
38+
{
39+
message: 'Sentence must end with a period.'
40+
}
41+
],
42+
output: `
43+
/**
44+
* Foo).
45+
*/
46+
function quux () {
47+
48+
}
49+
`
50+
},
2851
{
2952
code: `
3053
/**

0 commit comments

Comments
 (0)