Skip to content

fix: handleBlockDataChange에 타입 단언 제거하기 #13

Description

@Ospac

⚠️ Potential issue | 🟡 Minor

노드 타입 검증 없이 타입 단언(type assertion)을 사용하고 있습니다.

n.data as BlockNodeData 캐스팅 전에 노드 타입이 "block"인지 확인하지 않습니다. 만약 실수로 "menu" 노드에 대해 호출되면, onDataChange, onEditStart, onEditEnd 프로퍼티가 존재하지 않아 런타임 오류가 발생할 수 있습니다.

🛡️ 타입 가드를 추가하는 수정 제안
 const handleBlockDataChange = (nodeId: string, newData: BlockData) => {
   setNodes((curr) =>
     curr.map((n) => {
       if (n.id !== nodeId) return n;
+      if (n.type !== "block") return n;
       const existing = n.data as BlockNodeData;
       return {
         ...n,
         data: {
           ...newData,
           onDataChange: existing.onDataChange,
           onEditStart: existing.onEditStart,
           onEditEnd: existing.onEditEnd,
         },
       };
     }),
   );
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

	const handleBlockDataChange = (nodeId: string, newData: BlockData) => {
		setNodes((curr) =>
			curr.map((n) => {
				if (n.id !== nodeId) return n;
				if (n.type !== "block") return n;
				const existing = n.data as BlockNodeData;
				return {
					...n,
					data: {
						...newData,
						onDataChange: existing.onDataChange,
						onEditStart: existing.onEditStart,
						onEditEnd: existing.onEditEnd,
					},
				};
			}),
		);
	};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/features/diagram/ui/CanvasCore/CanvasCoreInner.tsx` around lines 122 -
138, handleBlockDataChange uses a type assertion (n.data as BlockNodeData)
without checking the node type, which can cause runtime errors for non-"block"
nodes; update handleBlockDataChange to first check that the node's type is
"block" (e.g., if (n.type !== "block") return n) before accessing
BlockNodeData-specific handlers, then safely merge newData with
existing.onDataChange, existing.onEditStart and existing.onEditEnd; for nodes
that are not "block" simply return them unchanged (or handle error explicitly)
so you never assert into BlockNodeData for other node types.

Originally posted by @coderabbitai[bot] in #12 (comment)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions