Skip to content

Commit ae6259b

Browse files
keptaqfox
authored andcommitted
requireDescriptionCompleteSentence: To detect and ignore HTML content
This rule has been flagging comments with html tags. In this patch the html is sanitized with *** so that it doesn't interfere with the regular regex tests. The htmlSanitizer function is padding all strings with 'x.' so that RE_NEW_LINE_START_WITH_UPPER_CASE doesnt fail. This will need to be improved after discussing with owner if he/she thinks so. Fixes jscs-dev/node-jscs#2056 Closes gh-186
1 parent f301aae commit ae6259b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/rules/validate-jsdoc/require-description-complete-sentence.js

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function requireDescriptionCompleteSentence(node, err) {
5656
.replace(/(`)([^`]+)\1/, quotedSanitizer)
5757
.replace(/(')([^']+)\1/, quotedSanitizer)
5858
.replace(/(")([^"]+)\1/, quotedSanitizer)
59+
// sanitize HTML tags which close
60+
.replace(/<([^ >]+)[^>]*>[\s\S]*?.*?<\/\1>|<[^\/]+\/>/g, htmlSanitizer)
61+
// sanitize self-closing HTML tags eg <br>
62+
.replace(/<([^ >]+)[^>]*>/g, htmlSanitizer)
5963
.replace(/\{([^}]+)\}/, function(_, m) {
6064
return '{' + (new Array(m.length + 1)).join('*') + '}';
6165
})
@@ -179,3 +183,19 @@ function quotedSanitizer(_, q, m) {
179183
var endsWithDot = /\.\s*$/.test(m);
180184
return q + (new Array(m.length + (endsWithDot ? 0 : 1))).join('*') + q + (endsWithDot ? '.' : '');
181185
}
186+
187+
/**
188+
* HTML part sanitizer.
189+
* To prevent RE_NEW_LINE_START_WITH_UPPER_CASE
190+
* return string will padded by 'x.'
191+
*
192+
* @private
193+
* @param {string} _ - Full matched string
194+
* @returns {string} - Sanitized string
195+
*/
196+
function htmlSanitizer(_) {
197+
return _.split('').map(function(token, iterator) {
198+
if (iterator === _.length - 1) { return 'x.'}
199+
return token === '\n' ? '\n' : '*';
200+
}).join('');
201+
}

test/lib/rules/validate-jsdoc/require-description-complete-sentence.js

+22
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,28 @@ describe('lib/rules/validate-jsdoc/require-description-complete-sentence', funct
232232
*/
233233
function quux() {}
234234
}
235+
}, {
236+
it: 'should not report sentences with html tags inside',
237+
code: function () {
238+
/**
239+
* A foo is assigned the boolean value <p>true</p>.
240+
*/
241+
function fun(p) {}
242+
}
243+
}, {
244+
it: 'should not report sentences that are html code',
245+
code: function () {
246+
/**
247+
* The html code here
248+
* <body>
249+
* <p> Hello world!</p>
250+
* </body>
251+
* Should always be there.
252+
* And the first letter of this line is in
253+
* uppercase.
254+
*/
255+
function fun(p) {}
256+
}
235257
}
236258
/* jshint ignore:end */
237259
]);

0 commit comments

Comments
 (0)