UNPKG

@nerdo/code-reviewer

Version:

A web-based visual git diff tool for reviewing code changes between commits, branches, and tags

146 lines (145 loc) 5.35 kB
"use strict"; 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DiffService = void 0; const diff = __importStar(require("diff")); class DiffService { generateDiff(oldContent, newContent) { const changes = diff.createPatch('file', oldContent, newContent); return this.parsePatch(changes); } generateFullFileDiff(oldContent, newContent) { // Use line-by-line diff to get all changes const changes = diff.diffLines(oldContent, newContent); const hunk = { oldStart: 1, oldLines: oldContent.split('\n').length, newStart: 1, newLines: newContent.split('\n').length, lines: [] }; let oldLineNumber = 1; let newLineNumber = 1; for (const change of changes) { const lines = change.value.split('\n'); // Remove empty last line if it exists (artifact of splitting) if (lines[lines.length - 1] === '') { lines.pop(); } for (const line of lines) { if (change.added) { hunk.lines.push({ type: 'add', content: line, newLineNumber: newLineNumber++ }); } else if (change.removed) { hunk.lines.push({ type: 'delete', content: line, oldLineNumber: oldLineNumber++ }); } else { hunk.lines.push({ type: 'normal', content: line, oldLineNumber: oldLineNumber++, newLineNumber: newLineNumber++ }); } } } return [hunk]; } parsePatch(patch) { const lines = patch.split('\n'); const hunks = []; let currentHunk = null; let oldLineNumber = 0; let newLineNumber = 0; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const hunkMatch = line.match(/^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/); if (hunkMatch) { if (currentHunk) { hunks.push(currentHunk); } oldLineNumber = parseInt(hunkMatch[1]); newLineNumber = parseInt(hunkMatch[3]); currentHunk = { oldStart: oldLineNumber, oldLines: parseInt(hunkMatch[2] || '1'), newStart: newLineNumber, newLines: parseInt(hunkMatch[4] || '1'), lines: [] }; continue; } if (currentHunk && line.length > 0 && i > 3) { const firstChar = line[0]; const content = line.substring(1); if (firstChar === '+') { currentHunk.lines.push({ type: 'add', content, newLineNumber: newLineNumber++ }); } else if (firstChar === '-') { currentHunk.lines.push({ type: 'delete', content, oldLineNumber: oldLineNumber++ }); } else if (firstChar === ' ') { currentHunk.lines.push({ type: 'normal', content, oldLineNumber: oldLineNumber++, newLineNumber: newLineNumber++ }); } } } if (currentHunk) { hunks.push(currentHunk); } return hunks; } } exports.DiffService = DiffService;