Add optional "platform" parameter to helpers.applyFixes (closes #496).

This commit is contained in:
David Anson 2022-02-09 22:44:49 -08:00
parent 4593b61ff5
commit b9474e84a3
3 changed files with 41 additions and 11 deletions

View file

@ -716,7 +716,7 @@ function normalizeFixInfo(fixInfo, lineNumber) {
*
* @param {string} line Line of Markdown content.
* @param {Object} fixInfo RuleOnErrorFixInfo instance.
* @param {string} lineEnding Line ending to use.
* @param {string} [lineEnding] Line ending to use.
* @returns {string} Fixed content.
*/
function applyFix(line, fixInfo, lineEnding) {
@ -729,9 +729,16 @@ function applyFix(line, fixInfo, lineEnding) {
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) {
var lineEnding = getPreferredLineEnding(input);
/**
* Applies as many fixes as possible to Markdown content.
*
* @param {string} input Lines of Markdown content.
* @param {Object[]} errors RuleOnErrorInfo instances.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Corrected content.
*/
function applyFixes(input, errors, platform) {
var lineEnding = getPreferredLineEnding(input, platform);
var lines = input.split(newLineRe);
// Normalize fixInfo objects
var fixInfos = errors
@ -789,7 +796,8 @@ module.exports.applyFixes = function applyFixes(input, errors) {
});
// Return corrected input
return lines.filter(function (line) { return line !== null; }).join(lineEnding);
};
}
module.exports.applyFixes = applyFixes;
/**
* Gets the range and fixInfo values for reporting an error if the expected
* text is found on the specified line.

View file

@ -741,7 +741,7 @@ function normalizeFixInfo(fixInfo, lineNumber) {
*
* @param {string} line Line of Markdown content.
* @param {Object} fixInfo RuleOnErrorFixInfo instance.
* @param {string} lineEnding Line ending to use.
* @param {string} [lineEnding] Line ending to use.
* @returns {string} Fixed content.
*/
function applyFix(line, fixInfo, lineEnding) {
@ -755,9 +755,16 @@ function applyFix(line, fixInfo, lineEnding) {
}
module.exports.applyFix = applyFix;
// Applies as many fixes as possible to the input lines
module.exports.applyFixes = function applyFixes(input, errors) {
const lineEnding = getPreferredLineEnding(input);
/**
* Applies as many fixes as possible to Markdown content.
*
* @param {string} input Lines of Markdown content.
* @param {Object[]} errors RuleOnErrorInfo instances.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Corrected content.
*/
function applyFixes(input, errors, platform) {
const lineEnding = getPreferredLineEnding(input, platform);
const lines = input.split(newLineRe);
// Normalize fixInfo objects
let fixInfos = errors
@ -822,7 +829,8 @@ module.exports.applyFixes = function applyFixes(input, errors) {
});
// Return corrected input
return lines.filter((line) => line !== null).join(lineEnding);
};
}
module.exports.applyFixes = applyFixes;
/**
* Gets the range and fixInfo values for reporting an error if the expected

View file

@ -473,7 +473,7 @@ test("applyFix", (t) => {
});
test("applyFixes", (t) => {
t.plan(30);
t.plan(33);
const testCases = [
[
"Hello world.",
@ -944,9 +944,23 @@ test("applyFixes", (t) => {
];
testCases.forEach((testCase) => {
const [ input, errors, expected ] = testCase;
// @ts-ignore
const actual = helpers.applyFixes(input, errors);
t.is(actual, expected, "Incorrect fix applied.");
});
const input = "# Heading";
const errors = [
{
"lineNumber": 1,
"fixInfo": {
"editColumn": input.length + 1,
"insertText": "\n"
}
}
];
t.is(helpers.applyFixes(input, errors, "darwin"), `${input}\n`);
t.is(helpers.applyFixes(input, errors, "linux"), `${input}\n`);
t.is(helpers.applyFixes(input, errors, "win32"), `${input}\r\n`);
});
test("deepFreeze", (t) => {