mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Refactor to remove helpers.getPlatformIdentifier and simplify.
This commit is contained in:
parent
295e481552
commit
19dfb960f1
3 changed files with 27 additions and 68 deletions
|
|
@ -774,25 +774,14 @@ function emphasisMarkersInContent(params) {
|
|||
return byLine;
|
||||
}
|
||||
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.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
function getPreferredLineEnding(input, platform) {
|
||||
function getPreferredLineEnding(input, process) {
|
||||
var cr = 0;
|
||||
var lf = 0;
|
||||
var crlf = 0;
|
||||
|
|
@ -814,10 +803,7 @@ function getPreferredLineEnding(input, platform) {
|
|||
var preferredLineEnding = null;
|
||||
if (!cr && !lf && !crlf) {
|
||||
preferredLineEnding =
|
||||
// eslint-disable-next-line node/prefer-global/process
|
||||
(getPlatformIdentifier(platform, __webpack_require__(/*! process */ "?4c74")) === "win32") ?
|
||||
"\r\n" :
|
||||
"\n";
|
||||
(process && (process.platform === "win32")) ? "\r\n" : "\n";
|
||||
}
|
||||
else if ((lf >= crlf) && (lf >= cr)) {
|
||||
preferredLineEnding = "\n";
|
||||
|
|
@ -869,11 +855,11 @@ module.exports.applyFix = applyFix;
|
|||
*
|
||||
* @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);
|
||||
function applyFixes(input, errors) {
|
||||
// eslint-disable-next-line node/prefer-global/process
|
||||
var lineEnding = getPreferredLineEnding(input, __webpack_require__(/*! process */ "?4c74"));
|
||||
var lines = input.split(newLineRe);
|
||||
// Normalize fixInfo objects
|
||||
var fixInfos = errors
|
||||
|
|
|
|||
|
|
@ -789,26 +789,14 @@ function emphasisMarkersInContent(params) {
|
|||
}
|
||||
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.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
function getPreferredLineEnding(input, platform) {
|
||||
function getPreferredLineEnding(input, process) {
|
||||
let cr = 0;
|
||||
let lf = 0;
|
||||
let crlf = 0;
|
||||
|
|
@ -830,10 +818,7 @@ function getPreferredLineEnding(input, platform) {
|
|||
let preferredLineEnding = null;
|
||||
if (!cr && !lf && !crlf) {
|
||||
preferredLineEnding =
|
||||
// eslint-disable-next-line node/prefer-global/process
|
||||
(getPlatformIdentifier(platform, require("process")) === "win32") ?
|
||||
"\r\n" :
|
||||
"\n";
|
||||
(process && (process.platform === "win32")) ? "\r\n" : "\n";
|
||||
} else if ((lf >= crlf) && (lf >= cr)) {
|
||||
preferredLineEnding = "\n";
|
||||
} else if (crlf >= cr) {
|
||||
|
|
@ -885,11 +870,11 @@ module.exports.applyFix = applyFix;
|
|||
*
|
||||
* @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);
|
||||
function applyFixes(input, errors) {
|
||||
// eslint-disable-next-line node/prefer-global/process
|
||||
const lineEnding = getPreferredLineEnding(input, require("process"));
|
||||
const lines = input.split(newLineRe);
|
||||
// Normalize fixInfo objects
|
||||
let fixInfos = errors
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const os = require("os");
|
||||
const test = require("ava").default;
|
||||
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) => {
|
||||
t.plan(19);
|
||||
t.plan(21);
|
||||
const testCases = [
|
||||
[ "", os.EOL ],
|
||||
[ "", "\n" ],
|
||||
[ "\r", "\r" ],
|
||||
[ "\n", "\n" ],
|
||||
[ "\r\n", "\r\n" ],
|
||||
|
|
@ -425,12 +416,22 @@ test("getPreferredLineEnding", (t) => {
|
|||
t.is(actual, expected, "Incorrect line ending returned.");
|
||||
});
|
||||
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",
|
||||
"Incorrect line ending for linux"
|
||||
);
|
||||
t.is(
|
||||
helpers.getPreferredLineEnding("", "win32"),
|
||||
helpers.getPreferredLineEnding("", { "platform": "win32" }),
|
||||
"\r\n",
|
||||
"Incorrect line ending for win32"
|
||||
);
|
||||
|
|
@ -485,7 +486,7 @@ test("applyFix", (t) => {
|
|||
});
|
||||
|
||||
test("applyFixes", (t) => {
|
||||
t.plan(33);
|
||||
t.plan(30);
|
||||
const testCases = [
|
||||
[
|
||||
"Hello world.",
|
||||
|
|
@ -960,19 +961,6 @@ test("applyFixes", (t) => {
|
|||
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) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue