check-openapi: Automatically fix Prettier formatting errors.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-09-09 16:06:35 -07:00 committed by Tim Abbott
parent a08c138c03
commit cf2302b272
1 changed files with 37 additions and 16 deletions

View File

@ -8,7 +8,7 @@ const Diff = require("diff");
const ExampleValidator = require("openapi-examples-validator"); const ExampleValidator = require("openapi-examples-validator");
const Prettier = require("prettier"); const Prettier = require("prettier");
const SwaggerParser = require("swagger-parser"); const SwaggerParser = require("swagger-parser");
const {Composer, LineCounter, Parser, Scalar, YAMLMap, YAMLSeq, visit} = require("yaml"); const {Composer, CST, LineCounter, Parser, Scalar, YAMLMap, YAMLSeq, visit} = require("yaml");
const yargs = require("yargs"); const yargs = require("yargs");
const {argv} = yargs.option("fix", { const {argv} = yargs.option("fix", {
@ -39,6 +39,7 @@ async function checkFile(file) {
} }
let ok = true; let ok = true;
const reformats = new Map();
visit(doc, { visit(doc, {
Map(key, node) { Map(key, node) {
@ -74,22 +75,34 @@ async function checkFile(file) {
formatted = formatted.replace(/\n$/, ""); formatted = formatted.replace(/\n$/, "");
} }
if (formatted !== node.value.value) { if (formatted !== node.value.value) {
ok = false; if (argv.fix) {
const {line, col} = lineCounter.linePos(node.value.range[0]); reformats.set(node.value.range[0], {
console.error("%s:%d:%d: Format description with Prettier:", file, line, col); value: formatted,
let diff = ""; context: {afterKey: true},
for (const part of Diff.diffLines(node.value.value, formatted)) { });
const prefix = part.added } else {
? "\u001B[32m+" ok = false;
: part.removed const {line, col} = lineCounter.linePos(node.value.range[0]);
? "\u001B[31m-" console.error(
: "\u001B[34m "; "%s:%d:%d: Format description with Prettier:",
diff += prefix; file,
diff += part.value.replace(/\n$/, "").replace(/\n/g, "\n" + prefix); line,
diff += "\n"; 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);
} }
diff += "\u001B[0m";
console.error(diff);
} }
} }
}, },
@ -100,6 +113,14 @@ async function checkFile(file) {
} }
if (reformats.size > 0) { if (reformats.size > 0) {
console.log("%s: Fixing problems", file); console.log("%s: Fixing problems", file);
for (const token of tokens) {
CST.visit(token, ({value}) => {
if (CST.isScalar(value) && reformats.has(value.offset)) {
const reformat = reformats.get(value.offset);
CST.setScalarValue(value, reformat.value, reformat.context);
}
});
}
await fs.promises.writeFile(file, tokens.map((token) => CST.stringify(token)).join("")); await fs.promises.writeFile(file, tokens.map((token) => CST.stringify(token)).join(""));
} }