Skip to content

Commit

Permalink
refactor(21481): remove the unnecessary "as" casts
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Apr 29, 2024
1 parent c94bdfc commit cf14897
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export const DataPolicyLoader: FC<PolicyLoaderProps> = ({ policyId }) => {
if (nodeChange.item && !allIds.includes(nodeChange.item.id)) acc.push(nodeChange)
return acc
}, [])
const edgeConnects = allArtefactsLoaded.filter((element) => (element as Connection).source) as Connection[]
const edgeConnects = allArtefactsLoaded.filter(
(element): element is Connection => !!(element as Connection).source
)

store.onNodesChange(nodeChanges)
edgeConnects.map((connection) => store.onConnect(connection))
Expand Down Expand Up @@ -124,7 +126,9 @@ export const BehaviorPolicyLoader: FC<PolicyLoaderProps> = ({ policyId }) => {
if (nodeChange.item && !allIds.includes(nodeChange.item.id)) acc.push(nodeChange)
return acc
}, [])
const edgeConnects = allArtefactsLoaded.filter((element) => (element as Connection).source) as Connection[]
const edgeConnects = allArtefactsLoaded.filter(
(element): element is Connection => !!(element as Connection).source
)

store.onNodesChange(nodeChanges)
edgeConnects.map((connection) => store.onConnect(connection))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function checkValidityBehaviorPolicy(
}
}

