Skip to content

Commit

Permalink
refactor: Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Nov 15, 2023
1 parent f15d24b commit 0d7644c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .esbuild/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { build } from 'esbuild';
import { mkdir, writeFile } from 'node:fs/promises';
import { MermaidBuildOptions, defaultOptions, getBuildConfig } from './util.js';
import { packageOptions } from '../.build/common.js';
import { generateLangium } from '../.build/generateLangium.js';
import { MermaidBuildOptions, defaultOptions, getBuildConfig } from './util.js';

const shouldVisualize = process.argv.includes('--visualize');

Expand Down Expand Up @@ -56,7 +56,7 @@ const main = async () => {
await generateLangium();
await mkdir('stats').catch(() => {});
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
// it should build `parser` before `mermaid` because it's a dependecy
// it should build `parser` before `mermaid` because it's a dependency
for (const pkg of packageNames) {
await buildPackage(pkg).catch(handler);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/mermaid/src/diagrams/packet/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
setAccTitle,
setDiagramTitle,
} from '../common/commonDb.js';
import type { PacketDB, PacketData, Row } from './types.js';
import type { PacketDB, PacketData, PacketWord } from './types.js';

const defaultPacketData: PacketData = {
packet: [],
Expand All @@ -32,9 +32,9 @@ const getConfig = (): Required<PacketDiagramConfig> => {
return config;
};

const getPacket = (): Row[] => data.packet;
const getPacket = (): PacketWord[] => data.packet;

const pushWord = (word: Row) => {
const pushWord = (word: PacketWord) => {
if (word.length > 0) {
data.packet.push(word);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/mermaid/src/diagrams/packet/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { ParserDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js';
import { db } from './db.js';
import type { Block, Row } from './types.js';
import type { PacketBlock, PacketWord } from './types.js';

const maxPacketSize = 10_000;

const populate = (ast: Packet) => {
populateCommonDb(ast, db);
let lastByte = -1;
let word: Row = [];
let word: PacketWord = [];
let row = 1;
const { bitsPerRow } = db.getConfig();
for (let { start, end, label } of ast.blocks) {
Expand Down Expand Up @@ -46,10 +46,10 @@ const populate = (ast: Packet) => {
};

const getNextFittingBlock = (
block: Block,
block: PacketBlock,
row: number,
bitsPerRow: number
): [Required<Block>, Block | undefined] => {
): [Required<PacketBlock>, PacketBlock | undefined] => {
if (block.end === undefined) {
block.end = block.start;
}
Expand All @@ -59,7 +59,7 @@ const getNextFittingBlock = (
}

if (block.end + 1 <= row * bitsPerRow) {
return [block as Required<Block>, undefined];
return [block as Required<PacketBlock>, undefined];
}

return [
Expand Down
6 changes: 3 additions & 3 deletions packages/mermaid/src/diagrams/packet/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { PacketDiagramConfig } from '../../config.type.js';
import type { DiagramRenderer, DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import type { PacketDB, Row } from './types.js';
import type { PacketDB, PacketWord } from './types.js';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
Expand Down Expand Up @@ -36,13 +36,13 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {

const drawWord = (
svg: SVG,
row: Row,
word: PacketWord,
rowNumber: number,
{ rowHeight, paddingX, paddingY, bitWidth, bitsPerRow, showBits }: Required<PacketDiagramConfig>
) => {
const group: Group = svg.append('g');
const wordY = rowNumber * (rowHeight + paddingY) + paddingY;
for (const block of row) {
for (const block of word) {
const blockX = (block.start % bitsPerRow) * bitWidth + 1;
const width = (block.end - block.start + 1) * bitWidth - paddingX;
// Block rectangle
Expand Down
14 changes: 7 additions & 7 deletions packages/mermaid/src/diagrams/packet/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Packet } from 'mermaid-parser';
import type { Packet, RecursiveAstOmit } from 'mermaid-parser';
import type { PacketDiagramConfig } from '../../config.type.js';
import type { DiagramDBBase } from '../../diagram-api/types.js';
import type { ArrayElement } from '../../types.js';

export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>;
export type Row = Required<Block>[];
export type PacketBlock = RecursiveAstOmit<ArrayElement<Packet['blocks']>>;
export type PacketWord = Required<PacketBlock>[];

export interface PacketDB extends DiagramDBBase<PacketDiagramConfig> {
pushWord: (word: Row) => void;
getPacket: () => Row[];
pushWord: (word: PacketWord) => void;
getPacket: () => PacketWord[];
}

export interface PacketStyleOptions {
Expand All @@ -25,5 +25,5 @@ export interface PacketStyleOptions {
}

export interface PacketData {
packet: Row[];
packet: PacketWord[];
}
2 changes: 2 additions & 0 deletions packages/mermaid/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export interface EdgeData {
labelStyle: string;
curve: any;
}

export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
11 changes: 10 additions & 1 deletion packages/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import type { AstNode } from 'langium';

export type { Info, Packet } from './language/index.js';
export { MermaidParseError, parse } from './parse.js';
export type { DiagramAST } from './parse.js';
export { parse, MermaidParseError } from './parse.js';

/**
* Exclude/omit all `AstNode` attributes recursively.
*/
export type RecursiveAstOmit<T> = T extends object
? { [P in keyof T as Exclude<P, keyof AstNode>]: RecursiveAstOmit<T[P]> }
: T;

0 comments on commit 0d7644c

Please sign in to comment.