refactor: lint cleanups and helpers

This commit is contained in:
Peter Steinberger
2025-12-23 00:28:40 +00:00
parent f5837dff9c
commit 918cbdcf03
39 changed files with 679 additions and 338 deletions
+55 -14
View File
@@ -1,3 +1,4 @@
import type { BrowserFormField } from "./client-actions-core.js";
import {
type BrowserConsoleMessage,
ensurePageState,
@@ -168,18 +169,29 @@ export async function typeViaPlaywright(opts: {
export async function fillFormViaPlaywright(opts: {
cdpPort: number;
targetId?: string;
fields: Array<Record<string, unknown>>;
fields: BrowserFormField[];
}): Promise<void> {
const page = await getPageForTargetId(opts);
ensurePageState(page);
for (const field of opts.fields) {
const ref = String(field.ref ?? "").trim();
const type = String(field.type ?? "").trim();
const value = String(field.value ?? "");
const ref = field.ref.trim();
const type = field.type.trim();
const rawValue = field.value;
const value =
typeof rawValue === "string"
? rawValue
: typeof rawValue === "number" || typeof rawValue === "boolean"
? String(rawValue)
: "";
if (!ref || !type) continue;
const locator = refLocator(page, ref);
if (type === "checkbox" || type === "radio") {
await locator.setChecked(value === "true");
const checked =
rawValue === true ||
rawValue === 1 ||
rawValue === "1" ||
rawValue === "true";
await locator.setChecked(checked);
continue;
}
await locator.fill(value);
@@ -199,18 +211,47 @@ export async function evaluateViaPlaywright(opts: {
if (opts.ref) {
const locator = refLocator(page, opts.ref);
return await locator.evaluate((el, fnBody) => {
const runner = new Function(
"element",
`"use strict"; const fn = ${fnBody}; return fn(element);`,
) as (element: Element) => unknown;
return runner(el as Element);
const compileRunner = (body: string) => {
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate(element) : candidate;`;
// This intentionally evaluates user-supplied code in the browser context.
// oxlint-disable-next-line typescript-eslint/no-implied-eval
return new Function("element", inner) as (element: Element) => unknown;
};
let compiled: unknown;
try {
compiled = compileRunner(fnBody);
} catch (err) {
const message =
err instanceof Error
? err.message
: typeof err === "string"
? err
: "invalid expression";
throw new Error(`Invalid evaluate function: ${message}`);
}
return (compiled as (element: Element) => unknown)(el as Element);
}, fnText);
}
return await page.evaluate((fnBody) => {
const runner = new Function(
`"use strict"; const fn = ${fnBody}; return fn();`,
) as () => unknown;
return runner();
const compileRunner = (body: string) => {
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate() : candidate;`;
// This intentionally evaluates user-supplied code in the browser context.
// oxlint-disable-next-line typescript-eslint/no-implied-eval
return new Function(inner) as () => unknown;
};
let compiled: unknown;
try {
compiled = compileRunner(fnBody);
} catch (err) {
const message =
err instanceof Error
? err.message
: typeof err === "string"
? err
: "invalid expression";
throw new Error(`Invalid evaluate function: ${message}`);
}
return (compiled as () => unknown)();
}, fnText);
}