2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-04 09:49:22 +01:00
|
|
|
/**
|
|
|
|
* mdiff.js
|
|
|
|
*
|
|
|
|
* Used to produce colorful and informative diffs for comparison of generated
|
|
|
|
* Markdown. Unlike the built-in diffs used in python or node.js assert libraries,
|
|
|
|
* is actually designed to be effective for long, single-line comparisons.
|
|
|
|
*
|
|
|
|
* Based on diffing library difflib, a js port of the python library.
|
|
|
|
*
|
|
|
|
* The sole exported function diff_strings(string_0, string_1) returns a pretty-printed
|
|
|
|
* unicode string containing their diff.
|
|
|
|
*/
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const difflib = require("difflib");
|
2017-12-04 09:49:22 +01:00
|
|
|
|
|
|
|
function apply_color(input_string, changes) {
|
|
|
|
let previous_index = 0;
|
2018-12-07 22:14:28 +01:00
|
|
|
let processed_string = input_string.slice(0, 2);
|
2017-12-04 09:49:22 +01:00
|
|
|
input_string = input_string.slice(2);
|
|
|
|
|
2020-05-27 01:08:49 +02:00
|
|
|
const formatter = new Map([
|
2020-07-02 01:41:40 +02:00
|
|
|
["delete", (string) => "\u001b[31m" + string + "\u001b[0m"],
|
|
|
|
["insert", (string) => "\u001b[32m" + string + "\u001b[0m"],
|
|
|
|
["replace", (string) => "\u001b[33m" + string + "\u001b[0m"],
|
2020-05-27 01:08:49 +02:00
|
|
|
]);
|
2017-12-04 09:49:22 +01:00
|
|
|
changes.forEach((change) => {
|
2020-05-27 01:08:49 +02:00
|
|
|
if (formatter.has(change.tag)) {
|
2017-12-04 09:49:22 +01:00
|
|
|
processed_string += input_string.slice(previous_index, change.beginning_index);
|
2020-05-27 01:08:49 +02:00
|
|
|
processed_string += formatter.get(change.tag)(
|
2020-07-02 02:16:03 +02:00
|
|
|
input_string.slice(change.beginning_index, change.ending_index),
|
2017-12-04 09:49:22 +01:00
|
|
|
);
|
|
|
|
previous_index = change.ending_index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
processed_string += input_string.slice(previous_index);
|
|
|
|
return processed_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The library difflib produces diffs that look as follows:
|
|
|
|
*
|
|
|
|
* - <p>upgrade! yes</p>
|
|
|
|
* ? ^^ -
|
|
|
|
* + <p>downgrade yes.</p>
|
|
|
|
* ? ^^^^ +
|
|
|
|
*
|
|
|
|
* The purpose of this function is to facilitate converting these diffs into
|
|
|
|
* colored versions, where the question-mark lines are removed, replaced with
|
|
|
|
* directions to add appropriate color to the lines that they annotate.
|
|
|
|
*/
|
|
|
|
function parse_questionmark_line(questionmark_line) {
|
2020-07-16 23:29:01 +02:00
|
|
|
let current_sequence = ""; // Either "^", "-", "+", or ""
|
2017-12-04 09:49:22 +01:00
|
|
|
let beginning_index = 0;
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
const changes_list = [];
|
2020-05-27 01:08:49 +02:00
|
|
|
const aliases = new Map([
|
|
|
|
["^", "replace"],
|
|
|
|
["+", "insert"],
|
|
|
|
["-", "delete"],
|
|
|
|
]);
|
2017-12-04 09:49:22 +01:00
|
|
|
const add_change = () => {
|
|
|
|
if (current_sequence) {
|
|
|
|
changes_list.push({
|
2020-05-27 01:08:49 +02:00
|
|
|
tag: aliases.get(current_sequence),
|
2017-12-04 09:49:22 +01:00
|
|
|
beginning_index,
|
2018-12-18 19:34:45 +01:00
|
|
|
ending_index: index,
|
2017-12-04 09:49:22 +01:00
|
|
|
});
|
|
|
|
current_sequence = "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
questionmark_line = questionmark_line.slice(2).trimRight("\n");
|
|
|
|
|
|
|
|
for (const character of questionmark_line) {
|
2020-05-27 01:08:49 +02:00
|
|
|
if (aliases.has(character)) {
|
2017-12-04 09:49:22 +01:00
|
|
|
if (current_sequence !== character) {
|
|
|
|
add_change();
|
|
|
|
current_sequence = character;
|
|
|
|
beginning_index = index;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add_change();
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case we have a "change" involving the last character on a line
|
|
|
|
// e.g. a string such as "? ^^ -- ++++"
|
|
|
|
add_change();
|
|
|
|
|
|
|
|
return changes_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
function diff_strings(string_0, string_1) {
|
|
|
|
let output_lines = [];
|
|
|
|
let ndiff_output = "";
|
|
|
|
let changes_list = [];
|
|
|
|
|
|
|
|
ndiff_output = difflib.ndiff(string_0.split("\n"), string_1.split("\n"));
|
|
|
|
|
|
|
|
ndiff_output.forEach((line) => {
|
|
|
|
if (line.startsWith("+")) {
|
|
|
|
output_lines.push(line);
|
|
|
|
} else if (line.startsWith("-")) {
|
|
|
|
output_lines.push(line);
|
|
|
|
} else if (line.startsWith("?")) {
|
|
|
|
changes_list = parse_questionmark_line(line);
|
|
|
|
output_lines[output_lines.length - 1] = apply_color(
|
2020-07-15 00:34:28 +02:00
|
|
|
output_lines[output_lines.length - 1],
|
|
|
|
changes_list,
|
|
|
|
);
|
2017-12-04 09:49:22 +01:00
|
|
|
} else {
|
|
|
|
output_lines.push(line);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const emphasize_codes = (string) =>
|
|
|
|
"\u001b[34m" + string.slice(0, 1) + "\u001b[0m" + string.slice(1);
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
|
|
|
output_lines = output_lines.map(emphasize_codes);
|
2017-12-04 09:49:22 +01:00
|
|
|
|
|
|
|
return output_lines.join("\n");
|
|
|
|
}
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
module.exports = {diff_strings};
|
2017-12-04 09:49:22 +01:00
|
|
|
|
|
|
|
// Simple CLI for this module
|
|
|
|
// Only run this code if called as a command-line utility
|
|
|
|
if (require.main === module) {
|
|
|
|
// First two args are just "node" and "mdiff.js"
|
2020-07-15 01:29:15 +02:00
|
|
|
const argv = require("minimist")(process.argv.slice(2));
|
2017-12-04 09:49:22 +01:00
|
|
|
|
2020-02-09 04:29:30 +01:00
|
|
|
if (Object.prototype.hasOwnProperty.call(argv, "help")) {
|
2020-07-15 00:34:28 +02:00
|
|
|
console.log(
|
|
|
|
process.argv[0] +
|
|
|
|
" " +
|
|
|
|
process.argv[1] +
|
|
|
|
" [ --help ]" +
|
|
|
|
" string_0" +
|
|
|
|
" string_1" +
|
|
|
|
"\n" +
|
|
|
|
"Where string_0 and string_1 are the strings to be diffed",
|
2017-12-04 09:49:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = diff_strings(argv._[0], argv._[1]);
|
|
|
|
console.log(output);
|
|
|
|
}
|