File

packages/ast-to-mmd/src/app/graph-blocks/try-catch-declaration.graph-block.ts

Extends

BlockDeclarationGraphBlock

Index

Properties
Methods
Accessors

Constructor

constructor(id: string, tryBlock?: GraphBlock[], catchBlock?: GraphBlock[], finallyBlock?: GraphBlock[], catchBlockId?: string, finallyBlockId?: string)
Parameters :
Name Type Optional
id string No
tryBlock GraphBlock[] Yes
catchBlock GraphBlock[] Yes
finallyBlock GraphBlock[] Yes
catchBlockId string Yes
finallyBlockId string Yes

Properties

Private Static CATCH_COUNTER
Type : number
Default value : 0
Private catchId
Default value : TryCatchDeclarationGraphBlock.CATCH_COUNTER++
Private Static FINALLY_COUNTER
Type : number
Default value : 0
Private finallyId
Default value : TryCatchDeclarationGraphBlock.FINALLY_COUNTER++
Private Static TRY_CATCH_COUNTER
Type : number
Default value : 0
Private Static TRY_COUNTER
Type : number
Default value : 0
Private tryCatchId
Default value : TryCatchDeclarationGraphBlock.TRY_CATCH_COUNTER++
Private tryId
Default value : TryCatchDeclarationGraphBlock.TRY_COUNTER++
Private _lazyDependencies
Type : string[]
Default value : []
Inherited from GraphBlock
Defined in GraphBlock:30

Lazy dependencies ready to render when this node is rendered.

Private _parent
Type : GraphBlock | undefined
Default value : undefined
Inherited from GraphBlock
Defined in GraphBlock:25

Parent of current node in graph. Can be undefined.

Private _parentSiblingCondition
Type : SiblingCondition
Inherited from GraphBlock
Defined in GraphBlock:39

SiblingCondition for finding correct parent.

Private _siblingCondition
Type : SiblingCondition
Inherited from GraphBlock
Defined in GraphBlock:35
Private Static Readonly EMPTY_LINE_RENDERER_MODIFIER
Type : LineRendererModifier
Default value : () => {...}
Inherited from GraphBlock
Defined in GraphBlock:20

Empty implementation of LineRendererModifier.

Public Readonly id
Type : string
Inherited from GraphBlock
Defined in GraphBlock:46
Unique ID in the node graph.
Private Static Readonly POSITIVE_SIBLING_CONDITION
Type : SiblingCondition
Default value : new PositiveSiblingCondition()
Inherited from GraphBlock
Defined in GraphBlock:16

Positive sibling condition default for all GraphBlock.

Private Static Readonly SHAPE_RENDERER
Type : ShapeRenderer
Default value : new ShapeRenderer()
Inherited from GraphBlock
Defined in GraphBlock:12

Shape renderer for generating different shapes in graph.

Methods

