Update definition of helpers.isBlankLine to treat unterminated start/end comments as potentially blank lines (fixes #431).

This commit is contained in:
David Anson 2021-12-20 04:18:45 +00:00 committed by GitHub
parent 1b23976aa2
commit 6dea67825a
4 changed files with 67 additions and 11 deletions

View file

@ -77,12 +77,18 @@ module.exports.isObject = function isObject(obj) {
return (obj !== null) && (typeof obj === "object") && !Array.isArray(obj); return (obj !== null) && (typeof obj === "object") && !Array.isArray(obj);
}; };
// Returns true iff the input line is blank (no content) // Returns true iff the input line is blank (no content)
// Example: Contains nothing, whitespace, or comments // Example: Contains nothing, whitespace, or comment (unclosed start/end okay)
var blankLineRe = />|(?:<!--.*?-->)/g;
module.exports.isBlankLine = function isBlankLine(line) { module.exports.isBlankLine = function isBlankLine(line) {
// Call to String.replace follows best practices and is not a security check // Call to String.replace follows best practices and is not a security check
// False-positive for js/incomplete-multi-character-sanitization // False-positive for js/incomplete-multi-character-sanitization
return !line || !line.trim() || !line.replace(blankLineRe, "").trim(); return (!line ||
!line.trim() ||
!line
.replace(/<!--.*?-->/g, "")
.replace(/<!--.*$/g, "")
.replace(/^.*-->/g, "")
.replace(/>/g, "")
.trim());
}; };
/** /**
* Compare function for Array.prototype.sort for ascending order of numbers. * Compare function for Array.prototype.sort for ascending order of numbers.

View file

@ -63,12 +63,20 @@ module.exports.isObject = function isObject(obj) {
}; };
// Returns true iff the input line is blank (no content) // Returns true iff the input line is blank (no content)
// Example: Contains nothing, whitespace, or comments // Example: Contains nothing, whitespace, or comment (unclosed start/end okay)
const blankLineRe = />|(?:<!--.*?-->)/g;
module.exports.isBlankLine = function isBlankLine(line) { module.exports.isBlankLine = function isBlankLine(line) {
// Call to String.replace follows best practices and is not a security check // Call to String.replace follows best practices and is not a security check
// False-positive for js/incomplete-multi-character-sanitization // False-positive for js/incomplete-multi-character-sanitization
return !line || !line.trim() || !line.replace(blankLineRe, "").trim(); return (
!line ||
!line.trim() ||
!line
.replace(/<!--.*?-->/g, "")
.replace(/<!--.*$/g, "")
.replace(/^.*-->/g, "")
.replace(/>/g, "")
.trim()
);
}; };
/** /**

View file

@ -0,0 +1,38 @@
# Lists with Commented Items
Text
- item <!-- comment -->
- item <!-- comment -->
<!--
- commented subitem: description
- commented subitem: description
-->
- item <!-- comment -->
- item <!-- comment -->
Text
- item <!-- comment -->
- item <!-- comment -->
<!-- - commented subitem: description
- commented subitem: description -->
- item <!-- comment -->
- item <!-- comment -->
Text
- item <!-- comment -->
<!-- - commented subitem: description -->
- item <!-- comment -->
Text
- item <!-- comment -->
- item <!-- comment -->
<!-- - commented subitem: description -->
<!-- - commented subitem: description -->
- item <!-- comment -->
- item <!-- comment -->
Text

View file

@ -226,7 +226,7 @@ bar`
}); });
test("isBlankLine", (t) => { test("isBlankLine", (t) => {
t.plan(25); t.plan(29);
const blankLines = [ const blankLines = [
null, null,
"", "",
@ -244,7 +244,11 @@ test("isBlankLine", (t) => {
"> ", "> ",
"> > > \t", "> > > \t",
"> <!--text-->", "> <!--text-->",
">><!--text-->" ">><!--text-->",
"<!--",
" <!-- text",
"text --> ",
"-->"
]; ];
blankLines.forEach((line) => t.true(helpers.isBlankLine(line), line || "")); blankLines.forEach((line) => t.true(helpers.isBlankLine(line), line || ""));
const nonBlankLines = [ const nonBlankLines = [
@ -253,9 +257,9 @@ test("isBlankLine", (t) => {
".", ".",
"> .", "> .",
"<!--text--> text", "<!--text--> text",
"<!--->", "text <!--text-->",
"<!--", "text <!--",
"-->" "--> text"
]; ];
nonBlankLines.forEach((line) => t.true(!helpers.isBlankLine(line), line)); nonBlankLines.forEach((line) => t.true(!helpers.isBlankLine(line), line));
}); });