Refactor to remove helpers.getPlatformIdentifier and simplify.

This commit is contained in:
David Anson 2022-05-05 23:14:18 -07:00
parent 295e481552
commit 19dfb960f1
3 changed files with 27 additions and 68 deletions

View file

@ -774,25 +774,14 @@ function emphasisMarkersInContent(params) {
return byLine; return byLine;
} }
module.exports.emphasisMarkersInContent = emphasisMarkersInContent; module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
/**
* Gets the platform identifier from a string or process-like object.
*
* @param {string} platform Platform identifier (process.platform).
* @param {Object} process Node.js process object.
* @returns {string} Platform identifier.
*/
function getPlatformIdentifier(platform, process) {
return (platform || (process && process.platform) || "unknown");
}
module.exports.getPlatformIdentifier = getPlatformIdentifier;
/** /**
* Gets the most common line ending, falling back to the platform default. * Gets the most common line ending, falling back to the platform default.
* *
* @param {string} input Markdown content to analyze. * @param {string} input Markdown content to analyze.
* @param {string} [platform] Platform identifier (process.platform). * @param {Object} [process] Node.js process object.
* @returns {string} Preferred line ending. * @returns {string} Preferred line ending.
*/ */
function getPreferredLineEnding(input, platform) { function getPreferredLineEnding(input, process) {
var cr = 0; var cr = 0;
var lf = 0; var lf = 0;
var crlf = 0; var crlf = 0;
@ -814,10 +803,7 @@ function getPreferredLineEnding(input, platform) {
var preferredLineEnding = null; var preferredLineEnding = null;
if (!cr && !lf && !crlf) { if (!cr && !lf && !crlf) {
preferredLineEnding = preferredLineEnding =
// eslint-disable-next-line node/prefer-global/process (process && (process.platform === "win32")) ? "\r\n" : "\n";
(getPlatformIdentifier(platform, __webpack_require__(/*! process */ "?4c74")) === "win32") ?
"\r\n" :
"\n";
} }
else if ((lf >= crlf) && (lf >= cr)) { else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n"; preferredLineEnding = "\n";
@ -869,11 +855,11 @@ module.exports.applyFix = applyFix;
* *
* @param {string} input Lines of Markdown content. * @param {string} input Lines of Markdown content.
* @param {Object[]} errors RuleOnErrorInfo instances. * @param {Object[]} errors RuleOnErrorInfo instances.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Corrected content. * @returns {string} Corrected content.
*/ */
function applyFixes(input, errors, platform) { function applyFixes(input, errors) {
var lineEnding = getPreferredLineEnding(input, platform); // eslint-disable-next-line node/prefer-global/process
var lineEnding = getPreferredLineEnding(input, __webpack_require__(/*! process */ "?4c74"));
var lines = input.split(newLineRe); var lines = input.split(newLineRe);
// Normalize fixInfo objects // Normalize fixInfo objects
var fixInfos = errors var fixInfos = errors

View file

@ -789,26 +789,14 @@ function emphasisMarkersInContent(params) {
} }
module.exports.emphasisMarkersInContent = emphasisMarkersInContent; module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
/**
* Gets the platform identifier from a string or process-like object.
*
* @param {string} platform Platform identifier (process.platform).
* @param {Object} process Node.js process object.
* @returns {string} Platform identifier.
*/
function getPlatformIdentifier(platform, process) {
return (platform || (process && process.platform) || "unknown");
}
module.exports.getPlatformIdentifier = getPlatformIdentifier;
/** /**
* Gets the most common line ending, falling back to the platform default. * Gets the most common line ending, falling back to the platform default.
* *
* @param {string} input Markdown content to analyze. * @param {string} input Markdown content to analyze.
* @param {string} [platform] Platform identifier (process.platform). * @param {Object} [process] Node.js process object.
* @returns {string} Preferred line ending. * @returns {string} Preferred line ending.
*/ */
function getPreferredLineEnding(input, platform) { function getPreferredLineEnding(input, process) {
let cr = 0; let cr = 0;
let lf = 0; let lf = 0;
let crlf = 0; let crlf = 0;
@ -830,10 +818,7 @@ function getPreferredLineEnding(input, platform) {
let preferredLineEnding = null; let preferredLineEnding = null;
if (!cr && !lf && !crlf) { if (!cr && !lf && !crlf) {
preferredLineEnding = preferredLineEnding =
// eslint-disable-next-line node/prefer-global/process (process && (process.platform === "win32")) ? "\r\n" : "\n";
(getPlatformIdentifier(platform, require("process")) === "win32") ?
"\r\n" :
"\n";
} else if ((lf >= crlf) && (lf >= cr)) { } else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n"; preferredLineEnding = "\n";
} else if (crlf >= cr) { } else if (crlf >= cr) {
@ -885,11 +870,11 @@ module.exports.applyFix = applyFix;
* *
* @param {string} input Lines of Markdown content. * @param {string} input Lines of Markdown content.
* @param {Object[]} errors RuleOnErrorInfo instances. * @param {Object[]} errors RuleOnErrorInfo instances.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Corrected content. * @returns {string} Corrected content.
*/ */
function applyFixes(input, errors, platform) { function applyFixes(input, errors) {
const lineEnding = getPreferredLineEnding(input, platform); // eslint-disable-next-line node/prefer-global/process
const lineEnding = getPreferredLineEnding(input, require("process"));
const lines = input.split(newLineRe); const lines = input.split(newLineRe);
// Normalize fixInfo objects // Normalize fixInfo objects
let fixInfos = errors let fixInfos = errors

View file

@ -2,7 +2,6 @@
"use strict"; "use strict";
const os = require("os");
const test = require("ava").default; const test = require("ava").default;
const helpers = require("../helpers"); const helpers = require("../helpers");
@ -390,18 +389,10 @@ test("forEachInlineCodeSpan", (t) => {
}); });
}); });
test("getPlatformIdentifier", (t) => {
t.plan(4);
t.is(helpers.getPlatformIdentifier("custom", process), "custom");
t.is(helpers.getPlatformIdentifier("custom", null), "custom");
t.is(helpers.getPlatformIdentifier(null, process), process.platform);
t.is(helpers.getPlatformIdentifier(null, null), "unknown");
});
test("getPreferredLineEnding", (t) => { test("getPreferredLineEnding", (t) => {
t.plan(19); t.plan(21);
const testCases = [ const testCases = [
[ "", os.EOL ], [ "", "\n" ],
[ "\r", "\r" ], [ "\r", "\r" ],
[ "\n", "\n" ], [ "\n", "\n" ],
[ "\r\n", "\r\n" ], [ "\r\n", "\r\n" ],
@ -425,12 +416,22 @@ test("getPreferredLineEnding", (t) => {
t.is(actual, expected, "Incorrect line ending returned."); t.is(actual, expected, "Incorrect line ending returned.");
}); });
t.is( t.is(
helpers.getPreferredLineEnding("", "linux"), helpers.getPreferredLineEnding(""),
"\n",
"Incorrect line ending for undefined platform"
);
t.is(
helpers.getPreferredLineEnding("", { "platform": "darwin" }),
"\n",
"Incorrect line ending for darwin"
);
t.is(
helpers.getPreferredLineEnding("", { "platform": "linux" }),
"\n", "\n",
"Incorrect line ending for linux" "Incorrect line ending for linux"
); );
t.is( t.is(
helpers.getPreferredLineEnding("", "win32"), helpers.getPreferredLineEnding("", { "platform": "win32" }),
"\r\n", "\r\n",
"Incorrect line ending for win32" "Incorrect line ending for win32"
); );
@ -485,7 +486,7 @@ test("applyFix", (t) => {
}); });
test("applyFixes", (t) => { test("applyFixes", (t) => {
t.plan(33); t.plan(30);
const testCases = [ const testCases = [
[ [
"Hello world.", "Hello world.",
@ -960,19 +961,6 @@ test("applyFixes", (t) => {
const actual = helpers.applyFixes(input, errors); const actual = helpers.applyFixes(input, errors);
t.is(actual, expected, "Incorrect fix applied."); 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) => { test("deepFreeze", (t) => {