refactor: Fix types

This commit is contained in:
Sidharth Vinod
2023-11-15 09:25:49 +05:30
parent f15d24b4e8
commit 0d7644c782
7 changed files with 32 additions and 21 deletions

View File

@@ -1,8 +1,8 @@
import { build } from 'esbuild'; import { build } from 'esbuild';
import { mkdir, writeFile } from 'node:fs/promises'; import { mkdir, writeFile } from 'node:fs/promises';
import { MermaidBuildOptions, defaultOptions, getBuildConfig } from './util.js';
import { packageOptions } from '../.build/common.js'; import { packageOptions } from '../.build/common.js';
import { generateLangium } from '../.build/generateLangium.js'; import { generateLangium } from '../.build/generateLangium.js';
import { MermaidBuildOptions, defaultOptions, getBuildConfig } from './util.js';
const shouldVisualize = process.argv.includes('--visualize'); const shouldVisualize = process.argv.includes('--visualize');
@@ -56,7 +56,7 @@ const main = async () => {
await generateLangium(); await generateLangium();
await mkdir('stats').catch(() => {}); await mkdir('stats').catch(() => {});
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[]; 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) { for (const pkg of packageNames) {
await buildPackage(pkg).catch(handler); await buildPackage(pkg).catch(handler);
} }

View File

@@ -11,7 +11,7 @@ import {
setAccTitle, setAccTitle,
setDiagramTitle, setDiagramTitle,
} from '../common/commonDb.js'; } from '../common/commonDb.js';
import type { PacketDB, PacketData, Row } from './types.js'; import type { PacketDB, PacketData, PacketWord } from './types.js';
const defaultPacketData: PacketData = { const defaultPacketData: PacketData = {
packet: [], packet: [],
@@ -32,9 +32,9 @@ const getConfig = (): Required<PacketDiagramConfig> => {
return config; return config;
}; };
const getPacket = (): Row[] => data.packet; const getPacket = (): PacketWord[] => data.packet;
const pushWord = (word: Row) => { const pushWord = (word: PacketWord) => {
if (word.length > 0) { if (word.length > 0) {
data.packet.push(word); data.packet.push(word);
} }

View File

@@ -4,14 +4,14 @@ import type { ParserDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js'; import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js'; import { populateCommonDb } from '../common/populateCommonDb.js';
import { db } from './db.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 maxPacketSize = 10_000;
const populate = (ast: Packet) => { const populate = (ast: Packet) => {
populateCommonDb(ast, db); populateCommonDb(ast, db);
let lastByte = -1; let lastByte = -1;
let word: Row = []; let word: PacketWord = [];
let row = 1; let row = 1;
const { bitsPerRow } = db.getConfig(); const { bitsPerRow } = db.getConfig();
for (let { start, end, label } of ast.blocks) { for (let { start, end, label } of ast.blocks) {
@@ -46,10 +46,10 @@ const populate = (ast: Packet) => {
}; };
const getNextFittingBlock = ( const getNextFittingBlock = (
block: Block, block: PacketBlock,
row: number, row: number,
bitsPerRow: number bitsPerRow: number
): [Required<Block>, Block | undefined] => { ): [Required<PacketBlock>, PacketBlock | undefined] => {
if (block.end === undefined) { if (block.end === undefined) {
block.end = block.start; block.end = block.start;
} }
@@ -59,7 +59,7 @@ const getNextFittingBlock = (
} }
if (block.end + 1 <= row * bitsPerRow) { if (block.end + 1 <= row * bitsPerRow) {
return [block as Required<Block>, undefined]; return [block as Required<PacketBlock>, undefined];
} }
return [ 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 type { DiagramRenderer, DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js'; import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
import { configureSvgSize } from '../../setupGraphViewbox.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 // eslint-disable-next-line @typescript-eslint/no-unused-vars
const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => { const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
@@ -36,13 +36,13 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
const drawWord = ( const drawWord = (
svg: SVG, svg: SVG,
row: Row, word: PacketWord,
rowNumber: number, rowNumber: number,
{ rowHeight, paddingX, paddingY, bitWidth, bitsPerRow, showBits }: Required<PacketDiagramConfig> { rowHeight, paddingX, paddingY, bitWidth, bitsPerRow, showBits }: Required<PacketDiagramConfig>
) => { ) => {
const group: Group = svg.append('g'); const group: Group = svg.append('g');
const wordY = rowNumber * (rowHeight + paddingY) + paddingY; const wordY = rowNumber * (rowHeight + paddingY) + paddingY;
for (const block of row) { for (const block of word) {
const blockX = (block.start % bitsPerRow) * bitWidth + 1; const blockX = (block.start % bitsPerRow) * bitWidth + 1;
const width = (block.end - block.start + 1) * bitWidth - paddingX; const width = (block.end - block.start + 1) * bitWidth - paddingX;
// Block rectangle // 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 { PacketDiagramConfig } from '../../config.type.js';
import type { DiagramDBBase } from '../../diagram-api/types.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 PacketBlock = RecursiveAstOmit<ArrayElement<Packet['blocks']>>;
export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>; export type PacketWord = Required<PacketBlock>[];
export type Row = Required<Block>[];
export interface PacketDB extends DiagramDBBase<PacketDiagramConfig> { export interface PacketDB extends DiagramDBBase<PacketDiagramConfig> {
pushWord: (word: Row) => void; pushWord: (word: PacketWord) => void;
getPacket: () => Row[]; getPacket: () => PacketWord[];
} }
export interface PacketStyleOptions { export interface PacketStyleOptions {
@@ -25,5 +25,5 @@ export interface PacketStyleOptions {
} }
export interface PacketData { export interface PacketData {
packet: Row[]; packet: PacketWord[];
} }

View File

@@ -32,3 +32,5 @@ export interface EdgeData {
labelStyle: string; labelStyle: string;
curve: any; 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 type { Info, Packet } from './language/index.js';
export { MermaidParseError, parse } from './parse.js';
export type { DiagramAST } 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;