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 { ScrollManager } from "./scrollManager.ts";
import { colorize } from "./style.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) {
console.log(message);
} else {
if (typeof message === "object") message = Deno.inspect(message);
block.setLines(message.split("\n"));
}
}

View File

@ -19,10 +19,14 @@
"rules": {
"exclude": [
"no-explicit-any"
],
"include": [
"require-await"
]
},
"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 { callWithArgPrompt } from "util/call.ts";
export async function checkFile(pdfPath: string, csPath: string) {
while (!pdfPath || !pdfPath.endsWith(".pdf")) {
pdfPath = prompt("Please provide path to PDF file:") || "";
}
while (!csPath || !csPath.endsWith(".cs")) {
csPath = prompt("Please provide path to CS class file:") || "";
}
const form = await loadPdfForm(pdfPath);
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!");
function getCaseSyntaxPatternByFileExtension(
extenstion: string,
field: string,
) {
switch (extenstion.trim().toLowerCase().replace(".", "")) {
case "cs":
case "js":
case "ts":
default:
return `(?<!//\s?)case ?"${field.replace(/\[\d\]/, `\\[\\?|\\d+\\]`)}"`;
}
}
class CheckCode implements ITool {
name = "checkcode";
description = "Checks if form fields are present in CS code";
help() {
console.log("Usage: checkcode <pdfPath> <csPath>");
description = "Checks if form fields are present in a given code file";
private block?: TerminalBlock;
setBlock(block: TerminalBlock) {
this.block = block;
this.block.setPreserveHistory(true);
}
async run(...args: string[]) {
await callWithArgPrompt(checkFile, [
["Please provide path to PDF file:", (p) => !!p && p.endsWith(".pdf")],
[
"Please provide path to CS file (comma separated for multiple):",
(p) => !!p && p.endsWith(".cs"),
],
], args);
async help() {
cliLog("Usage: checkcode <pdfPath> <csPath>", this.block);
await cliAlert("", this.block);
}
async run(pdfPath: string, codePaths: string) {
[pdfPath, codePaths] = await forceArgs([pdfPath, codePaths], [
"Please provide path to PDF file:",
"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;
}
help(standalone = false) {
cliAlert(
async help(standalone = false) {
await cliAlert(
"Usage: rename-fields <pdfPath> <pattern> <change>\n",
standalone ? undefined : this.block,
);