refactor: Fix types

This commit is contained in:
Sidharth Vinod 2023-11-15 09:25:49 +05:30
parent f15d24b4e8
commit 0d7644c782
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
7 changed files with 32 additions and 21 deletions

View File

@ -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');
@ -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);
}

View File

@ -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: [],
@ -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);
}

View File

@ -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) {
@ -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;
}
@ -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 [

View File

@ -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) => {
@ -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

View File

@ -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 {
@ -25,5 +25,5 @@ export interface PacketStyleOptions {
}
export interface PacketData {
packet: Row[];
packet: PacketWord[];
}

View File

@ -32,3 +32,5 @@ export interface EdgeData {
labelStyle: string;
curve: any;
}
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;

View File

@ -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;