mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Rename and refactor applyFix from fixErrors for one-off scenarios.
This commit is contained in:
parent
0502e370de
commit
5895ea62cb
2 changed files with 35 additions and 25 deletions
|
|
@ -399,19 +399,35 @@ module.exports.frontMatterHasTitle =
|
||||||
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
|
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Applies as many fixes as possible to the input
|
// Normalizes the fields of a fixInfo object
|
||||||
module.exports.fixErrors = function fixErrors(input, errors) {
|
function normalizeFixInfo(fixInfo, lineNumber) {
|
||||||
const lines = input.split(newLineRe);
|
|
||||||
// Normalize fixInfo objects
|
|
||||||
let fixInfos = errors.filter((error) => !!error.fixInfo).map((error) => {
|
|
||||||
const { fixInfo } = error;
|
|
||||||
return {
|
return {
|
||||||
"lineNumber": fixInfo.lineNumber || error.lineNumber,
|
"lineNumber": fixInfo.lineNumber || lineNumber,
|
||||||
"editColumn": fixInfo.editColumn || 1,
|
"editColumn": fixInfo.editColumn || 1,
|
||||||
"deleteCount": fixInfo.deleteCount || 0,
|
"deleteCount": fixInfo.deleteCount || 0,
|
||||||
"insertText": fixInfo.insertText || ""
|
"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
|
// Sort bottom-to-top, line-deletes last, right-to-left, long-to-short
|
||||||
fixInfos.sort((a, b) => {
|
fixInfos.sort((a, b) => {
|
||||||
const aDeletingLine = (a.deleteCount === -1);
|
const aDeletingLine = (a.deleteCount === -1);
|
||||||
|
|
@ -455,7 +471,7 @@ module.exports.fixErrors = function fixErrors(input, errors) {
|
||||||
let lastLineIndex = -1;
|
let lastLineIndex = -1;
|
||||||
let lastEditIndex = -1;
|
let lastEditIndex = -1;
|
||||||
fixInfos.forEach((fixInfo) => {
|
fixInfos.forEach((fixInfo) => {
|
||||||
const { lineNumber, editColumn, deleteCount, insertText } = fixInfo;
|
const { lineNumber, editColumn, deleteCount } = fixInfo;
|
||||||
const lineIndex = lineNumber - 1;
|
const lineIndex = lineNumber - 1;
|
||||||
const editIndex = editColumn - 1;
|
const editIndex = editColumn - 1;
|
||||||
if (
|
if (
|
||||||
|
|
@ -463,13 +479,7 @@ module.exports.fixErrors = function fixErrors(input, errors) {
|
||||||
((editIndex + deleteCount) < lastEditIndex) ||
|
((editIndex + deleteCount) < lastEditIndex) ||
|
||||||
(deleteCount === -1)
|
(deleteCount === -1)
|
||||||
) {
|
) {
|
||||||
const line = lines[lineIndex];
|
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo);
|
||||||
lines[lineIndex] =
|
|
||||||
(deleteCount === -1) ?
|
|
||||||
null :
|
|
||||||
line.slice(0, editIndex) +
|
|
||||||
insertText +
|
|
||||||
line.slice(editIndex + deleteCount);
|
|
||||||
}
|
}
|
||||||
lastLineIndex = lineIndex;
|
lastLineIndex = lineIndex;
|
||||||
lastEditIndex = editIndex;
|
lastEditIndex = editIndex;
|
||||||
|
|
|
||||||
|
|
@ -118,14 +118,14 @@ function createTestForFile(file) {
|
||||||
return actual2or3;
|
return actual2or3;
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
function verifyFixErrors(errors) {
|
function verifyFixes(errors) {
|
||||||
if (detailedResults) {
|
if (detailedResults) {
|
||||||
return test.ok(true);
|
return test.ok(true);
|
||||||
}
|
}
|
||||||
return promisify(fs.readFile, file, helpers.utf8Encoding)
|
return promisify(fs.readFile, file, helpers.utf8Encoding)
|
||||||
.then(
|
.then(
|
||||||
function applyFixErrors(content) {
|
function applyFixes(content) {
|
||||||
const corrections = helpers.fixErrors(content, errors);
|
const corrections = helpers.applyFixes(content, errors);
|
||||||
return promisify(markdownlint, {
|
return promisify(markdownlint, {
|
||||||
"strings": {
|
"strings": {
|
||||||
"input": corrections
|
"input": corrections
|
||||||
|
|
@ -135,7 +135,7 @@ function createTestForFile(file) {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
function checkFixErrors(newErrors) {
|
function checkFixes(newErrors) {
|
||||||
const unfixed = newErrors.input
|
const unfixed = newErrors.input
|
||||||
.filter((error) => !!error.fixInfo);
|
.filter((error) => !!error.fixInfo);
|
||||||
test.deepEqual(unfixed, [], "Fixable error was not fixed.");
|
test.deepEqual(unfixed, [], "Fixable error was not fixed.");
|
||||||
|
|
@ -1897,7 +1897,7 @@ module.exports.forEachInlineCodeSpan = function forEachInlineCodeSpan(test) {
|
||||||
test.done();
|
test.done();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.fixErrors = function fixErrors(test) {
|
module.exports.applyFixes = function applyFixes(test) {
|
||||||
test.expect(24);
|
test.expect(24);
|
||||||
const testCases = [
|
const testCases = [
|
||||||
[
|
[
|
||||||
|
|
@ -2278,7 +2278,7 @@ module.exports.fixErrors = function fixErrors(test) {
|
||||||
];
|
];
|
||||||
testCases.forEach((testCase) => {
|
testCases.forEach((testCase) => {
|
||||||
const [ input, errors, expected ] = 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.equal(actual, expected, "Incorrect fix applied.");
|
||||||
});
|
});
|
||||||
test.done();
|
test.done();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue