Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-multiple-empty-lines crash on space after last \n (fixes #8401) #8402

Merged
merged 2 commits into from Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/rules/no-multiple-empty-lines.js
Expand Up @@ -111,10 +111,19 @@ module.exports = {
message,
data: { max: maxAllowed, pluralizedLines: maxAllowed === 1 ? "line" : "lines" },
fix(fixer) {
return fixer.removeRange([
sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 }),
sourceCode.getIndexFromLoc({ line: lineNumber - maxAllowed, column: 0 })
]);
const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 });

/*
* The end of the removal range is usually the start index of the next line.
* However, at the end of the file there is no next line, so the end of the
* range is just the length of the text.
*/
const lineNumberAfterRemovedLines = lineNumber - maxAllowed;
const rangeEnd = lineNumberAfterRemovedLines <= allLines.length
? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 })
: sourceCode.text.length;

return fixer.removeRange([rangeStart, rangeEnd]);
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-multiple-empty-lines.js
Expand Up @@ -316,6 +316,14 @@ ruleTester.run("no-multiple-empty-lines", rule, {
code: `a\n\n\n\n${"a".repeat(1e5)}`,
output: `a\n\n\n${"a".repeat(1e5)}`,
errors: [getExpectedError(2)]
},
{

// https://github.com/eslint/eslint/issues/8401
code: "foo\n ",
output: "foo\n",
options: [{ max: 1, maxEOF: 0 }],
errors: [getExpectedErrorEOF(0)]
}
]
});