export const loadBehaviorPolicy = (behaviorPolicy: BehaviorPolicy) => {
export const loadBehaviorPolicy = (behaviorPolicy: BehaviorPolicy): NodeAddChange => {
const model = enumFromStringValue(BehaviorPolicyType, behaviorPolicy.behavior.id)
if (!model)
throw new Error(
Expand All @@ -69,5 +69,5 @@ export const loadBehaviorPolicy = (behaviorPolicy: BehaviorPolicy) => {
},
}

return { item: behaviorPolicyNode, type: 'add' } as NodeAddChange
return { item: behaviorPolicyNode, type: 'add' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export function checkValidityClients(
}
}

export const loadClientFilter = (behaviorPolicy: BehaviorPolicy, behaviorPolicyNode: Node<BehaviorPolicyData>) => {
export const loadClientFilter = (
behaviorPolicy: BehaviorPolicy,
behaviorPolicyNode: Node<BehaviorPolicyData>
): (NodeAddChange | Connection)[] => {
if (behaviorPolicyNode.id !== behaviorPolicy.id)
throw new Error(
i18n.t('datahub:error.loading.connection.notFound', { type: DataHubNodeType.BEHAVIOR_POLICY }) as string
Expand All @@ -58,7 +61,7 @@ export const loadClientFilter = (behaviorPolicy: BehaviorPolicy, behaviorPolicyN
}

return [
{ item: topicNode, type: 'add' } as NodeAddChange,
{ source: topicNode.id, target: behaviorPolicyNode.id, sourceHandle: null, targetHandle: null } as Connection,
{ item: topicNode, type: 'add' },
{ source: topicNode.id, target: behaviorPolicyNode.id, sourceHandle: null, targetHandle: null },
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const checkValidityDataPolicy = (
}
}

export const loadDataPolicy = (policy: DataPolicy) => {
export const loadDataPolicy = (policy: DataPolicy): NodeAddChange => {
const position: XYPosition = {
x: 0,
y: 0,
Expand All @@ -128,5 +128,5 @@ export const loadDataPolicy = (policy: DataPolicy) => {
data: {},
}

return { item: dataPolicyNode, type: 'add' } as NodeAddChange
return { item: dataPolicyNode, type: 'add' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,7 @@ export const loadPipeline = (

if (!operationNode) throw new Error(i18n.t('datahub:error.loading.operation.unknown') as string)
if (!Array.isArray(operationNode)) {
newNodes.push(
{ item: operationNode, type: 'add' } as NodeAddChange,
{ ...connect, target: operationNode.id } as Connection
)
newNodes.push({ item: operationNode, type: 'add' }, { ...connect, target: operationNode.id })
connect = {
source: operationNode.id,
target: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function loadSchema(
positionDeltaX: number,
schemaRef: SchemaReference,
schemas: Schema[]
) {
): (NodeAddChange | Connection)[] {
const schema = schemas.find((schema) => schema.id === schemaRef.schemaId)
if (!schema)
throw new Error(i18n.t('datahub:error.loading.connection.notFound', { source: DataHubNodeType.SCHEMA }) as string)
Expand All @@ -148,13 +148,13 @@ export function loadSchema(
}

return [
{ item: schemaNode, type: 'add' } as NodeAddChange,
{ item: schemaNode, type: 'add' },
{
source: schemaNode.id,
target: parentNode.id,
sourceHandle: null,
targetHandle: targetHandle,
} as Connection,
},
]
}

Expand All @@ -181,13 +181,13 @@ export function loadSchema(
}

return [
{ item: schemaNode, type: 'add' } as NodeAddChange,
{ item: schemaNode, type: 'add' },
{
source: schemaNode.id,
target: parentNode.id,
sourceHandle: null,
targetHandle: targetHandle,
} as Connection,
},
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export const loadScripts = (parentNode: Node<DataHubNodeData>, functions: Policy
}

newNodes.push(
{ item: functionScriptNode, type: 'add' } as NodeAddChange,
{ item: functionScriptNode, type: 'add' },
{
source: functionScriptNode.id,
target: parentNode.id,
sourceHandle: null,
targetHandle: OperationData.Handle.FUNCTION,
} as Connection
}
)
}
return newNodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { getNodeId } from '@datahub/utils/node.utils.ts'
import { DataHubNodeType, DataPolicyData, TopicFilterData } from '@datahub/types.ts'
import { CANVAS_POSITION } from '@datahub/designer/checks.utils.ts'

export const loadTopicFilter = (policy: DataPolicy, dataPolicyNode: Node<DataPolicyData>) => {
export const loadTopicFilter = (
policy: DataPolicy,
dataPolicyNode: Node<DataPolicyData>
): (NodeAddChange | Connection)[] => {
if (dataPolicyNode.id !== policy.id)
throw new Error(
i18n.t('datahub:error.loading.connection.notFound', { type: DataHubNodeType.DATA_POLICY }) as string
Expand All @@ -30,7 +33,7 @@ export const loadTopicFilter = (policy: DataPolicy, dataPolicyNode: Node<DataPol
}

return [
{ item: topicNode, type: 'add' } as NodeAddChange,
{ source: topicNode.id, target: dataPolicyNode.id, sourceHandle: null, targetHandle: null } as Connection,
{ item: topicNode, type: 'add' },
{ source: topicNode.id, target: dataPolicyNode.id, sourceHandle: null, targetHandle: null },
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ export const loadTransitions = (
const pipelines = loadBehaviorPolicyPipelines(behaviorPolicyTransition, transitionNode, schemas, scripts)

newNodes.push(
{ item: transitionNode, type: 'add' } as NodeAddChange,
{ item: transitionNode, type: 'add' },
{
source: behaviorPolicy.id,
target: transitionNode.id,
sourceHandle: null,
targetHandle: null,
} as Connection,
},
...pipelines
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ export const loadValidators = (policy: DataPolicy, schemas: Schema[], dataPolicy
},
}

newNodes.push({ item: validatorNode, type: 'add' } as NodeAddChange)
newNodes.push({ item: validatorNode, type: 'add' })
newNodes.push({
source: validatorNode.id,
target: dataPolicyNode.id,
sourceHandle: null,
targetHandle: DataPolicyData.Handle.VALIDATION,
} as Connection)
})

for (const schemaRef of validatorArguments.schemas) {
const schemaNodes = loadSchema(validatorNode, null, 0, schemaRef, schemas)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import { NodeAddChange, EdgeAddChange, Node, Edge, NodeProps } from 'reactflow'
import { EdgeAddChange, Node, Edge, NodeProps } from 'reactflow'

import { DataHubNodeType, DataPolicyData, FunctionSpecs, WorkspaceAction, WorkspaceState } from '../types.ts'
import useDataHubDraftStore from '@/extensions/datahub/hooks/useDataHubDraftStore.ts'
Expand Down Expand Up @@ -36,8 +36,8 @@ describe('useDataHubDraftStore', () => {

act(() => {
const { onNodesChange } = result.current
const item: Partial<Node> = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onNodesChange([{ item, type: 'add' } as NodeAddChange])
const item: Node = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onNodesChange([{ item, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(1)
Expand Down Expand Up @@ -66,9 +66,12 @@ describe('useDataHubDraftStore', () => {

act(() => {
const { onNodesChange } = result.current
const item1: Partial<Node> = { ...MOCK_NODE, id: '1', position: { x: 0, y: 0 } }
const item2: Partial<Node> = { ...MOCK_NODE, id: '2', position: { x: 0, y: 0 } }
onNodesChange([{ item: item1, type: 'add' } as NodeAddChange, { item: item2, type: 'add' } as NodeAddChange])
const item1: Node = { ...MOCK_NODE, id: '1', position: { x: 0, y: 0 } }
const item2: Node = { ...MOCK_NODE, id: '2', position: { x: 0, y: 0 } }
onNodesChange([
{ item: item1, type: 'add' },
{ item: item2, type: 'add' },
])
})

expect(result.current.nodes).toHaveLength(2)
Expand All @@ -90,26 +93,26 @@ describe('useDataHubDraftStore', () => {

act(() => {
const { onAddNodes } = result.current
const item1: Partial<Node> = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' } as NodeAddChange])
const item1: Node = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(1)
expect(result.current.edges).toHaveLength(0)

act(() => {
const { onAddNodes } = result.current
const item1: Partial<Node> = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' } as NodeAddChange])
const item1: Node = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(1)
expect(result.current.edges).toHaveLength(0)

act(() => {
const { onAddNodes } = result.current
const item1: Partial<Node> = { ...MOCK_NODE, id: '2', position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' } as NodeAddChange])
const item1: Node = { ...MOCK_NODE, id: '2', position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(2)
Expand Down Expand Up @@ -156,8 +159,8 @@ describe('useDataHubDraftStore', () => {

act(() => {
const { onAddNodes } = result.current
const item1: Partial<Node> = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' } as NodeAddChange])
const item1: Node = { ...MOCK_NODE, position: { x: 0, y: 0 } }
onAddNodes([{ item: item1, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import { NodeAddChange, EdgeAddChange, Node, Edge, Rect } from 'reactflow'
import { EdgeAddChange, Node, Edge, Rect } from 'reactflow'

import { Group, IdStubs, NodeTypes, WorkspaceAction, WorkspaceState } from '../types.ts'
import useWorkspaceStore from './useWorkspaceStore.ts'
Expand Down Expand Up @@ -29,8 +29,8 @@ describe('useWorkspaceStore', () => {

act(() => {
const { onNodesChange } = result.current
const item: Partial<Node> = { ...MOCK_NODE_ADAPTER, position: { x: 0, y: 0 } }
onNodesChange([{ item, type: 'add' } as NodeAddChange])
const item: Node = { ...MOCK_NODE_ADAPTER, position: { x: 0, y: 0 } }
onNodesChange([{ item, type: 'add' }])
})

expect(result.current.nodes).toHaveLength(1)
Expand Down Expand Up @@ -59,18 +59,18 @@ describe('useWorkspaceStore', () => {

act(() => {
const { onNodesChange, onEdgesChange } = result.current
const nodeEdge: Partial<Node> = { ...MOCK_NODE_EDGE, id: IdStubs.EDGE_NODE, position: { x: 0, y: 0 } }
const item1: Partial<Node> = { ...MOCK_NODE_ADAPTER, position: { x: 0, y: 0 } }
const item2: Partial<Node> = { ...MOCK_NODE_BRIDGE, position: { x: 0, y: 0 } }
const nodeEdge: Node = { ...MOCK_NODE_EDGE, id: IdStubs.EDGE_NODE, position: { x: 0, y: 0 } }
const item1: Node = { ...MOCK_NODE_ADAPTER, position: { x: 0, y: 0 } }
const item2: Node = { ...MOCK_NODE_BRIDGE, position: { x: 0, y: 0 } }

const item: Partial<Edge> = { id: '1-2', source: 'idAdapter', target: IdStubs.EDGE_NODE }
const item: Edge = { id: '1-2', source: 'idAdapter', target: IdStubs.EDGE_NODE }

onNodesChange([
{ item: nodeEdge, type: 'add' } as NodeAddChange,
{ item: item1, type: 'add' } as NodeAddChange,
{ item: item2, type: 'add' } as NodeAddChange,
{ item: nodeEdge, type: 'add' },
{ item: item1, type: 'add' },
{ item: item2, type: 'add' },
])
onEdgesChange([{ item, type: 'add' } as EdgeAddChange])
onEdgesChange([{ item, type: 'add' }])
})
expect(result.current.nodes).toHaveLength(3)
expect(result.current.edges).toHaveLength(1)
Expand Down

0 comments on commit cf14897

Please sign in to comment.