|
1 | | -import { getSchema } from "@mrleebo/prisma-ast"; |
| 1 | +import { type CstNodeLocation, getSchema } from "@mrleebo/prisma-ast"; |
2 | 2 | import { type JSONTableSchema } from "shared/types/tableSchema"; |
| 3 | +import { DiagnosticError } from "shared/types/diagnostic"; |
3 | 4 |
|
4 | 5 | import { prismaASTToJSONTableSchema } from "./utils/transformers/prismaASTToJSONTableSchema"; |
5 | 6 |
|
6 | 7 | export const parsePrismaToJSON = (prismaCode: string): JSONTableSchema => { |
7 | | - const rawParsedSchema = getSchema(prismaCode); |
8 | | - return prismaASTToJSONTableSchema(rawParsedSchema); |
| 8 | + try { |
| 9 | + const rawParsedSchema = getSchema(prismaCode); |
| 10 | + return prismaASTToJSONTableSchema(rawParsedSchema); |
| 11 | + } catch (error) { |
| 12 | + if ("token" in (error as any)) { |
| 13 | + const token = (error as any).token as CstNodeLocation; |
| 14 | + |
| 15 | + const endColumn = token.endColumn; |
| 16 | + const endLine = token.endLine; |
| 17 | + const startColumn = token.startColumn; |
| 18 | + const startLine = token.startLine; |
| 19 | + |
| 20 | + if ( |
| 21 | + endColumn === undefined || |
| 22 | + startColumn === undefined || |
| 23 | + endLine === undefined || |
| 24 | + startLine === undefined |
| 25 | + ) { |
| 26 | + throw error; |
| 27 | + } |
| 28 | + |
| 29 | + const locationEnd = { column: endColumn, line: endLine }; |
| 30 | + const locationStart = { |
| 31 | + column: startColumn, |
| 32 | + line: startLine, |
| 33 | + }; |
| 34 | + |
| 35 | + throw new DiagnosticError( |
| 36 | + { |
| 37 | + end: { |
| 38 | + column: locationEnd.column - 1, |
| 39 | + line: locationEnd.line - 1, |
| 40 | + }, |
| 41 | + start: { |
| 42 | + column: locationStart.column - 1, |
| 43 | + line: locationStart.line - 1, |
| 44 | + }, |
| 45 | + }, |
| 46 | + String((error as Error).message), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + throw error; |
| 51 | + } |
9 | 52 | }; |
0 commit comments