diff --git a/.esbuild/build.ts b/.esbuild/build.ts index 3e829d7193..3c87f9d621 100644 --- a/.esbuild/build.ts +++ b/.esbuild/build.ts @@ -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); } diff --git a/packages/mermaid/src/diagrams/packet/db.ts b/packages/mermaid/src/diagrams/packet/db.ts index a40b71724d..d7b5504723 100644 --- a/packages/mermaid/src/diagrams/packet/db.ts +++ b/packages/mermaid/src/diagrams/packet/db.ts @@ -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 => { 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); } diff --git a/packages/mermaid/src/diagrams/packet/parser.ts b/packages/mermaid/src/diagrams/packet/parser.ts index c0e9e12a08..fa90444df2 100644 --- a/packages/mermaid/src/diagrams/packet/parser.ts +++ b/packages/mermaid/src/diagrams/packet/parser.ts @@ -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 | undefined] => { +): [Required, 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, undefined]; + return [block as Required, undefined]; } return [ diff --git a/packages/mermaid/src/diagrams/packet/renderer.ts b/packages/mermaid/src/diagrams/packet/renderer.ts index c4547fd80d..21b941a172 100644 --- a/packages/mermaid/src/diagrams/packet/renderer.ts +++ b/packages/mermaid/src/diagrams/packet/renderer.ts @@ -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 ) => { 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 diff --git a/packages/mermaid/src/diagrams/packet/types.ts b/packages/mermaid/src/diagrams/packet/types.ts index 0a4507076d..bbb87db698 100644 --- a/packages/mermaid/src/diagrams/packet/types.ts +++ b/packages/mermaid/src/diagrams/packet/types.ts @@ -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 extends readonly (infer T)[] ? T : never; -export type Block = Pick, 'start' | 'end' | 'label'>; -export type Row = Required[]; +export type PacketBlock = RecursiveAstOmit>; +export type PacketWord = Required[]; export interface PacketDB extends DiagramDBBase { - 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[]; } diff --git a/packages/mermaid/src/types.ts b/packages/mermaid/src/types.ts index 13da885033..487e089dce 100644 --- a/packages/mermaid/src/types.ts +++ b/packages/mermaid/src/types.ts @@ -32,3 +32,5 @@ export interface EdgeData { labelStyle: string; curve: any; } + +export type ArrayElement = A extends readonly (infer T)[] ? T : never; diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index 9ec6b4e9b9..c72b4fcb6e 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -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 extends object + ? { [P in keyof T as Exclude]: RecursiveAstOmit } + : T;