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 Prettier = require("prettier");
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 {argv} = yargs.option("fix", {
@ -39,6 +39,7 @@ async function checkFile(file) {
}
let ok = true;
const reformats = new Map();
visit(doc, {
Map(key, node) {
@ -74,9 +75,20 @@ async function checkFile(file) {
formatted = formatted.replace(/\n$/, "");
}
if (formatted !== node.value.value) {
if (argv.fix) {
reformats.set(node.value.range[0], {
value: formatted,
context: {afterKey: true},
});
} else {
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
@ -92,6 +104,7 @@ async function checkFile(file) {
console.error(diff);
}
}
}
},
});
@ -100,6 +113,14 @@ async function checkFile(file) {
}
if (reformats.size > 0) {
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(""));
}