Public render
render(_indent: number)
Inherited from GraphBlock
Defined in GraphBlock:29
Parameters :
Name Type Optional
_indent number No
Returns : string
Protected renderCatchBody
renderCatchBody(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected renderDependencies
renderDependencies(_indent: number)

Render dependencies between internal blocks: TRY - CATCH - FINALLY

Parameters :
Name Type Optional
_indent number No
Returns : string
Protected renderFinallyBody
renderFinallyBody(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected renderOuterBody
renderOuterBody(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected renderTryBody
renderTryBody(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected createLineBuilder
createLineBuilder(lhsId: string, rhsId: string)
Inherited from GraphBlock
Defined in GraphBlock:71
Parameters :
Name Type Optional
lhsId string No
rhsId string No
Returns : LineRenderer
Protected renderChildren
renderChildren(indent: number, children: GraphBlock[])
Parameters :
Name Type Optional
indent number No
children GraphBlock[] No
Returns : string
Private __multiFindChildIndex
__multiFindChildIndex(children: GraphBlock[][], child: GraphBlock)
Inherited from GraphBlock
Defined in GraphBlock:575

Private method for finding a child index of defined block in a multidimensional array of children.

Parameters :
Name Type Optional Description
children GraphBlock[][] No

[] Multidimensional array of children including the child.

child GraphBlock No

GraphBlock Wanted child.

Returns : | undefined

Coordinates of child block or undefined when block is not found.

Private __renderLines
__renderLines(indent: number, id: string, ids: string[], direction: "left" | "right", builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:546

Private method for rendering lines between nodes in defined directions.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

id string No

ID of one side of connection.

ids string[] No

Array of IDs of end of a connection.

direction "left" | "right" No

Direction of a line.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _assignParent
_assignParent(children: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:289

Helper method for assigning this parent into each children.

Parameters :
Name Type Optional Description
children GraphBlock[] No

[]GraphBlock Children where this parent is going to be assigned.

Returns : void
Protected _filterChildren
_filterChildren(children: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:466

Helper method for filtering array of children.

Parameters :
Name Type Optional Description
children GraphBlock[] No

[]GraphBlock Array of children.

Returns : GraphBlock[]
Protected _findParentBySiblingCondition
_findParentBySiblingCondition(child: GraphBlock, siblingCondition: SiblingCondition)
Inherited from GraphBlock
Defined in GraphBlock:521

Helper method for finding an indirect parent of defined child by SiblingCondition.

Parameters :
Name Type Optional Description
child GraphBlock No

GraphBlock Child whose parent is wanted.

siblingCondition SiblingCondition No

SiblingCondition.

Protected _findSiblingChild
_findSiblingChild(child: GraphBlock, parentSiblingCondition: SiblingCondition, siblingCondition: SiblingCondition)
Inherited from GraphBlock
Defined in GraphBlock:478

Helper method for finding a sibling of defined child in parents children.

Parameters :
Name Type Optional Default value Description
child GraphBlock No

GraphBlock Node which for sibling is looked for.

parentSiblingCondition SiblingCondition No this.siblingCondition

SiblingCondition.

siblingCondition SiblingCondition No this.siblingCondition

SiblingCondition.

Protected _generateSpace
_generateSpace(count: number)
Inherited from GraphBlock
Defined in GraphBlock:301

Helper method for generating space.

Parameters :
Name Type Optional Description
count number No

Count of space.

Returns : string

Space of defined length.

Protected _multiFilterChildren
_multiFilterChildren(children: GraphBlock[][])
Inherited from GraphBlock
Defined in GraphBlock:456

Helper method for filtering children in multidimensional array of children.

Parameters :
Name Type Optional Description
children GraphBlock[][] No

[] Array of children.

Returns : [][]
Protected _renderConnectionWithParent
_renderConnectionWithParent(indent: number, children: GraphBlock, lastIds: string[], priorityBuilderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:410
Parameters :
Name Type Optional
indent number No
children GraphBlock No
lastIds string[] No
priorityBuilderModifier LineRendererModifier[] No
Returns : string
Protected _renderDependencies
_renderDependencies(indent: number, blocks: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:346

Helper method for rendering dependencies with defined indent between blocks.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

blocks GraphBlock[] No

[]GraphBlock Blocks to render dependencies between each node.

Returns : string

Dependencies of blocks.

Protected _renderLine
_renderLine(indent: number, lhsId: string, rhsId: string, builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:314

Helper method for generating line between two nodes.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

lhsId string No

ID of start node.

rhsId string No

ID of end node.

builderModifier LineRendererModifier[] No
Returns : string

Line definition between two nodes.

Protected _renderLinesL2R
_renderLinesL2R(indent: number, toId: string, fromIds: string[], builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:383

Helper method for rendering line starting in one node and ending in multiple nodes.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

toId string No

ID of start node.

fromIds string[] No

Array of end nodes.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _renderLinesR2L
_renderLinesR2L(indent: number, fromId: string, toIds: string[], builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:401

Helper method for rendering line starting in multiple nodes and ending in one node.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

fromId string No

Array of start nodes.

toIds string[] No

ID of end node.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _renderShape
_renderShape(text: string, shape: Shape)
Inherited from GraphBlock
Defined in GraphBlock:335

Helper method for rendering defined shape with text.

Parameters :
Name Type Optional Description
text string No

Text in shape.

shape Shape No

Shape Type of shape.

Returns : string

Rendered shape with text.

Protected renderInSubgraph
renderInSubgraph(_indent: number, id: string, text: string, bodyRenderer: (indent: number) => void, direction: "TB" | "TD" | "BT" | "LR" | "RL")
Inherited from GraphBlock
Defined in GraphBlock:86

Renders current node wrapped in a subgraph.

Direction options:

  • TB - top to bottom (default)
  • TD - top-down / same as top to bottom
  • BT - bottom to top
  • LR - left to rignt
  • RL - right to left
Parameters :
Name Type Optional Default value Description
_indent number No

Indentation from start of the line.

id string No

ID of a subgraph.

text string No

Name of a subgraph.

bodyRenderer function No

Callback responsible for rendering real node content.

direction "TB" | "TD" | "BT" | "LR" | "RL" No 'TB'

Direction of a subgraph.

Returns : string

Graph wrapped in a subgraph.

Public renderLazyDependencies
renderLazyDependencies(_indent: number)
Inherited from GraphBlock
Defined in GraphBlock:63

Renders lazy dependencies of current node.

Parameters :
Name Type Optional Description
_indent number No

Indentiation from start of the line.

Returns : string
Public toString
toString()
Inherited from GraphBlock
Defined in GraphBlock:589
Returns : string

Accessors

blockKind
getblockKind()
children
getchildren()
lastBlocks
getlastBlocks()

Try-catch block

Example of full try-catch block with finally section

Try catch block

Example of full try-catch block without finally section

Try catch block

import { BlockKind } from '../block.kind';
import { BlockDeclarationGraphBlock } from './block-declaration.graph-block';
import { GraphBlock } from './graph-block';

export class TryCatchDeclarationGraphBlock extends BlockDeclarationGraphBlock {
  private static TRY_CATCH_COUNTER = 0;
  private static TRY_COUNTER = 0;
  private static CATCH_COUNTER = 0;
  private static FINALLY_COUNTER = 0;

  private tryCatchId = TryCatchDeclarationGraphBlock.TRY_CATCH_COUNTER++;
  private tryId = TryCatchDeclarationGraphBlock.TRY_COUNTER++;
  private catchId = TryCatchDeclarationGraphBlock.CATCH_COUNTER++;
  private finallyId = TryCatchDeclarationGraphBlock.FINALLY_COUNTER++;

  constructor(
    id: string,
    private readonly tryBlock?: GraphBlock[],
    private readonly catchBlock?: GraphBlock[],
    private readonly finallyBlock?: GraphBlock[],
    private readonly catchBlockId?: string,
    private readonly finallyBlockId?: string
  ) {
    super(id, tryBlock || []);
    this._assignParent(catchBlock || []);
    this._assignParent(finallyBlock || []);
  }

  public override render(_indent: number): string {
    return this.renderInSubgraph(_indent, this.id, `TRY-CATCH_${this.tryCatchId}`, (bodyIndent: number) =>
      this.renderOuterBody(bodyIndent)
    );
  }

  public override get blockKind(): BlockKind {
    return BlockKind.TRY_CATCH_DECLARATION;
  }

  override get children(): GraphBlock[][] {
    const tryChildren = super.children;
    const catchChildren = this.catchBlock || [];
    const finallyChildren = this.finallyBlock || [];

    return [...tryChildren, catchChildren, finallyChildren];
  }

  protected renderOuterBody(indent: number): string {
    return `
${
  this.tryBlock
    ? this.renderInSubgraph(indent, `TRY_${this.tryCatchId}`, `TRY_${this.tryId}`, (bodyIndent) =>
        this.renderTryBody(bodyIndent)
      )
    : ''
}
${
  this.catchBlock
    ? this.renderInSubgraph(indent, `CATCH_${this.catchBlockId}`, `CATCH_${this.catchId}`, (bodyIndent) =>
        this.renderCatchBody(bodyIndent)
      )
    : ''
}
${
  this.finallyBlock
    ? this.renderInSubgraph(
        indent,
        `FINALLY_${this.finallyBlockId}`,
        `FINALLY_${this.finallyId}`,
        (bodyIndent) => this.renderFinallyBody(bodyIndent)
      )
    : ''
}
${this.renderDependencies(indent)}
`;
  }

  protected renderTryBody(indent: number): string {
    return `
${this.tryBlock ? this.renderChildren(indent, this.tryBlock) : ''}
${this.tryBlock ? this._renderDependencies(indent, this.tryBlock) : ''}
`;
  }

  protected renderCatchBody(indent: number): string {
    return `
${this.catchBlock ? this.renderChildren(indent, this.catchBlock) : ''}
${this.catchBlock ? this._renderDependencies(indent, this.catchBlock) : ''}
`;
  }

  protected renderFinallyBody(indent: number): string {
    return `
${this.finallyBlock ? this.renderChildren(indent, this.finallyBlock) : ''}
${this.finallyBlock ? this._renderDependencies(indent, this.finallyBlock) : ''}
`;
  }

  /**
   * Render dependencies between internal blocks: TRY - CATCH - FINALLY
   *
   * @param _indent
   */
  protected override renderDependencies(_indent: number): string {
    let result = '';
    if (this.finallyBlock) {
      const filteredFinalyBlock = this._filterChildren(this.finallyBlock);
      if (filteredFinalyBlock) {
        if (this.tryBlock) {
          const filteredTryBlock = this._filterChildren(this.tryBlock);
          if (filteredTryBlock.length !== 0) {
            result += this._renderLinesL2R(
              _indent,
              `${filteredFinalyBlock[0].firstBlock.id}`,
              filteredTryBlock[filteredTryBlock.length - 1].lastBlocks
            );
          }
        }
        if (this.catchBlock) {
          const filteredCatchBlock = this._filterChildren(this.catchBlock);
          if (filteredCatchBlock.length !== 0) {
            result += this._renderLinesL2R(
              _indent,
              `${filteredFinalyBlock[0].firstBlock.id}`,
              filteredCatchBlock[filteredCatchBlock.length - 1].lastBlocks
            );
          }
        }
      }
    }

    return result;
  }

  public override get lastBlocks(): string[] {
    if (this.finallyBlock) {
      return this.finallyBlock[this.finallyBlock.length - 1].lastBlocks;
    }

    const tryBlockLastId = super.lastBlocks;
    const catchLastId: string[] = this.catchBlock
      ? this.catchBlock[this.catchBlock.length - 1].lastBlocks
      : [];

    const allIds = [...tryBlockLastId, ...catchLastId];
    if (allIds.length === 0) {
      allIds.push(this.id);
    }

    return allIds;
  }
}

results matching ""

    No results matching ""