Skip to content

Commit 3d4b9b7

Browse files
authored
relint when schema changes (#3405)
* relint when schema changes * Create blue-rice-arrive.md
1 parent 64f7220 commit 3d4b9b7

File tree

5 files changed

+99
-67
lines changed

5 files changed

+99
-67
lines changed

.changeset/blue-rice-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cm6-graphql": patch
3+
---
4+
5+
relint when schema changes

packages/cm6-graphql/package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"graphql-language-service": "^5.1.7"
2020
},
2121
"devDependencies": {
22-
"@codemirror/autocomplete": "^6.0.0",
22+
"@codemirror/autocomplete": "6.2.0",
2323
"@codemirror/buildhelper": "^0.1.16",
24-
"@codemirror/language": "^6.0.0",
25-
"@codemirror/lint": "^6.0.0",
26-
"@codemirror/state": "^6.1.0",
27-
"@codemirror/view": "^6.1.2",
24+
"@codemirror/language": "6.2.1",
25+
"@codemirror/lint": "6.2.1",
26+
"@codemirror/state": "6.2.0",
27+
"@codemirror/view": "6.2.1",
2828
"@lezer/common": "^1.0.0",
2929
"@lezer/generator": "^1.1.0",
3030
"@lezer/highlight": "^1.0.0",
@@ -38,11 +38,11 @@
3838
"typescript": "^4.6.3"
3939
},
4040
"peerDependencies": {
41-
"@codemirror/autocomplete": "^6.0.0",
42-
"@codemirror/language": "^6.0.0",
43-
"@codemirror/lint": "^6.0.0",
44-
"@codemirror/state": "^6.1.0",
45-
"@codemirror/view": "^6.1.2",
41+
"@codemirror/autocomplete": "6.2.0",
42+
"@codemirror/language": "6.2.1",
43+
"@codemirror/lint": "6.2.1",
44+
"@codemirror/state": "6.2.0",
45+
"@codemirror/view": "6.2.1",
4646
"@lezer/highlight": "^1.0.0",
4747
"graphql": "^16.5.0"
4848
},

packages/cm6-graphql/src/lint.ts

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
11
import { Diagnostic, linter } from '@codemirror/lint';
22
import { getDiagnostics } from 'graphql-language-service';
33
import { Position, posToOffset } from './helpers';
4-
import { getSchema } from './state';
4+
import { getSchema, optionsStateField, schemaStateField } from './state';
5+
import { Extension } from '@codemirror/state';
56

67
const SEVERITY = ['error', 'warning', 'info'] as const;
78

8-
export const lint = linter(view => {
9-
const schema = getSchema(view.state);
10-
if (!schema) {
11-
return [];
12-
}
13-
const results = getDiagnostics(view.state.doc.toString(), schema);
9+
export const lint: Extension = linter(
10+
view => {
11+
const schema = getSchema(view.state);
12+
if (!schema) {
13+
return [];
14+
}
15+
const results = getDiagnostics(view.state.doc.toString(), schema);
1416

15-
return results
16-
.map((item): Diagnostic | null => {
17-
if (!item.severity || !item.source) {
18-
return null;
19-
}
17+
return results
18+
.map((item): Diagnostic | null => {
19+
if (!item.severity || !item.source) {
20+
return null;
21+
}
2022

21-
const calculatedFrom = posToOffset(
22-
view.state.doc,
23-
new Position(item.range.start.line, item.range.start.character),
23+
const calculatedFrom = posToOffset(
24+
view.state.doc,
25+
new Position(item.range.start.line, item.range.start.character),
26+
);
27+
const from = Math.max(
28+
0,
29+
Math.min(calculatedFrom, view.state.doc.length),
30+
);
31+
const calculatedRo = posToOffset(
32+
view.state.doc,
33+
new Position(item.range.end.line, item.range.end.character - 1),
34+
);
35+
const to = Math.min(
36+
Math.max(from + 1, calculatedRo),
37+
view.state.doc.length,
38+
);
39+
return {
40+
from,
41+
to: from === to ? to + 1 : to,
42+
severity: SEVERITY[item.severity - 1],
43+
// source: item.source, // TODO:
44+
message: item.message,
45+
actions: [], // TODO:
46+
};
47+
})
48+
.filter((_): _ is Diagnostic => Boolean(_));
49+
},
50+
{
51+
needsRefresh(vu) {
52+
return (
53+
vu.startState.field(schemaStateField) !==
54+
vu.state.field(schemaStateField) ||
55+
vu.startState.field(optionsStateField) !==
56+
vu.state.field(optionsStateField)
2457
);
25-
const from = Math.max(0, Math.min(calculatedFrom, view.state.doc.length));
26-
const calculatedRo = posToOffset(
27-
view.state.doc,
28-
new Position(item.range.end.line, item.range.end.character - 1),
29-
);
30-
const to = Math.min(
31-
Math.max(from + 1, calculatedRo),
32-
view.state.doc.length,
33-
);
34-
return {
35-
from,
36-
to: from === to ? to + 1 : to,
37-
severity: SEVERITY[item.severity - 1],
38-
// source: item.source, // TODO:
39-
message: item.message,
40-
actions: [], // TODO:
41-
};
42-
})
43-
.filter((_): _ is Diagnostic => Boolean(_));
44-
});
58+
},
59+
},
60+
);

packages/cm6-graphql/src/state.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GraphQLSchema } from 'graphql';
44
import { GqlExtensionsOptions } from './interfaces';
55

