Skip to content

Commit

Permalink
Refactor checkPipesOfBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-schwarz committed Jan 9, 2024
1 parent 5c013e6 commit 4f0ebd2
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions libs/language-server/src/lib/validation/checks/block-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,41 @@ function checkPipesOfBlock(
return;
}
const blockType = new BlockTypeWrapper(block?.type);
const pipes = collectPipes(block, whatToCheck);

let pipes: PipeWrapper[];
switch (whatToCheck) {
case 'input': {
pipes = collectIngoingPipes(block);
break;
}
case 'output': {
pipes = collectOutgoingPipes(block);
break;
}
default: {
assertUnreachable(whatToCheck);
const isStartOrEnd =
(whatToCheck === 'input' && !blockType.hasInput()) ||
(whatToCheck === 'output' && !blockType.hasOutput());
if (isStartOrEnd) {
if (pipes.length > 0) {
for (const pipe of pipes) {
context.accept(
'error',
`Blocks of type ${blockType.type} do not have an ${whatToCheck}`,
whatToCheck === 'input'
? pipe.getToDiagnostic()
: pipe.getFromDiagnostic(),
);
}
}
return;
}

if (
(whatToCheck === 'input' && !blockType.hasInput()) ||
(whatToCheck === 'output' && !blockType.hasOutput())
) {
for (const pipe of pipes) {
context.accept(
'error',
`Blocks of type ${blockType.type} do not have an ${whatToCheck}`,
whatToCheck === 'input'
? pipe.getToDiagnostic()
: pipe.getFromDiagnostic(),
);
}
} else if (pipes.length > 1 && whatToCheck === 'input') {
const hasMultipleInputPorts = pipes.length > 1 && whatToCheck === 'input';
if (hasMultipleInputPorts) {
for (const pipe of pipes) {
context.accept(
'error',
`At most one pipe can be connected to the ${whatToCheck} of a ${blockType.type}`,
pipe.getToDiagnostic(),
);
}
} else if (pipes.length === 0) {
return;
}

// above: there should be pipes (defined by blocktype)
// but there aren't any
if (pipes.length === 0) {
const isLastBlockOfCompositeBlocktype =
isCompositeBlocktypeDefinition(block.$container) &&
block.$container.blocks.at(-1)?.name === block.name;
Expand All @@ -84,7 +81,8 @@ function checkPipesOfBlock(
isCompositeBlocktypeDefinition(block.$container) &&
block.$container.blocks.at(0)?.name === block.name;

// The last block in a composite block is connected to the output, not another block
// exception: the first block in a composite block is connected to the input, not another block
// exception: The last block in a composite block is connected to the output, not another block
if (!isLastBlockOfCompositeBlocktype && !isFirstBlockOfCompositeBlocktype) {
context.accept(
'warning',
Expand All @@ -97,3 +95,24 @@ function checkPipesOfBlock(
}
}
}

function collectPipes(
block: BlockDefinition,
whatToCheck: 'input' | 'output',
): PipeWrapper[] {
let pipes: PipeWrapper[];
switch (whatToCheck) {
case 'input': {
pipes = collectIngoingPipes(block);
break;
}
case 'output': {
pipes = collectOutgoingPipes(block);
break;
}
default: {
assertUnreachable(whatToCheck);
}
}
return pipes;
}

0 comments on commit 4f0ebd2

Please sign in to comment.