Skip to content

Commit

Permalink
requireDescriptionCompleteSentence: To detect and ignore HTML content
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kepta authored and qfox committed Mar 22, 2016
1 parent f301aae commit ae6259b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/rules/validate-jsdoc/require-description-complete-sentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function requireDescriptionCompleteSentence(node, err) {
.replace(/(`)([^`]+)\1/, quotedSanitizer)
.replace(/(')([^']+)\1/, quotedSanitizer)
.replace(/(")([^"]+)\1/, quotedSanitizer)
// sanitize HTML tags which close
.replace(/<([^ >]+)[^>]*>[\s\S]*?.*?<\/\1>|<[^\/]+\/>/g, htmlSanitizer)
// sanitize self-closing HTML tags eg <br>
.replace(/<([^ >]+)[^>]*>/g, htmlSanitizer)
.replace(/\{([^}]+)\}/, function(_, m) {
return '{' + (new Array(m.length + 1)).join('*') + '}';
})
Expand Down Expand Up @@ -179,3 +183,19 @@ function quotedSanitizer(_, q, m) {
var endsWithDot = /\.\s*$/.test(m);
return q + (new Array(m.length + (endsWithDot ? 0 : 1))).join('*') + q + (endsWithDot ? '.' : '');
}

/**
* HTML part sanitizer.
* To prevent RE_NEW_LINE_START_WITH_UPPER_CASE
* return string will padded by 'x.'
*
* @private
* @param {string} _ - Full matched string
* @returns {string} - Sanitized string
*/
function htmlSanitizer(_) {
return _.split('').map(function(token, iterator) {
if (iterator === _.length - 1) { return 'x.'}
return token === '\n' ? '\n' : '*';
}).join('');
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ describe('lib/rules/validate-jsdoc/require-description-complete-sentence', funct
*/
function quux() {}
}
}, {
it: 'should not report sentences with html tags inside',
code: function () {
/**
* A foo is assigned the boolean value <p>true</p>.
*/
function fun(p) {}
}
}, {
it: 'should not report sentences that are html code',
code: function () {
/**
* The html code here
* <body>
* <p> Hello world!</p>
* </body>
* Should always be there.
* And the first letter of this line is in
* uppercase.
*/
function fun(p) {}
}
}
/* jshint ignore:end */
]);
Expand Down

0 comments on commit ae6259b

Please sign in to comment.