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

@@ -53,7 +53,9 @@ export function parseFenceSpans(buffer: string): FenceSpan[] {
}
}
if (nextNewline === -1) break;
if (nextNewline === -1) {
break;
}
offset = nextNewline + 1;
}

View File

@@ -13,9 +13,15 @@ function stripQuotes(value: string): string {
}
function coerceFrontmatterValue(value: unknown): string | undefined {
if (value === null || value === undefined) return undefined;
if (typeof value === "string") return value.trim();
if (typeof value === "number" || typeof value === "boolean") return String(value);
if (value === null || value === undefined) {
return undefined;
}
if (typeof value === "string") {
return value.trim();
}
if (typeof value === "number" || typeof value === "boolean") {
return String(value);
}
if (typeof value === "object") {
try {
return JSON.stringify(value);
@@ -29,13 +35,19 @@ function coerceFrontmatterValue(value: unknown): string | undefined {
function parseYamlFrontmatter(block: string): ParsedFrontmatter | null {
try {
const parsed = YAML.parse(block) as unknown;
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return null;
}
const result: ParsedFrontmatter = {};
for (const [rawKey, value] of Object.entries(parsed as Record<string, unknown>)) {
const key = rawKey.trim();
if (!key) continue;
if (!key) {
continue;
}
const coerced = coerceFrontmatterValue(value);
if (coerced === undefined) continue;
if (coerced === undefined) {
continue;
}
result[key] = coerced;
}
return result;
@@ -50,7 +62,9 @@ function extractMultiLineValue(
): { value: string; linesConsumed: number } {
const startLine = lines[startIndex];
const match = startLine.match(/^([\w-]+):\s*(.*)$/);
if (!match) return { value: "", linesConsumed: 1 };
if (!match) {
return { value: "", linesConsumed: 1 };
}
const inlineValue = match[2].trim();
if (inlineValue) {
@@ -118,14 +132,20 @@ function parseLineFrontmatter(block: string): ParsedFrontmatter {
export function parseFrontmatterBlock(content: string): ParsedFrontmatter {
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
if (!normalized.startsWith("---")) return {};
if (!normalized.startsWith("---")) {
return {};
}
const endIndex = normalized.indexOf("\n---", 3);
if (endIndex === -1) return {};
if (endIndex === -1) {
return {};
}
const block = normalized.slice(4, endIndex);
const lineParsed = parseLineFrontmatter(block);
const yamlParsed = parseYamlFrontmatter(block);
if (yamlParsed === null) return lineParsed;
if (yamlParsed === null) {
return lineParsed;
}
const merged: ParsedFrontmatter = { ...yamlParsed };
for (const [key, value] of Object.entries(lineParsed)) {

View File

@@ -112,10 +112,14 @@ function createMarkdownIt(options: MarkdownParseOptions): MarkdownIt {
}
function getAttr(token: MarkdownToken, name: string): string | null {
if (token.attrGet) return token.attrGet(name);
if (token.attrGet) {
return token.attrGet(name);
}
if (token.attrs) {
for (const [key, value] of token.attrs) {
if (key === name) return value;
if (key === name) {
return value;
}
}
}
return null;
@@ -187,7 +191,9 @@ function resolveRenderTarget(state: RenderState): RenderTarget {
}
function appendText(state: RenderState, value: string) {
if (!value) return;
if (!value) {
return;
}
const target = resolveRenderTarget(state);
target.text += value;
}
@@ -213,15 +219,21 @@ function closeStyle(state: RenderState, style: MarkdownStyle) {
}
function appendParagraphSeparator(state: RenderState) {
if (state.env.listStack.length > 0) return;
if (state.table) return; // Don't add paragraph separators inside tables
if (state.env.listStack.length > 0) {
return;
}
if (state.table) {
return;
} // Don't add paragraph separators inside tables
state.text += "\n\n";
}
function appendListPrefix(state: RenderState) {
const stack = state.env.listStack;
const top = stack[stack.length - 1];
if (!top) return;
if (!top) {
return;
}
top.index += 1;
const indent = " ".repeat(Math.max(0, stack.length - 1));
const prefix = top.type === "ordered" ? `${top.index}. ` : "• ";
@@ -229,7 +241,9 @@ function appendListPrefix(state: RenderState) {
}
function renderInlineCode(state: RenderState, content: string) {
if (!content) return;
if (!content) {
return;
}
const target = resolveRenderTarget(state);
const start = target.text.length;
target.text += content;
@@ -238,7 +252,9 @@ function renderInlineCode(state: RenderState, content: string) {
function renderCodeBlock(state: RenderState, content: string) {
let code = content ?? "";
if (!code.endsWith("\n")) code = `${code}\n`;
if (!code.endsWith("\n")) {
code = `${code}\n`;
}
const target = resolveRenderTarget(state);
const start = target.text.length;
target.text += code;
@@ -251,9 +267,13 @@ function renderCodeBlock(state: RenderState, content: string) {
function handleLinkClose(state: RenderState) {
const target = resolveRenderTarget(state);
const link = target.linkStack.pop();
if (!link?.href) return;
if (!link?.href) {
return;
}
const href = link.href.trim();
if (!href) return;
if (!href) {
return;
}
const start = link.labelStart;
const end = target.text.length;
if (end <= start) {
@@ -286,9 +306,15 @@ function trimCell(cell: TableCell): TableCell {
const text = cell.text;
let start = 0;
let end = text.length;
while (start < end && /\s/.test(text[start] ?? "")) start += 1;
while (end > start && /\s/.test(text[end - 1] ?? "")) end -= 1;
if (start === 0 && end === text.length) return cell;
while (start < end && /\s/.test(text[start] ?? "")) {
start += 1;
}
while (end > start && /\s/.test(text[end - 1] ?? "")) {
end -= 1;
}
if (start === 0 && end === text.length) {
return cell;
}
const trimmedText = text.slice(start, end);
const trimmedLength = trimmedText.length;
const trimmedStyles: MarkdownStyleSpan[] = [];
@@ -311,7 +337,9 @@ function trimCell(cell: TableCell): TableCell {
}
function appendCell(state: RenderState, cell: TableCell) {
if (!cell.text) return;
if (!cell.text) {
return;
}
const start = state.text.length;
state.text += cell.text;
for (const span of cell.styles) {
@@ -331,12 +359,16 @@ function appendCell(state: RenderState, cell: TableCell) {
}
function renderTableAsBullets(state: RenderState) {
if (!state.table) return;
if (!state.table) {
return;
}
const headers = state.table.headers.map(trimCell);
const rows = state.table.rows.map((row) => row.map(trimCell));
// If no headers or rows, skip
if (headers.length === 0 && rows.length === 0) return;
if (headers.length === 0 && rows.length === 0) {
return;
}
// Determine if first column should be used as row labels
// (common pattern: first column is category/feature name)
@@ -345,7 +377,9 @@ function renderTableAsBullets(state: RenderState) {
if (useFirstColAsLabel) {
// Format: each row becomes a section with header as row[0], then key:value pairs
for (const row of rows) {
if (row.length === 0) continue;
if (row.length === 0) {
continue;
}
const rowLabel = row[0];
if (rowLabel?.text) {
@@ -362,7 +396,9 @@ function renderTableAsBullets(state: RenderState) {
for (let i = 1; i < row.length; i++) {
const header = headers[i];
const value = row[i];
if (!value?.text) continue;
if (!value?.text) {
continue;
}
state.text += "• ";
if (header?.text) {
appendCell(state, header);
@@ -381,7 +417,9 @@ function renderTableAsBullets(state: RenderState) {
for (let i = 0; i < row.length; i++) {
const header = headers[i];
const value = row[i];
if (!value?.text) continue;
if (!value?.text) {
continue;
}
state.text += "• ";
if (header?.text) {
appendCell(state, header);
@@ -396,23 +434,31 @@ function renderTableAsBullets(state: RenderState) {
}
function renderTableAsCode(state: RenderState) {
if (!state.table) return;
if (!state.table) {
return;
}
const headers = state.table.headers.map(trimCell);
const rows = state.table.rows.map((row) => row.map(trimCell));
const columnCount = Math.max(headers.length, ...rows.map((row) => row.length));
if (columnCount === 0) return;
if (columnCount === 0) {
return;
}
const widths = Array.from({ length: columnCount }, () => 0);
const updateWidths = (cells: TableCell[]) => {
for (let i = 0; i < columnCount; i += 1) {
const cell = cells[i];
const width = cell?.text.length ?? 0;
if (widths[i] < width) widths[i] = width;
if (widths[i] < width) {
widths[i] = width;
}
}
};
updateWidths(headers);
for (const row of rows) updateWidths(row);
for (const row of rows) {
updateWidths(row);
}
const codeStart = state.text.length;
@@ -421,9 +467,13 @@ function renderTableAsCode(state: RenderState) {
for (let i = 0; i < columnCount; i += 1) {
state.text += " ";
const cell = cells[i];
if (cell) appendCell(state, cell);
if (cell) {
appendCell(state, cell);
}
const pad = widths[i] - (cell?.text.length ?? 0);
if (pad > 0) state.text += " ".repeat(pad);
if (pad > 0) {
state.text += " ".repeat(pad);
}
state.text += " |";
}
state.text += "\n";
@@ -457,7 +507,9 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
for (const token of tokens) {
switch (token.type) {
case "inline":
if (token.children) renderTokens(token.children, state);
if (token.children) {
renderTokens(token.children, state);
}
break;
case "text":
appendText(state, token.content ?? "");
@@ -484,10 +536,14 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
renderInlineCode(state, token.content ?? "");
break;
case "spoiler_open":
if (state.enableSpoilers) openStyle(state, "spoiler");
if (state.enableSpoilers) {
openStyle(state, "spoiler");
}
break;
case "spoiler_close":
if (state.enableSpoilers) closeStyle(state, "spoiler");
if (state.enableSpoilers) {
closeStyle(state, "spoiler");
}
break;
case "link_open": {
const href = getAttr(token, "href") ?? "";
@@ -509,14 +565,20 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
appendParagraphSeparator(state);
break;
case "heading_open":
if (state.headingStyle === "bold") openStyle(state, "bold");
if (state.headingStyle === "bold") {
openStyle(state, "bold");
}
break;
case "heading_close":
if (state.headingStyle === "bold") closeStyle(state, "bold");
if (state.headingStyle === "bold") {
closeStyle(state, "bold");
}
appendParagraphSeparator(state);
break;
case "blockquote_open":
if (state.blockquotePrefix) state.text += state.blockquotePrefix;
if (state.blockquotePrefix) {
state.text += state.blockquotePrefix;
}
break;
case "blockquote_close":
state.text += "\n";
@@ -613,7 +675,9 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
state.text += "\n";
break;
default:
if (token.children) renderTokens(token.children, state);
if (token.children) {
renderTokens(token.children, state);
}
break;
}
}
@@ -639,7 +703,9 @@ function clampStyleSpans(spans: MarkdownStyleSpan[], maxLength: number): Markdow
for (const span of spans) {
const start = Math.max(0, Math.min(span.start, maxLength));
const end = Math.max(start, Math.min(span.end, maxLength));
if (end > start) clamped.push({ start, end, style: span.style });
if (end > start) {
clamped.push({ start, end, style: span.style });
}
}
return clamped;
}
@@ -649,15 +715,21 @@ function clampLinkSpans(spans: MarkdownLinkSpan[], maxLength: number): MarkdownL
for (const span of spans) {
const start = Math.max(0, Math.min(span.start, maxLength));
const end = Math.max(start, Math.min(span.end, maxLength));
if (end > start) clamped.push({ start, end, href: span.href });
if (end > start) {
clamped.push({ start, end, href: span.href });
}
}
return clamped;
}
function mergeStyleSpans(spans: MarkdownStyleSpan[]): MarkdownStyleSpan[] {
const sorted = [...spans].toSorted((a, b) => {
if (a.start !== b.start) return a.start - b.start;
if (a.end !== b.end) return a.end - b.end;
if (a.start !== b.start) {
return a.start - b.start;
}
if (a.end !== b.end) {
return a.end - b.end;
}
return a.style.localeCompare(b.style);
});
@@ -678,7 +750,9 @@ function sliceStyleSpans(
start: number,
end: number,
): MarkdownStyleSpan[] {
if (spans.length === 0) return [];
if (spans.length === 0) {
return [];
}
const sliced: MarkdownStyleSpan[] = [];
for (const span of spans) {
const sliceStart = Math.max(span.start, start);
@@ -695,7 +769,9 @@ function sliceStyleSpans(
}
function sliceLinkSpans(spans: MarkdownLinkSpan[], start: number, end: number): MarkdownLinkSpan[] {
if (spans.length === 0) return [];
if (spans.length === 0) {
return [];
}
const sliced: MarkdownLinkSpan[] = [];
for (const span of spans) {
const sliceStart = Math.max(span.start, start);
@@ -750,8 +826,12 @@ export function markdownToIRWithMeta(
const trimmedLength = trimmedText.length;
let codeBlockEnd = 0;
for (const span of state.styles) {
if (span.style !== "code_block") continue;
if (span.end > codeBlockEnd) codeBlockEnd = span.end;
if (span.style !== "code_block") {
continue;
}
if (span.end > codeBlockEnd) {
codeBlockEnd = span.end;
}
}
const finalLength = Math.max(trimmedLength, codeBlockEnd);
const finalText =
@@ -768,15 +848,21 @@ export function markdownToIRWithMeta(
}
export function chunkMarkdownIR(ir: MarkdownIR, limit: number): MarkdownIR[] {
if (!ir.text) return [];
if (limit <= 0 || ir.text.length <= limit) return [ir];
if (!ir.text) {
return [];
}
if (limit <= 0 || ir.text.length <= limit) {
return [ir];
}
const chunks = chunkText(ir.text, limit);
const results: MarkdownIR[] = [];
let cursor = 0;
chunks.forEach((chunk, index) => {
if (!chunk) return;
if (!chunk) {
return;
}
if (index > 0) {
while (cursor < ir.text.length && /\s/.test(ir.text[cursor] ?? "")) {
cursor += 1;

View File

@@ -35,15 +35,21 @@ const STYLE_RANK = new Map<MarkdownStyle, number>(
function sortStyleSpans(spans: MarkdownStyleSpan[]): MarkdownStyleSpan[] {
return [...spans].toSorted((a, b) => {
if (a.start !== b.start) return a.start - b.start;
if (a.end !== b.end) return b.end - a.end;
if (a.start !== b.start) {
return a.start - b.start;
}
if (a.end !== b.end) {
return b.end - a.end;
}
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
});
}
export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions): string {
const text = ir.text ?? "";
if (!text) return "";
if (!text) {
return "";
}
const styleMarkers = options.styleMarkers;
const styled = sortStyleSpans(ir.styles.filter((span) => Boolean(styleMarkers[span.style])));
@@ -54,16 +60,23 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
const startsAt = new Map<number, MarkdownStyleSpan[]>();
for (const span of styled) {
if (span.start === span.end) continue;
if (span.start === span.end) {
continue;
}
boundaries.add(span.start);
boundaries.add(span.end);
const bucket = startsAt.get(span.start);
if (bucket) bucket.push(span);
else startsAt.set(span.start, [span]);
if (bucket) {
bucket.push(span);
} else {
startsAt.set(span.start, [span]);
}
}
for (const spans of startsAt.values()) {
spans.sort((a, b) => {
if (a.end !== b.end) return b.end - a.end;
if (a.end !== b.end) {
return b.end - a.end;
}
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
});
}
@@ -71,14 +84,21 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
const linkStarts = new Map<number, RenderLink[]>();
if (options.buildLink) {
for (const link of ir.links) {
if (link.start === link.end) continue;
if (link.start === link.end) {
continue;
}
const rendered = options.buildLink(link, text);
if (!rendered) continue;
if (!rendered) {
continue;
}
boundaries.add(rendered.start);
boundaries.add(rendered.end);
const openBucket = linkStarts.get(rendered.start);
if (openBucket) openBucket.push(rendered);
else linkStarts.set(rendered.start, [rendered]);
if (openBucket) {
openBucket.push(rendered);
} else {
linkStarts.set(rendered.start, [rendered]);
}
}
}
@@ -103,7 +123,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
// Close ALL elements (styles and links) in LIFO order at this position
while (stack.length && stack[stack.length - 1]?.end === pos) {
const item = stack.pop();
if (item) out += item.close;
if (item) {
out += item.close;
}
}
const openingItems: OpeningItem[] = [];
@@ -125,7 +147,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
if (openingStyles) {
for (const [index, span] of openingStyles.entries()) {
const marker = styleMarkers[span.style];
if (!marker) continue;
if (!marker) {
continue;
}
openingItems.push({
end: span.end,
open: marker.open,
@@ -139,8 +163,12 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
if (openingItems.length > 0) {
openingItems.sort((a, b) => {
if (a.end !== b.end) return b.end - a.end;
if (a.kind !== b.kind) return a.kind === "link" ? -1 : 1;
if (a.end !== b.end) {
return b.end - a.end;
}
if (a.kind !== b.kind) {
return a.kind === "link" ? -1 : 1;
}
if (a.kind === "style" && b.kind === "style") {
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
}
@@ -155,7 +183,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
}
const next = points[i + 1];
if (next === undefined) break;
if (next === undefined) {
break;
}
if (next > pos) {
out += options.escapeText(text.slice(pos, next));
}

View File

@@ -11,7 +11,9 @@ const MARKDOWN_STYLE_MARKERS = {
} as const;
export function convertMarkdownTables(markdown: string, mode: MarkdownTableMode): string {
if (!markdown || mode === "off") return markdown;
if (!markdown || mode === "off") {
return markdown;
}
const { ir, hasTables } = markdownToIRWithMeta(markdown, {
linkify: false,
autolink: false,
@@ -19,15 +21,21 @@ export function convertMarkdownTables(markdown: string, mode: MarkdownTableMode)
blockquotePrefix: "",
tableMode: mode,
});
if (!hasTables) return markdown;
if (!hasTables) {
return markdown;
}
return renderMarkdownWithMarkers(ir, {
styleMarkers: MARKDOWN_STYLE_MARKERS,
escapeText: (text) => text,
buildLink: (link, text) => {
const href = link.href.trim();
if (!href) return null;
if (!href) {
return null;
}
const label = text.slice(link.start, link.end);
if (!label) return null;
if (!label) {
return null;
}
return { start: link.start, end: link.end, open: "[", close: `](${href})` };
},
});