|
1 | | -import { Parser } from "@dbml/core"; |
| 1 | +import { Parser, type CompilerDiagnostic } from "@dbml/core"; |
| 2 | +import { DiagnosticError } from "shared/types/diagnostic"; |
2 | 3 |
|
3 | 4 | import { dbmlSchemaToJSONTableSchema } from "./utils/transfomers/dbmlSchemaToJSONTableSchema"; |
4 | 5 | import { validateSchema } from "./validators"; |
5 | 6 |
|
6 | 7 | import type { JSONTableSchema } from "shared/types/tableSchema"; |
7 | 8 |
|
8 | 9 | export const parseDBMLToJSON = (dbmlCode: string): JSONTableSchema => { |
9 | | - const rawParsedSchema = Parser.parseDBMLToJSON(dbmlCode); |
10 | | - validateSchema(rawParsedSchema); |
11 | | - return dbmlSchemaToJSONTableSchema(rawParsedSchema); |
| 10 | + try { |
| 11 | + const rawParsedSchema = Parser.parseDBMLToJSON(dbmlCode); |
| 12 | + validateSchema(rawParsedSchema); |
| 13 | + return dbmlSchemaToJSONTableSchema(rawParsedSchema); |
| 14 | + } catch (error) { |
| 15 | + if ("location" in (error as any) && "message" in (error as any)) { |
| 16 | + const _error = error as CompilerDiagnostic; |
| 17 | + |
| 18 | + const locationEnd = _error.location.end; |
| 19 | + const locationStart = _error.location.start; |
| 20 | + |
| 21 | + if (locationEnd === undefined || locationStart === undefined) { |
| 22 | + throw error; |
| 23 | + } |
| 24 | + |
| 25 | + throw new DiagnosticError( |
| 26 | + { |
| 27 | + end: { |
| 28 | + column: locationEnd.column - 1, |
| 29 | + line: locationEnd.line - 1, |
| 30 | + }, |
| 31 | + start: { |
| 32 | + column: locationStart.column - 1, |
| 33 | + line: locationStart.line - 1, |
| 34 | + }, |
| 35 | + }, |
| 36 | + _error.message, |
| 37 | + ); |
| 38 | + } |
| 39 | + throw error; |
| 40 | + } |
12 | 41 | }; |
0 commit comments