check-openapi: Print diffs for Prettier formatting errors.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-09-08 16:40:28 -07:00 committed by Tim Abbott
parent b45548e85a
commit 8854cd7945
2 changed files with 16 additions and 1 deletions

View File

@ -99,6 +99,7 @@
"@typescript-eslint/parser": "^4.0.1",
"babel-plugin-rewire-ts": "^1.4.0",
"callsites": "^3.1.0",
"diff": "^4.0.1",
"difflib": "^0.2.4",
"es-check": "^6.0.0",
"eslint": "^7.2.0",

View File

@ -4,6 +4,7 @@
const fs = require("fs");
const Diff = require("diff");
const ExampleValidator = require("openapi-examples-validator");
const Prettier = require("prettier");
const SwaggerParser = require("swagger-parser");
@ -64,7 +65,20 @@ async function checkFile(file) {
if (formatted !== node.value.value) {
ok = false;
const {line, col} = lineCounter.linePos(node.value.range[0]);
console.error("%s:%d:%d: Format description with Prettier", file, line, col);
console.error("%s:%d:%d: Format description with Prettier:", file, line, col);
let diff = "";
for (const part of Diff.diffLines(node.value.value, formatted)) {
const prefix = part.added
? "\u001B[32m+"
: part.removed
? "\u001B[31m-"
: "\u001B[34m ";
diff += prefix;
diff += part.value.replace(/\n$/, "").replace(/\n/g, "\n" + prefix);
diff += "\n";
}
diff += "\u001B[0m";
console.error(diff);
}
}
},