Skip to content

Commit

Permalink
create method to return ParsedDiff objects from code lines
Browse files Browse the repository at this point in the history
Summary:
Context: Code Patch suggestion returns the code diff in the format of two strings (oldCode and newCode). Need a way to parse it into ParsedDiff object to fit the structures that ISL already have.

Will be used in later diff

Reviewed By: evangrayk

Differential Revision: D64094142

fbshipit-source-id: 2807532054704ea7b4d98ed57e895ea77fbb2a3c
  • Loading branch information
Jack Huang authored and facebook-github-bot committed Oct 10, 2024
1 parent 366e54b commit 301426d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
48 changes: 47 additions & 1 deletion addons/shared/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {parsePatch} from '../patch/parse';
import {splitLines} from '../diff';
import {parseParsedDiff, parsePatch} from '../patch/parse';

describe('patch/parse', () => {
it('should parse basic modified patch', () => {
Expand Down Expand Up @@ -256,3 +257,48 @@ new mode 100755
expect(() => parsePatch(patch)).toThrow("invalid format 'old mode XXX'");
});
});

describe('createParsedDiffWithLines', () => {
it('return no hunks for empty lines', () => {
expect(parseParsedDiff([], [], 1)).toMatchObject({hunks: []});
});

it('returns no hunks when comparing same lines', () => {
const lines = splitLines('a\nb\nc\nd\ne\n');
expect(parseParsedDiff(lines, lines, 1)).toMatchObject({hunks: []});
});

it('return all "-" for old code and "=" for new code for totally different contents', () => {
const aLines = splitLines('x\ny\n');
const bLines = splitLines('a\nb\nc\n');
expect(parseParsedDiff(aLines, bLines, 1)).toMatchObject({
hunks: [
{
oldStart: 1,
oldLines: 2,
newStart: 1,
newLines: 3,
lines: ['-x\n', '-y\n', '+a\n', '+b\n', '+c\n'],
linedelimiters: ['\n', '\n', '\n', '\n', '\n'],
},
],
});
});

it('reutrn for when a line was changed in the middle', () => {
const aLines = splitLines('a\nb\nc\nd\ne\n');
const bLines = splitLines('a\nb\nc\nd1\nd2\ne\n');
expect(parseParsedDiff(aLines, bLines, 1)).toMatchObject({
hunks: [
{
oldStart: 4,
oldLines: 1,
newStart: 4,
newLines: 2,
lines: ['-d\n', '+d1\n', '+d2\n'],
linedelimiters: ['\n', '\n', '\n'],
},
],
});
});
});
43 changes: 43 additions & 0 deletions addons/shared/patch/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {diffBlocks} from '../diff';

export type Hunk = {
oldStart: number;
oldLines: number;
Expand Down Expand Up @@ -264,3 +266,44 @@ export function parsePatch(patch: string): ParsedDiff[] {

return list;
}

export function parseParsedDiff(
oldCodeLines: string[],
newCodeLines: string[],
lineNumber: number,
oldFileName?: string,
newFileName?: string,
): ParsedDiff {
const hunks: Hunk[] = [];
const blocks = diffBlocks(oldCodeLines, newCodeLines);

blocks.forEach(block => {
if (block[0] === '=') {
return;
}

const oldRange = [block[1][0], block[1][1]];
const newRange = [block[1][2], block[1][3]];

const oldLines = oldCodeLines.slice(oldRange[0], oldRange[1]).map(codeStr => '-' + codeStr);
const newLines = newCodeLines.slice(newRange[0], newRange[1]).map(codeStr => '+' + codeStr);
const delimiters = new Array(oldLines.length + newLines.length).fill('\n');

const hunk: Hunk = {
oldStart: lineNumber + oldRange[0],
oldLines: oldLines.length ?? 0,
newStart: lineNumber + newRange[0],
newLines: newLines.length ?? 0,
lines: oldLines.concat(newLines),
linedelimiters: delimiters,
};

hunks.push(hunk);
});

return {
oldFileName,
newFileName,
hunks,
} as ParsedDiff;
}

0 comments on commit 301426d

Please sign in to comment.