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. * 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 {Object} [process] Node.js process object. * @param {Object} [os] Node.js "os" module.
* @returns {string} Preferred line ending. * @returns {string} Preferred line ending.
*/ */
function getPreferredLineEnding(input, process) { function getPreferredLineEnding(input, os) {
var cr = 0; var cr = 0;
var lf = 0; var lf = 0;
var crlf = 0; var crlf = 0;
@ -802,8 +802,7 @@ function getPreferredLineEnding(input, process) {
}); });
var preferredLineEnding = null; var preferredLineEnding = null;
if (!cr && !lf && !crlf) { if (!cr && !lf && !crlf) {
preferredLineEnding = preferredLineEnding = (os && os.EOL) || "\n";
(process && (process.platform === "win32")) ? "\r\n" : "\n";
} }
else if ((lf >= crlf) && (lf >= cr)) { else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n"; preferredLineEnding = "\n";
@ -858,8 +857,7 @@ module.exports.applyFix = applyFix;
* @returns {string} Corrected content. * @returns {string} Corrected content.
*/ */
function applyFixes(input, errors) { function applyFixes(input, errors) {
// eslint-disable-next-line node/prefer-global/process var lineEnding = getPreferredLineEnding(input, __webpack_require__(/*! os */ "?591e"));
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
@ -1013,10 +1011,10 @@ module.exports = markdownit;
/***/ }), /***/ }),
/***/ "?4c74": /***/ "?591e":
/*!*************************!*\ /*!********************!*\
!*** process (ignored) ***! !*** os (ignored) ***!
\*************************/ \********************/
/***/ (() => { /***/ (() => {
/* (ignored) */ /* (ignored) */

View file

@ -44,8 +44,8 @@ function config(options) {
"resolve": { "resolve": {
"fallback": { "fallback": {
"fs": false, "fs": false,
"os": false,
"path": false, "path": false,
"process": false,
"util": 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. * 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 {Object} [process] Node.js process object. * @param {Object} [os] Node.js "os" module.
* @returns {string} Preferred line ending. * @returns {string} Preferred line ending.
*/ */
function getPreferredLineEnding(input, process) { function getPreferredLineEnding(input, os) {
let cr = 0; let cr = 0;
let lf = 0; let lf = 0;
let crlf = 0; let crlf = 0;
@ -817,8 +817,7 @@ function getPreferredLineEnding(input, process) {
}); });
let preferredLineEnding = null; let preferredLineEnding = null;
if (!cr && !lf && !crlf) { if (!cr && !lf && !crlf) {
preferredLineEnding = preferredLineEnding = (os && os.EOL) || "\n";
(process && (process.platform === "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) {
@ -873,8 +872,7 @@ module.exports.applyFix = applyFix;
* @returns {string} Corrected content. * @returns {string} Corrected content.
*/ */
function applyFixes(input, errors) { function applyFixes(input, errors) {
// eslint-disable-next-line node/prefer-global/process const lineEnding = getPreferredLineEnding(input, require("os"));
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

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