@textlint/fixer-formatter
Version:
textlint output formatter for fixer
175 lines • 6.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const fs = __importStar(require("node:fs"));
const diff_1 = require("diff");
const chalk_1 = __importDefault(require("chalk"));
const strip_ansi_1 = __importDefault(require("strip-ansi"));
const isFile = (filePath) => {
try {
const stats = fs.statSync(filePath);
return stats.isFile();
}
catch (error) {
return false;
}
};
/**
* Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form.
* @param {number} count A number controlling whether word should be pluralized.
* @returns {string} The original word with an s on the end if count is not one.
*/
function pluralize(word, count) {
return count === 1 ? word : `${word}s`;
}
function isModified(part) {
if (!part) {
return false;
}
return typeof part === "object" && part !== null && (!!part.removed || !!part.added);
}
function addMarkEachLine(mark, text) {
if (text.length === 0) {
return "\n";
}
const lines = text.split("\n");
const markedLines = lines
.filter((line) => line.length > 0)
.map((line) => {
return mark + line;
});
return `${markedLines.join("\n")}\n`;
}
function default_1(results, options = {}) {
// default: true
const useColor = options.color !== undefined ? options.color : true;
let output = "\n";
let totalFixed = 0;
let errors = 0;
const summaryColor = "yellow";
const greenColor = "green";
results.forEach(function (result) {
const filePath = result.filePath;
const messages = result.applyingMessages;
// still error count
const remainingMessages = result.remainingMessages;
errors += remainingMessages.length;
totalFixed += messages.length;
if (messages.length === 0) {
return;
}
if (!isFile(filePath)) {
return;
}
output += `${chalk_1.default.underline(result.filePath)}\n`;
const originalContent = fs.readFileSync(filePath, "utf-8");
const diff = (0, diff_1.diffLines)(originalContent, result.output);
diff.forEach(function (part, index) {
var _a;
const prevLine = diff[index - 1];
const nextLine = diff[index + 1];
if (!isModified(part) && ((_a = part.count) !== null && _a !== void 0 ? _a : 0) > 1) {
const greyColor = "grey";
/*
<MODIFIED>
first line
....
*/
if (isModified(prevLine)) {
const lines = part.value.split("\n");
output += `${chalk_1.default[greyColor](lines[0])}\n`;
}
output += chalk_1.default[greyColor]("...");
if (isModified(nextLine)) {
const lines = part.value.split("\n");
output += `${chalk_1.default[greyColor](lines[lines.length - 1])}\n`;
}
/*
...
last line
<MODIFIED>
*/
return;
}
// green for additions, red for deletions
// grey for common parts
let lineColor;
let diffMark = "";
if (part.added) {
lineColor = "green";
diffMark = "+ ";
}
else if (part.removed) {
lineColor = "red";
diffMark = "- ";
}
else {
lineColor = "grey";
diffMark = "";
}
output += chalk_1.default[lineColor](addMarkEachLine(diffMark, part.value));
});
output += "\n\n";
});
if (totalFixed > 0) {
output += chalk_1.default[greenColor].bold([
// http://www.fileformat.info/info/unicode/char/2714/index.htm
"✔ Fixed ",
totalFixed,
pluralize(" problem", totalFixed),
"\n"
].join(""));
}
if (errors > 0) {
output += chalk_1.default[summaryColor].bold([
// http://www.fileformat.info/info/unicode/char/2716/index.htm
"✖ Remaining ",
errors,
pluralize(" problem", errors),
"\n"
].join(""));
}
const finalOutput = totalFixed > 0 ? output : "";
if (!useColor) {
return (0, strip_ansi_1.default)(finalOutput);
}
return finalOutput;
}
//# sourceMappingURL=diff.js.map