66
const schemaEffect = StateEffect.define<GraphQLSchema | undefined>();
7-
const schemaStateField = StateField.define<GraphQLSchema | void>({
7+
export const schemaStateField = StateField.define<GraphQLSchema | void>({
88
create() {},
99
update(schema, tr) {
1010
for (const e of tr.effects) {
@@ -18,18 +18,20 @@ const schemaStateField = StateField.define<GraphQLSchema | void>({
1818
});
1919

2020
const optionsEffect = StateEffect.define<GqlExtensionsOptions | undefined>();
21-
const optionsStateField = StateField.define<GqlExtensionsOptions | void>({
22-
create() {},
23-
update(opts, tr) {
24-
for (const e of tr.effects) {
25-
if (e.is(optionsEffect)) {
26-
return e.value;
21+
export const optionsStateField = StateField.define<GqlExtensionsOptions | void>(
22+
{
23+
create() {},
24+
update(opts, tr) {
25+
for (const e of tr.effects) {
26+
if (e.is(optionsEffect)) {
27+
return e.value;
28+
}
2729
}
28-
}
2930

30-
return opts;
31+
return opts;
32+
},
3133
},
32-
});
34+
);
3335
export const updateSchema = (view: EditorView, schema?: GraphQLSchema) => {
3436
view.dispatch({
3537
effects: schemaEffect.of(schema),

yarn.lock

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,14 +2631,14 @@
26312631
human-id "^1.0.2"
26322632
prettier "^2.7.1"
26332633

2634-
"@codemirror/autocomplete@^6.0.0":
2635-
version "6.4.2"
2636-
resolved "https://registry.yarnpkg.com/@codemirror/autocomplete/-/autocomplete-6.4.2.tgz#938b25223bd21f97b2a6d85474643355f98b505b"
2637-
integrity sha512-8WE2xp+D0MpWEv5lZ6zPW1/tf4AGb358T5GWYiKEuCP8MvFfT3tH2mIF9Y2yr2e3KbHuSvsVhosiEyqCpiJhZQ==
2634+
"@codemirror/autocomplete@6.2.0":
2635+
version "6.2.0"
2636+
resolved "https://registry.yarnpkg.com/@codemirror/autocomplete/-/autocomplete-6.2.0.tgz#f7205f8281613f77529f07b279ee25e1a5d20124"
2637+
integrity sha512-yNCm2CEE4kE4L2Sf7WeyCej1Q3951ccaCWfomrlBkoERKCss+TzuEeqGe5VnAJTEybLy1yzf1BdMUY/988bfpg==
26382638
dependencies:
26392639
"@codemirror/language" "^6.0.0"
26402640
"@codemirror/state" "^6.0.0"
2641-
"@codemirror/view" "^6.6.0"
2641+
"@codemirror/view" "^6.0.0"
26422642
"@lezer/common" "^1.0.0"
26432643

26442644
"@codemirror/buildhelper@^0.1.16":
@@ -2659,7 +2659,7 @@
26592659
serve-static "^1.14.1"
26602660
typescript "^4.2.3"
26612661

2662-
"@codemirror/language@^6.0.0":
2662+
"@codemirror/language@6.2.1", "@codemirror/language@^6.0.0":
26632663
version "6.2.1"
26642664
resolved "https://registry.yarnpkg.com/@codemirror/language/-/language-6.2.1.tgz#cb10cd785a76e50ecd2fe2dc59ff66af8a41b87a"
26652665
integrity sha512-MC3svxuvIj0MRpFlGHxLS6vPyIdbTr2KKPEW46kCoCXw2ktb4NTkpkPBI/lSP/FoNXLCBJ0mrnUi1OoZxtpW1Q==
@@ -2671,21 +2671,30 @@
26712671
"@lezer/lr" "^1.0.0"
26722672
style-mod "^4.0.0"
26732673

2674-
"@codemirror/lint@^6.0.0":
2675-
version "6.2.0"
2676-
resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.2.0.tgz#25cdab7425fcda1b38a9d63f230f833c8b6b369f"
2677-
integrity sha512-KVCECmR2fFeYBr1ZXDVue7x3q5PMI0PzcIbA+zKufnkniMBo1325t0h1jM85AKp8l3tj67LRxVpZfgDxEXlQkg==
2674+
"@codemirror/lint@6.2.1":
2675+
version "6.2.1"
2676+
resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.2.1.tgz#654581d8cc293c315ecfa5c9d61d78c52bbd9ccd"
2677+
integrity sha512-y1muai5U/uUPAGRyHMx9mHuHLypPcHWxzlZGknp/U5Mdb5Ol8Q5ZLp67UqyTbNFJJ3unVxZ8iX3g1fMN79S1JQ==
26782678
dependencies:
26792679
"@codemirror/state" "^6.0.0"
26802680
"@codemirror/view" "^6.0.0"
26812681
crelt "^1.0.5"
26822682

2683-
"@codemirror/state@^6.0.0", "@codemirror/state@^6.1.0", "@codemirror/state@^6.1.4":
2683+
"@codemirror/state@6.2.0", "@codemirror/state@^6.0.0", "@codemirror/state@^6.1.4":
26842684
version "6.2.0"
26852685
resolved "https://registry.yarnpkg.com/@codemirror/state/-/state-6.2.0.tgz#a0fb08403ced8c2a68d1d0acee926bd20be922f2"
26862686
integrity sha512-69QXtcrsc3RYtOtd+GsvczJ319udtBf1PTrr2KbLWM/e2CXUPnh0Nz9AUo8WfhSQ7GeL8dPVNUmhQVgpmuaNGA==
26872687

2688-
"@codemirror/view@^6.0.0", "@codemirror/view@^6.1.2", "@codemirror/view@^6.6.0":
2688+
"@codemirror/[email protected]":
2689+
version "6.2.1"
2690+
resolved "https://registry.yarnpkg.com/@codemirror/view/-/view-6.2.1.tgz#299698639c658c738f10021c5ea78a513c63977b"
2691+
integrity sha512-r1svbtAj2Lp/86F3yy1TfDAOAtJRGLINLSEqByETyUaGo1EnLS+P+bbGCVHV62z46BzZYm16noDid69+4bzn0g==
2692+
dependencies:
2693+
"@codemirror/state" "^6.0.0"
2694+
style-mod "^4.0.0"
2695+
w3c-keyname "^2.2.4"
2696+
2697+
"@codemirror/view@^6.0.0":
26892698
version "6.9.1"
26902699
resolved "https://registry.yarnpkg.com/@codemirror/view/-/view-6.9.1.tgz#2ce4c528974b6172a5a4a738b7b0a0f04a4c1140"
26912700
integrity sha512-bzfSjJn9dAADVpabLKWKNmMG4ibyTV2e3eOGowjElNPTdTkSbi6ixPYHm2u0ADcETfKsi2/R84Rkmi91dH9yEg==

0 commit comments

Comments
 (0)