From 69e67a764d5f5d1ed7f5f12ae98b6ed0bf62f166 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 27 Mar 2026 18:00:06 +0000 Subject: [PATCH] refactor(feishu): remove docx table lint suppressions --- extensions/feishu/src/docx-table-ops.test.ts | 53 ++++++++++++++++++++ extensions/feishu/src/docx-table-ops.ts | 37 ++++++++------ 2 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 extensions/feishu/src/docx-table-ops.test.ts diff --git a/extensions/feishu/src/docx-table-ops.test.ts b/extensions/feishu/src/docx-table-ops.test.ts new file mode 100644 index 00000000000..1b1432bef74 --- /dev/null +++ b/extensions/feishu/src/docx-table-ops.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { cleanBlocksForDescendant } from "./docx-table-ops.js"; + +describe("cleanBlocksForDescendant", () => { + it("removes parent links and read-only table fields while normalizing table cells", () => { + const blocks = [ + { + block_id: "table-1", + parent_id: "parent-1", + block_type: 31, + children: "cell-1", + table: { + property: { + row_size: 1, + column_size: 1, + column_width: [240], + }, + cells: ["cell-1"], + merge_info: [{ row_span: 1, col_span: 1 }], + }, + }, + { + block_id: "cell-1", + parent_id: "table-1", + block_type: 32, + children: "text-1", + }, + { + block_id: "text-1", + parent_id: "cell-1", + block_type: 2, + text: { + elements: [{ text_run: { content: "hello" } }], + }, + }, + ]; + + const cleaned = cleanBlocksForDescendant(blocks); + + expect(cleaned[0]).not.toHaveProperty("parent_id"); + expect(cleaned[1]).not.toHaveProperty("parent_id"); + expect(cleaned[2]).not.toHaveProperty("parent_id"); + + expect(cleaned[0]?.table).toEqual({ + property: { + row_size: 1, + column_size: 1, + column_width: [240], + }, + }); + expect(cleaned[1]?.children).toEqual(["text-1"]); + }); +}); diff --git a/extensions/feishu/src/docx-table-ops.ts b/extensions/feishu/src/docx-table-ops.ts index 361917c6ced..b2464cd448f 100644 --- a/extensions/feishu/src/docx-table-ops.ts +++ b/extensions/feishu/src/docx-table-ops.ts @@ -8,7 +8,7 @@ */ import type * as Lark from "@larksuiteoapi/node-sdk"; -import type { FeishuDocxBlock } from "./docx-types.js"; +import type { FeishuBlockTable, FeishuDocxBlock } from "./docx-types.js"; // ============ Table Utilities ============ @@ -41,6 +41,26 @@ function normalizeChildBlockIds(children: string[] | string | undefined): string return typeof children === "string" ? [children] : []; } +function omitParentId(block: FeishuDocxBlock): FeishuDocxBlock { + const cleanBlock = { ...block }; + delete cleanBlock.parent_id; + return cleanBlock; +} + +function createDescendantTable( + table: FeishuBlockTable, + adaptiveWidths: number[] | undefined, +): FeishuBlockTable { + const { row_size, column_size } = table.property || {}; + return { + property: { + row_size, + column_size, + ...(adaptiveWidths?.length ? { column_width: adaptiveWidths } : {}), + }, + }; +} + export function calculateAdaptiveColumnWidths( blocks: FeishuDocxBlock[], tableBlockId: string, @@ -178,8 +198,7 @@ export function cleanBlocksForDescendant(blocks: FeishuDocxBlock[]): FeishuDocxB } return blocks.map((block) => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { parent_id: _parentId, ...cleanBlock } = block; + const cleanBlock = omitParentId(block); // Fix: Convert API sometimes returns children as string for TableCell if (cleanBlock.block_type === 32 && typeof cleanBlock.children === "string") { @@ -188,18 +207,8 @@ export function cleanBlocksForDescendant(blocks: FeishuDocxBlock[]): FeishuDocxB // Clean table blocks if (cleanBlock.block_type === 31 && cleanBlock.table) { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { cells: _cells, ...tableWithoutCells } = cleanBlock.table; - const { row_size, column_size } = tableWithoutCells.property || {}; const adaptiveWidths = block.block_id ? tableWidths.get(block.block_id) : undefined; - - cleanBlock.table = { - property: { - row_size, - column_size, - ...(adaptiveWidths?.length && { column_width: adaptiveWidths }), - }, - }; + cleanBlock.table = createDescendantTable(cleanBlock.table, adaptiveWidths); } return cleanBlock;