chore: Enable "curly" rule to avoid single-statement if confusion/errors.

This commit is contained in:
cpojer
2026-01-31 16:19:20 +09:00
parent 009b16fab8
commit 5ceff756e1
1266 changed files with 27871 additions and 9393 deletions

View File

@@ -18,21 +18,31 @@ const ENVELOPE_CHANNELS = [
const MESSAGE_ID_LINE = /^\s*\[message_id:\s*[^\]]+\]\s*$/i;
function looksLikeEnvelopeHeader(header: string): boolean {
if (/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}Z\b/.test(header)) return true;
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}\b/.test(header)) return true;
if (/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}Z\b/.test(header)) {
return true;
}
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}\b/.test(header)) {
return true;
}
return ENVELOPE_CHANNELS.some((label) => header.startsWith(`${label} `));
}
export function stripEnvelope(text: string): string {
const match = text.match(ENVELOPE_PREFIX);
if (!match) return text;
if (!match) {
return text;
}
const header = match[1] ?? "";
if (!looksLikeEnvelopeHeader(header)) return text;
if (!looksLikeEnvelopeHeader(header)) {
return text;
}
return text.slice(match[0].length);
}
function stripMessageIdHints(text: string): string {
if (!text.includes("[message_id:")) return text;
if (!text.includes("[message_id:")) {
return text;
}
const lines = text.split(/\r?\n/);
const filtered = lines.filter((line) => !MESSAGE_ID_LINE.test(line));
return filtered.length === lines.length ? text : filtered.join("\n");
@@ -41,11 +51,17 @@ function stripMessageIdHints(text: string): string {
function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; changed: boolean } {
let changed = false;
const next = content.map((item) => {
if (!item || typeof item !== "object") return item;
if (!item || typeof item !== "object") {
return item;
}
const entry = item as Record<string, unknown>;
if (entry.type !== "text" || typeof entry.text !== "string") return item;
if (entry.type !== "text" || typeof entry.text !== "string") {
return item;
}
const stripped = stripMessageIdHints(stripEnvelope(entry.text));
if (stripped === entry.text) return item;
if (stripped === entry.text) {
return item;
}
changed = true;
return {
...entry,
@@ -56,10 +72,14 @@ function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; cha
}
export function stripEnvelopeFromMessage(message: unknown): unknown {
if (!message || typeof message !== "object") return message;
if (!message || typeof message !== "object") {
return message;
}
const entry = message as Record<string, unknown>;
const role = typeof entry.role === "string" ? entry.role.toLowerCase() : "";
if (role !== "user") return message;
if (role !== "user") {
return message;
}
let changed = false;
const next: Record<string, unknown> = { ...entry };
@@ -88,11 +108,15 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
}
export function stripEnvelopeFromMessages(messages: unknown[]): unknown[] {
if (messages.length === 0) return messages;
if (messages.length === 0) {
return messages;
}
let changed = false;
const next = messages.map((message) => {
const stripped = stripEnvelopeFromMessage(message);
if (stripped !== message) changed = true;
if (stripped !== message) {
changed = true;
}
return stripped;
});
return changed ? next : messages;