Skip to content

Commit

Permalink
Add type narrowing to parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Aug 17, 2023
1 parent 6fa8294 commit efad6b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/info/infoParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function populateDb(ast: Info, db: InfoDB) {

export const parser: ParserDefinition = {
parse: (input: string): void => {
const result = parse<Info>('info', input);
const result = parse('info', input);
log.debug(result);
populateDb(result, db);
},
Expand Down
8 changes: 4 additions & 4 deletions packages/parser/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { LangiumParser, ParseResult } from 'langium';

import type { Info } from './index.js';
import { createInfoServices } from './language/index.js';

Expand All @@ -16,10 +15,11 @@ const initializers = {
},
} as const;

export const parse = <T extends DiagramAST>(
export function parse<Info>(diagramType: 'info', text: string): Info;
export function parse<T extends DiagramAST>(
diagramType: keyof typeof initializers,
text: string
): T => {
): T {
const initializer = initializers[diagramType];
if (!initializer) {
throw new Error(`Unknown diagram type: ${diagramType}`);
Expand All @@ -33,7 +33,7 @@ export const parse = <T extends DiagramAST>(
throw new MermaidParseError(result);
}
return result.value;
};
}

export class MermaidParseError extends Error {
constructor(public result: ParseResult<DiagramAST>) {
Expand Down

0 comments on commit efad6b3

Please sign in to comment.