updates checkCode to new framework

This commit is contained in:
Emmaline Autumn 2025-05-02 01:11:15 -06:00
parent 26b7089cc2
commit 03a1e3ed21
6 changed files with 128 additions and 51 deletions

View File

@ -1,5 +1,5 @@
// deno-lint-disable-must-await-calls
import { Cursor } from "./cursor.ts"; import { Cursor } from "./cursor.ts";
import { ScrollManager } from "./scrollManager.ts";
import { colorize } from "./style.ts"; import { colorize } from "./style.ts";
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts"; import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
@ -75,10 +75,14 @@ export function cliAlert(message: string, block?: TerminalBlock) {
}); });
} }
export function cliLog(message: string, block?: TerminalBlock) { export function cliLog(
message: string | object | Array<unknown>,
block?: TerminalBlock,
) {
if (!block) { if (!block) {
console.log(message); console.log(message);
} else { } else {
if (typeof message === "object") message = Deno.inspect(message);
block.setLines(message.split("\n")); block.setLines(message.split("\n"));
} }
} }

View File

@ -19,10 +19,14 @@
"rules": { "rules": {
"exclude": [ "exclude": [
"no-explicit-any" "no-explicit-any"
],
"include": [
"require-await"
] ]
}, },
"plugins": [ "plugins": [
"./no-log.ts" "./no-log.ts",
"./must_await_cli_prompts.ts"
] ]
} }
} }

45
must_await_cli_prompts.ts Normal file
View File

@ -0,0 +1,45 @@
const TARGET_FUNCTIONS = new Set(["cliAlert", "cliPrompt", "cliConfirm"]);
const plugin: Deno.lint.Plugin = {
name: "must-await-calls",
rules: {
"must-await-calls": {
create(context) {
return {
CallExpression(node) {
if (
node.callee.type !== "Identifier" ||
!TARGET_FUNCTIONS.has(node.callee.name)
) return;
const parent = node.parent;
// Allow `await fetchData()`
if (parent?.type === "AwaitExpression") return;
// Allow `return fetchData()` or `return await fetchData()`
if (parent?.type === "ReturnStatement") return;
// Allow `fetchData().then(...)`
if (
parent?.type === "MemberExpression" &&
parent.property.type === "Identifier" &&
parent.property.name === "then"
) return;
context.report({
node,
message:
`Call to "${node.callee.name}" must be awaited, returned, or .then-chained.`,
fix(fixer) {
return fixer.insertTextBefore(node, "await ");
},
});
},
};
},
},
},
};
export default plugin;

View File

@ -0,0 +1,7 @@
const thing: string = "";
switch (thing) {
case "Text1":
break;
default:
break;
}

View File

@ -1,57 +1,74 @@
import { forceArgs } from "../cli/forceArgs.ts";
import { cliAlert, cliLog } from "../cli/prompts.ts";
import { colorize } from "../cli/style.ts";
import type { TerminalBlock } from "../cli/TerminalLayout.ts";
import { loadPdfForm } from "../util/saveLoadPdf.ts"; import { loadPdfForm } from "../util/saveLoadPdf.ts";
import { callWithArgPrompt } from "util/call.ts";
export async function checkFile(pdfPath: string, csPath: string) { function getCaseSyntaxPatternByFileExtension(
while (!pdfPath || !pdfPath.endsWith(".pdf")) { extenstion: string,
pdfPath = prompt("Please provide path to PDF file:") || ""; field: string,
} ) {
while (!csPath || !csPath.endsWith(".cs")) { switch (extenstion.trim().toLowerCase().replace(".", "")) {
csPath = prompt("Please provide path to CS class file:") || ""; case "cs":
} case "js":
case "ts":
const form = await loadPdfForm(pdfPath); default:
return `(?<!//\s?)case ?"${field.replace(/\[\d\]/, `\\[\\?|\\d+\\]`)}"`;
const fields = form.getFields();
const csFiles = await Promise.all(
csPath.split(",").map((c) => Deno.readTextFile(c.trim())),
);
const fieldNames: string[] = fields.map((f) => f.getName())
.filter((f) => {
const rx = new RegExp(
`(?<!//\s?)case ?"${f.replace(/\[\d\]/, "\\[\\?\\]")}"`,
);
return !csFiles.some((c) => rx.test(c));
})
.filter((f) => !f.toLowerCase().includes("signature"));
if (fieldNames.length) {
console.log(
"%cThe following field names are not present in the CS code",
"color: red",
);
console.log(fieldNames);
alert("Your princess is in another castle...");
} else {
console.log("%cAll form fields present", "color: lime");
alert("Ok!");
} }
} }
class CheckCode implements ITool { class CheckCode implements ITool {
name = "checkcode"; name = "checkcode";
description = "Checks if form fields are present in CS code"; description = "Checks if form fields are present in a given code file";
help() { private block?: TerminalBlock;
console.log("Usage: checkcode <pdfPath> <csPath>"); setBlock(block: TerminalBlock) {
this.block = block;
this.block.setPreserveHistory(true);
} }
async run(...args: string[]) { async help() {
await callWithArgPrompt(checkFile, [ cliLog("Usage: checkcode <pdfPath> <csPath>", this.block);
["Please provide path to PDF file:", (p) => !!p && p.endsWith(".pdf")], await cliAlert("", this.block);
[ }
"Please provide path to CS file (comma separated for multiple):", async run(pdfPath: string, codePaths: string) {
(p) => !!p && p.endsWith(".cs"), [pdfPath, codePaths] = await forceArgs([pdfPath, codePaths], [
], "Please provide path to PDF file:",
], args); "Please provide path(s) to code file(s) (comma separated for multiple):",
]);
const form = await loadPdfForm(pdfPath);
const fields = form.getFields();
const codeFiles: [string, string][] = codePaths.split(",").map((
c,
) => [c, Deno.readTextFileSync(c.trim())]);
const fieldNames: string[] = fields.map((f) => f.getName())
.filter((f) => !f.toLowerCase().includes("signature"));
let unfound = fieldNames.slice();
for (const [path, content] of codeFiles) {
unfound = unfound.filter((f) => {
const rx = new RegExp(
getCaseSyntaxPatternByFileExtension(path.split(".").at(-1) ?? "", f),
);
return rx.test(content);
});
}
if (unfound.length) {
cliLog(
colorize(
"The following field names are not present in the CS code",
"red",
),
this.block,
);
cliLog(unfound, this.block);
await cliAlert("Your princess is in another castle...", this.block);
} else {
cliLog(colorize("All form fields present", "green"), this.block);
await cliAlert("Ok!", this.block);
}
} }
} }

View File

@ -87,8 +87,8 @@ class RenameFields implements ITool {
this.block = block; this.block = block;
} }
help(standalone = false) { async help(standalone = false) {
cliAlert( await cliAlert(
"Usage: rename-fields <pdfPath> <pattern> <change>\n", "Usage: rename-fields <pdfPath> <pattern> <change>\n",
standalone ? undefined : this.block, standalone ? undefined : this.block,
); );