Rename and refactor applyFix from fixErrors for one-off scenarios.

This commit is contained in:
David Anson 2019-09-12 21:50:40 -07:00
parent 0502e370de
commit 5895ea62cb
2 changed files with 35 additions and 25 deletions

View file

@ -399,19 +399,35 @@ module.exports.frontMatterHasTitle =
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
};
// Applies as many fixes as possible to the input
module.exports.fixErrors = function fixErrors(input, errors) {
const lines = input.split(newLineRe);
// Normalize fixInfo objects
let fixInfos = errors.filter((error) => !!error.fixInfo).map((error) => {
const { fixInfo } = error;
// Normalizes the fields of a fixInfo object
function normalizeFixInfo(fixInfo, lineNumber) {
return {
"lineNumber": fixInfo.lineNumber || error.lineNumber,
"lineNumber": fixInfo.lineNumber || lineNumber,
"editColumn": fixInfo.editColumn || 1,
"deleteCount": fixInfo.deleteCount || 0,
"insertText": fixInfo.insertText || ""
};
});
}
// Fixes the specifide error on a line
function applyFix(line, fixInfo) {
const { editColumn, deleteCount, insertText } = normalizeFixInfo(fixInfo);
const editIndex = editColumn - 1;
return (deleteCount === -1) ?
null :
line.slice(0, editIndex) +
insertText +
line.slice(editIndex + deleteCount);
}
module.exports.applyFix = applyFix;
// Applies as many fixes as possible to the input lines
module.exports.applyFixes = function applyFixes(input, errors) {
const lines = input.split(newLineRe);
// Normalize fixInfo objects
let fixInfos = errors
.filter((error) => error.fixInfo)
.map((error) => normalizeFixInfo(error.fixInfo, error.lineNumber));
// Sort bottom-to-top, line-deletes last, right-to-left, long-to-short
fixInfos.sort((a, b) => {
const aDeletingLine = (a.deleteCount === -1);
@ -455,7 +471,7 @@ module.exports.fixErrors = function fixErrors(input, errors) {
let lastLineIndex = -1;
let lastEditIndex = -1;
fixInfos.forEach((fixInfo) => {
const { lineNumber, editColumn, deleteCount, insertText } = fixInfo;
const { lineNumber, editColumn, deleteCount } = fixInfo;
const lineIndex = lineNumber - 1;
const editIndex = editColumn - 1;
if (
@ -463,13 +479,7 @@ module.exports.fixErrors = function fixErrors(input, errors) {
((editIndex + deleteCount) < lastEditIndex) ||
(deleteCount === -1)
) {
const line = lines[lineIndex];
lines[lineIndex] =
(deleteCount === -1) ?
null :
line.slice(0, editIndex) +
insertText +
line.slice(editIndex + deleteCount);
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo);
}
lastLineIndex = lineIndex;
lastEditIndex = editIndex;

View file

@ -118,14 +118,14 @@ function createTestForFile(file) {
return actual2or3;
})
.then(
function verifyFixErrors(errors) {
function verifyFixes(errors) {
if (detailedResults) {
return test.ok(true);
}
return promisify(fs.readFile, file, helpers.utf8Encoding)
.then(
function applyFixErrors(content) {
const corrections = helpers.fixErrors(content, errors);
function applyFixes(content) {
const corrections = helpers.applyFixes(content, errors);
return promisify(markdownlint, {
"strings": {
"input": corrections
@ -135,7 +135,7 @@ function createTestForFile(file) {
});
})
.then(
function checkFixErrors(newErrors) {
function checkFixes(newErrors) {
const unfixed = newErrors.input
.filter((error) => !!error.fixInfo);
test.deepEqual(unfixed, [], "Fixable error was not fixed.");
@ -1897,7 +1897,7 @@ module.exports.forEachInlineCodeSpan = function forEachInlineCodeSpan(test) {
test.done();
};
module.exports.fixErrors = function fixErrors(test) {
module.exports.applyFixes = function applyFixes(test) {
test.expect(24);
const testCases = [
[
@ -2278,7 +2278,7 @@ module.exports.fixErrors = function fixErrors(test) {
];
testCases.forEach((testCase) => {
const [ input, errors, expected ] = testCase;
const actual = helpers.fixErrors(input, errors);
const actual = helpers.applyFixes(input, errors);
test.equal(actual, expected, "Incorrect fix applied.");
});
test.done();