UNPKG

@dev-build-deploy/diagnose-it

Version:
107 lines (106 loc) 4.57 kB
"use strict"; /* * SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com> * SPDX-License-Identifier: MIT */ 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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.applyPatch = exports.createPatch = void 0; const fs_1 = __importDefault(require("fs")); const diff = __importStar(require("diff")); /** * Creates a patch based on an unified Diff. * * NOTE: This is not a full implementation of the unified Diff format, * but only supports the bare minimum required to create a patch * that applies Fix-It Hints, covering a single line in a single * hunk. * @returns Patch. */ function createPatch(message) { var _a, _b, _c, _d; if (message.context === undefined) { throw new Error("Cannot apply FixIt hints without a context."); } // get modification date of file as Date object const stats = fs_1.default.existsSync(message.file) ? fs_1.default.statSync(message.file) : undefined; const mtime = stats ? new Date(stats.mtimeMs) : new Date(); const original = (_a = message.context) === null || _a === void 0 ? void 0 : _a.lines[((_b = message.message.linenumber) !== null && _b !== void 0 ? _b : 1) - message.context.linenumber]; // apply fixit hints to original string let expected = original; // each fixit hint modifies the string, so we need to keep track of the offset let offset = 0; // Use a copy of the array to prevent modifying the original const fixitHints = [...message.getFixitHints()]; fixitHints.forEach(fixit => { var _a, _b, _c, _d; fixit.range.index += offset; expected = fixit.apply(expected); // update offset based on modification type switch (fixit.modification) { case "INSERT": offset += (_b = (_a = fixit.text) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; break; case "REMOVE": offset -= fixit.range.length; break; case "REPLACE": offset += ((_d = (_c = fixit.text) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : fixit.range.length) - fixit.range.length; break; } }); const linenumber = message.context.linenumber; const linecount = message.context.lines.length; let result = `--- ${message.file} ${mtime.toLocaleString("en-GB")} +++ ${message.file}.fix ${new Date().toLocaleString("en-GB")} @@ -${linenumber},${linecount} +${linenumber},${linecount} @@`; for (const line of (_d = (_c = message.context) === null || _c === void 0 ? void 0 : _c.lines) !== null && _d !== void 0 ? _d : []) { const modification = line === original && original !== expected; result += `\n${modification ? "-" : " "}${line}`; if (modification) { result += `\n+${expected}`; } } return result; } exports.createPatch = createPatch; /** * Applies a patch (see createPatch) to a file using a Buffer. * @param patch * @internal */ function applyPatch(patch) { const filename = patch.split(/\r?\n/)[0].split(" ")[1]; const source = fs_1.default.readFileSync(filename, "utf-8"); const res = diff.applyPatch(source, patch); if (res === false) { throw new Error("Failed to apply patch"); } return res; } exports.applyPatch = applyPatch;