Skip to content

Commit 72f394d

Browse files
not-an-aardvarknzakas
authored andcommitted
Update: Fix false negative of no-multiple-empty-lines (fixes #7312) (#7313)
This commit is intended to change as little of the rule's logic as possible in order to fix the issue. I think this rule could use refactoring, which I plan to do afterwards. I made this commit based on the old logic to allow for an easier revert if the refactoring goes wrong somehow. Bug explanation: The rule stores a list of strings containing the lines of a file, with whitespace trimmed from the start and end of each line. It detects an empty line by checking whether the trimmed line in its list is an empty string. The rule also has to account for empty lines in template literals (see #2605). In order to do this, for each line in the file that overlaps a template literal, the rule tries to overwrite the corresponding value in its list of lines with something other than an empty string, to prevent it from detected as an empty line. Due to a logical error, the rule was overwriting the strings at the *beginning* of its list of lines, rather than the lines that actually contained template literals.
1 parent 756bc5a commit 72f394d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/rules/no-multiple-empty-lines.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ module.exports = {
113113
});
114114

115115
// add the notEmpty lines in there with a placeholder
116-
notEmpty.forEach(function(x, i) {
117-
trimmedLines[i] = x;
116+
notEmpty.forEach(function(nonEmptyLineNumber) {
117+
118+
// FIXME: (not-an-aardvark) Use a better strategy after refactoring this rule
119+
trimmedLines[nonEmptyLineNumber] = "non-empty placeholder string";
118120
});
119121

120122
if (typeof maxEOF === "undefined") {

tests/lib/rules/no-multiple-empty-lines.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ ruleTester.run("no-multiple-empty-lines", rule, {
286286
output: "// invalid 21\n// unix line endings\nvar a = 5;\nvar b = 3;\n\n",
287287
errors: [ getExpectedErrorEOF(1) ],
288288
options: [ { max: 1 } ]
289+
},
290+
{
291+
code:
292+
"'foo';\n" +
293+
"\n" +
294+
"\n" +
295+
"`bar`;\n" +
296+
"`baz`;",
297+
output:
298+
"'foo';\n" +
299+
"\n" +
300+
"`bar`;\n" +
301+
"`baz`;",
302+
errors: [ getExpectedError(1) ],
303+
options: [ { max: 1 } ],
304+
parserOptions: { ecmaVersion: 6 }
289305
}
290306
]
291307
});

0 commit comments

Comments
 (0)