Refactor/simplify helpers.getPreferredLineEnding to use os.EOL instead of process.platform.

This commit is contained in:
David Anson 2022-05-06 21:04:34 -07:00
parent 19dfb960f1
commit a8f946e0b3
4 changed files with 17 additions and 37 deletions

View file

@ -778,10 +778,10 @@ module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
* Gets the most common line ending, falling back to the platform default.
*
* @param {string} input Markdown content to analyze.
* @param {Object} [process] Node.js process object.
* @param {Object} [os] Node.js "os" module.
* @returns {string} Preferred line ending.
*/
function getPreferredLineEnding(input, process) {
function getPreferredLineEnding(input, os) {
var cr = 0;
var lf = 0;
var crlf = 0;
@ -802,8 +802,7 @@ function getPreferredLineEnding(input, process) {
});
var preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding =
(process && (process.platform === "win32")) ? "\r\n" : "\n";
preferredLineEnding = (os && os.EOL) || "\n";
}
else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
@ -858,8 +857,7 @@ module.exports.applyFix = applyFix;
* @returns {string} Corrected content.
*/
function applyFixes(input, errors) {
// eslint-disable-next-line node/prefer-global/process
var lineEnding = getPreferredLineEnding(input, __webpack_require__(/*! process */ "?4c74"));
var lineEnding = getPreferredLineEnding(input, __webpack_require__(/*! os */ "?591e"));
var lines = input.split(newLineRe);
// Normalize fixInfo objects
var fixInfos = errors
@ -1013,10 +1011,10 @@ module.exports = markdownit;
/***/ }),
/***/ "?4c74":
/*!*************************!*\
!*** process (ignored) ***!
\*************************/
/***/ "?591e":
/*!********************!*\
!*** os (ignored) ***!
\********************/
/***/ (() => {
/* (ignored) */

View file

@ -44,8 +44,8 @@ function config(options) {
"resolve": {
"fallback": {
"fs": false,
"os": false,
"path": false,
"process": false,
"util": false
}
}

View file

@ -793,10 +793,10 @@ module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
* Gets the most common line ending, falling back to the platform default.
*
* @param {string} input Markdown content to analyze.
* @param {Object} [process] Node.js process object.
* @param {Object} [os] Node.js "os" module.
* @returns {string} Preferred line ending.
*/
function getPreferredLineEnding(input, process) {
function getPreferredLineEnding(input, os) {
let cr = 0;
let lf = 0;
let crlf = 0;
@ -817,8 +817,7 @@ function getPreferredLineEnding(input, process) {
});
let preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding =
(process && (process.platform === "win32")) ? "\r\n" : "\n";
preferredLineEnding = (os && os.EOL) || "\n";
} else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
} else if (crlf >= cr) {
@ -873,8 +872,7 @@ module.exports.applyFix = applyFix;
* @returns {string} Corrected content.
*/
function applyFixes(input, errors) {
// eslint-disable-next-line node/prefer-global/process
const lineEnding = getPreferredLineEnding(input, require("process"));
const lineEnding = getPreferredLineEnding(input, require("os"));
const lines = input.split(newLineRe);
// Normalize fixInfo objects
let fixInfos = errors

View file

@ -415,26 +415,10 @@ test("getPreferredLineEnding", (t) => {
const actual = helpers.getPreferredLineEnding(input);
t.is(actual, expected, "Incorrect line ending returned.");
});
t.is(
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",
"Incorrect line ending for linux"
);
t.is(
helpers.getPreferredLineEnding("", { "platform": "win32" }),
"\r\n",
"Incorrect line ending for win32"
);
t.is(helpers.getPreferredLineEnding("", null), "\n");
t.is(helpers.getPreferredLineEnding("", { "EOL": "\n" }), "\n");
t.is(helpers.getPreferredLineEnding("", { "EOL": "\r\n" }), "\r\n");
t.is(helpers.getPreferredLineEnding("", { "EOL": "custom" }), "custom");
});
test("applyFix", (t) => {