Skip to content

Commit ecfae3e

Browse files
committed
feat(dbml-parser): transform parsing error location
1 parent 5442b80 commit ecfae3e

File tree

1 file changed

+33
-4
lines changed
  • packages/dbml-to-json-table-schema/src

1 file changed

+33
-4
lines changed
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
import { Parser } from "@dbml/core";
1+
import { Parser, type CompilerDiagnostic } from "@dbml/core";
2+
import { DiagnosticError } from "shared/types/diagnostic";
23

34
import { dbmlSchemaToJSONTableSchema } from "./utils/transfomers/dbmlSchemaToJSONTableSchema";
45
import { validateSchema } from "./validators";
56

67
import type { JSONTableSchema } from "shared/types/tableSchema";
78

89
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+
}
1241
};

0 commit comments

Comments
 (0)