|
1 | 1 | import { Diagnostic, linter } from '@codemirror/lint';
|
2 | 2 | import { getDiagnostics } from 'graphql-language-service';
|
3 | 3 | import { Position, posToOffset } from './helpers';
|
4 |
| -import { getSchema, optionsStateField, schemaStateField } from './state'; |
| 4 | +import { |
| 5 | + getOpts, |
| 6 | + getSchema, |
| 7 | + optionsStateField, |
| 8 | + schemaStateField, |
| 9 | +} from './state'; |
5 | 10 | import { Extension } from '@codemirror/state';
|
| 11 | +import { validateSchema } from 'graphql'; |
6 | 12 |
|
7 | 13 | const SEVERITY = ['error', 'warning', 'info'] as const;
|
8 | 14 |
|
9 | 15 | export const lint: Extension = linter(
|
10 | 16 | view => {
|
11 |
| - try { |
12 |
| - const schema = getSchema(view.state); |
13 |
| - if (!schema) { |
| 17 | + const schema = getSchema(view.state); |
| 18 | + const options = getOpts(view.state); |
| 19 | + if (!schema) { |
| 20 | + return []; |
| 21 | + } |
| 22 | + const validationErrors = validateSchema(schema); |
| 23 | + if (validationErrors.length) { |
| 24 | + if (!options?.showErrorOnInvalidSchema) { |
14 | 25 | return [];
|
15 | 26 | }
|
16 |
| - const results = getDiagnostics(view.state.doc.toString(), schema); |
17 | 27 |
|
18 |
| - return results |
19 |
| - .map((item): Diagnostic | null => { |
20 |
| - if (!item.severity || !item.source) { |
21 |
| - return null; |
22 |
| - } |
| 28 | + const combinedError = validationErrors.map(error => { |
| 29 | + return error.message; |
| 30 | + }); |
23 | 31 |
|
24 |
| - const calculatedFrom = posToOffset( |
25 |
| - view.state.doc, |
26 |
| - new Position(item.range.start.line, item.range.start.character), |
27 |
| - ); |
28 |
| - const from = Math.max( |
29 |
| - 0, |
30 |
| - Math.min(calculatedFrom, view.state.doc.length), |
31 |
| - ); |
32 |
| - const calculatedRo = posToOffset( |
33 |
| - view.state.doc, |
34 |
| - new Position(item.range.end.line, item.range.end.character - 1), |
35 |
| - ); |
36 |
| - const to = Math.min( |
37 |
| - Math.max(from + 1, calculatedRo), |
38 |
| - view.state.doc.length, |
39 |
| - ); |
40 |
| - return { |
41 |
| - from, |
42 |
| - to: from === to ? to + 1 : to, |
43 |
| - severity: SEVERITY[item.severity - 1], |
44 |
| - // source: item.source, // TODO: |
45 |
| - message: item.message, |
46 |
| - actions: [], // TODO: |
47 |
| - }; |
48 |
| - }) |
49 |
| - .filter((_): _ is Diagnostic => Boolean(_)); |
50 |
| - } catch { |
51 |
| - return []; |
| 32 | + return [ |
| 33 | + { |
| 34 | + from: 0, |
| 35 | + to: view.state.doc.length, |
| 36 | + severity: 'error', |
| 37 | + message: combinedError.join('\n'), |
| 38 | + actions: [], // TODO: |
| 39 | + }, |
| 40 | + ]; |
52 | 41 | }
|
| 42 | + const results = getDiagnostics(view.state.doc.toString(), schema); |
| 43 | + |
| 44 | + return results |
| 45 | + .map((item): Diagnostic | null => { |
| 46 | + if (!item.severity || !item.source) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + const calculatedFrom = posToOffset( |
| 51 | + view.state.doc, |
| 52 | + new Position(item.range.start.line, item.range.start.character), |
| 53 | + ); |
| 54 | + const from = Math.max( |
| 55 | + 0, |
| 56 | + Math.min(calculatedFrom, view.state.doc.length), |
| 57 | + ); |
| 58 | + const calculatedRo = posToOffset( |
| 59 | + view.state.doc, |
| 60 | + new Position(item.range.end.line, item.range.end.character - 1), |
| 61 | + ); |
| 62 | + const to = Math.min( |
| 63 | + Math.max(from + 1, calculatedRo), |
| 64 | + view.state.doc.length, |
| 65 | + ); |
| 66 | + return { |
| 67 | + from, |
| 68 | + to: from === to ? to + 1 : to, |
| 69 | + severity: SEVERITY[item.severity - 1], |
| 70 | + // source: item.source, // TODO: |
| 71 | + message: item.message, |
| 72 | + actions: [], // TODO: |
| 73 | + }; |
| 74 | + }) |
| 75 | + .filter((_): _ is Diagnostic => Boolean(_)); |
53 | 76 | },
|
54 | 77 | {
|
55 | 78 | needsRefresh(vu) {
|
|
0 commit comments