UNPKG

1.97 kBJavaScriptView Raw
1import { LinesAndColumns } from "lines-and-columns";
2import * as utils from "prettier-linter-helpers";
3export var Operation;
4(function (Operation) {
5 Operation[Operation["Delete"] = 0] = "Delete";
6 Operation[Operation["Insert"] = 1] = "Insert";
7 Operation[Operation["Replace"] = 2] = "Replace";
8})(Operation || (Operation = {}));
9export function diagnose(input, output) {
10 return utils
11 .generateDifferences(input, output)
12 .map((difference) => {
13 const { offset: startOffset, deleteText = "", insertText = "", } = difference;
14 const endOffset = startOffset + deleteText.length;
15 const insertCode = utils.showInvisibles(insertText);
16 const deleteCode = utils.showInvisibles(deleteText);
17 let message;
18 let operation;
19 switch (difference.operation) {
20 case "delete":
21 operation = Operation.Delete;
22 message = `Delete \`${deleteCode}\``;
23 break;
24 case "insert":
25 operation = Operation.Insert;
26 message = `Insert \`${insertCode}\``;
27 break;
28 case "replace":
29 operation = Operation.Replace;
30 message = `Replace \`${deleteCode}\` with \`${insertCode}\``;
31 break;
32 /* c8 ignore start */
33 default:
34 throw new Error(`Unexpected operation '${difference.operation}'`);
35 /* c8 ignore stop */
36 }
37 const locator = new LinesAndColumns(input);
38 const getLocation = (offset) => {
39 const { line, column } = locator.locationForIndex(offset);
40 return { line: line + 1, column: column + 1, offset };
41 };
42 return {
43 start: getLocation(startOffset),
44 end: getLocation(endOffset),
45 message,
46 operation,
47 insertText: insertText,
48 deleteText: deleteText,
49 };
50 });
51}