Skip to content

Commit

Permalink
Chore: use ast-utils helper functions in no-multiple-empty-lines (#7842)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and nzakas committed Jan 3, 2017
1 parent 7122205 commit 18fa521
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rules/no-multiple-empty-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
"use strict";

const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -56,8 +58,6 @@ module.exports = {
}

const sourceCode = context.getSourceCode();
const fullLines = sourceCode.text.match(/.*(\r\n|\r|\n|\u2028|\u2029)/g) || [];
const lineStartLocations = fullLines.reduce((startIndices, nextLine) => startIndices.concat(startIndices[startIndices.length - 1] + nextLine.length), [0]);

// Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue
const allLines = sourceCode.lines[sourceCode.lines.length - 1] === "" ? sourceCode.lines.slice(0, -1) : sourceCode.lines;
Expand Down Expand Up @@ -107,7 +107,12 @@ module.exports = {
loc: { start: { line: lastLineNumber + 1, column: 0 }, end: { line: lineNumber, column: 0 } },
message,
data: { max: maxAllowed, pluralizedLines: maxAllowed === 1 ? "line" : "lines" },
fix: fixer => fixer.removeRange([lineStartLocations[lastLineNumber], lineStartLocations[lineNumber - maxAllowed - 1]])
fix(fixer) {
return fixer.removeRange([
astUtils.getRangeIndexFromLocation(sourceCode, { line: lastLineNumber + 1, column: 0 }),
astUtils.getRangeIndexFromLocation(sourceCode, { line: lineNumber - maxAllowed, column: 0 })
]);
}
});
}

Expand Down

0 comments on commit 18fa521

Please sign in to comment.