@@ -2,123 +2,143 @@ import * as vscode from 'vscode';
22import parse from "json-to-ast" ;
33import { pluginSnippets } from "./constants" ;
44import { getASTNode , getRangeFromASTNode } from "./helpers" ;
5+ import { DevProxyInstall } from './types' ;
56
67export const updateDiagnostics = (
7- document : vscode . TextDocument ,
8- collection : vscode . DiagnosticCollection
9- ) : void => {
10- let diagnostics : vscode . Diagnostic [ ] = [ ] ;
11-
12- const documentNode = parse ( document . getText ( ) ) as parse . ObjectNode ;
13-
14- // check if urlsToWatch is empty
15- const urlsToWatchNode = getASTNode (
16- documentNode . children ,
17- 'Identifier' ,
18- 'urlsToWatch'
8+ context : vscode . ExtensionContext ,
9+ document : vscode . TextDocument ,
10+ collection : vscode . DiagnosticCollection ,
11+ ) : void => {
12+ const devProxyInstall = context . globalState . get < DevProxyInstall > ( 'devProxyInstall' ) ;
13+ if ( ! devProxyInstall ) {
14+ return ;
15+ }
16+ const diagnostics : vscode . Diagnostic [ ] = [ ] ;
17+ const documentNode = parse ( document . getText ( ) ) as parse . ObjectNode ;
18+
19+ // check if schema version is compatible
20+ const schemaNode = getASTNode ( documentNode . children , 'Identifier' , '$schema' ) ;
21+ if ( schemaNode ) {
22+ const schemaValue = ( schemaNode . value as parse . LiteralNode ) . value as string ;
23+ if ( ! schemaValue . includes ( `${ devProxyInstall . version } ` ) ) {
24+ const diagnostic = new vscode . Diagnostic (
25+ getRangeFromASTNode ( schemaNode ) ,
26+ `Schema version is not compatible with the installed version of Dev Proxy. Expected v${ devProxyInstall . version } .` ,
27+ vscode . DiagnosticSeverity . Warning
28+ ) ;
29+ diagnostic . code = 'invalidSchema' ;
30+ diagnostics . push ( diagnostic ) ;
31+ }
32+ }
33+
34+ // check if urlsToWatch is empty
35+ const urlsToWatchNode = getASTNode (
36+ documentNode . children ,
37+ 'Identifier' ,
38+ 'urlsToWatch'
39+ ) ;
40+ if (
41+ urlsToWatchNode &&
42+ ( urlsToWatchNode . value as parse . ArrayNode ) . children . length === 0
43+ ) {
44+ diagnostics . push (
45+ new vscode . Diagnostic (
46+ getRangeFromASTNode ( urlsToWatchNode ) ,
47+ 'Add at least one url to watch.' ,
48+ vscode . DiagnosticSeverity . Error
49+ )
1950 ) ;
20- if (
21- urlsToWatchNode &&
22- ( urlsToWatchNode . value as parse . ArrayNode ) . children . length === 0
23- ) {
51+ }
52+
53+ // check validity of plugins
54+ const pluginsNode = getASTNode (
55+ documentNode . children ,
56+ 'Identifier' ,
57+ 'plugins'
58+ ) ;
59+ if (
60+ pluginsNode &&
61+ ( pluginsNode . value as parse . ArrayNode ) . children . length !== 0
62+ ) {
63+ const pluginNodes = ( pluginsNode . value as parse . ArrayNode )
64+ . children as parse . ObjectNode [ ] ;
65+
66+ // check for plugins
67+ if ( pluginNodes . length === 0 ) {
2468 diagnostics . push (
2569 new vscode . Diagnostic (
26- getRangeFromASTNode ( urlsToWatchNode ) ,
27- 'Add at least one url to watch. ' ,
70+ getRangeFromASTNode ( pluginsNode ) ,
71+ 'Add at least one plugin ' ,
2872 vscode . DiagnosticSeverity . Error
2973 )
3074 ) ;
3175 }
32-
33- // check validity of plugins
34- const pluginsNode = getASTNode (
35- documentNode . children ,
36- 'Identifier' ,
37- 'plugins'
38- ) ;
39- if (
40- pluginsNode &&
41- ( pluginsNode . value as parse . ArrayNode ) . children . length !== 0
42- ) {
43- const pluginNodes = ( pluginsNode . value as parse . ArrayNode )
44- . children as parse . ObjectNode [ ] ;
45-
46- // check for plugins
47- if ( pluginNodes . length === 0 ) {
48- diagnostics . push (
49- new vscode . Diagnostic (
50- getRangeFromASTNode ( pluginsNode ) ,
51- 'Add at least one plugin' ,
52- vscode . DiagnosticSeverity . Error
53- )
54- ) ;
55- }
56-
57- // does the plugin have a config section?
58- pluginNodes . forEach ( ( pluginNode : parse . ObjectNode ) => {
59- const pluginNameNode = getASTNode (
60- pluginNode . children ,
61- 'Identifier' ,
62- 'name'
63- ) ;
64- const pluginName = ( pluginNameNode ?. value as parse . LiteralNode )
65- . value as string ;
66- const enabledNode = getASTNode (
76+
77+ // does the plugin have a config section?
78+ pluginNodes . forEach ( ( pluginNode : parse . ObjectNode ) => {
79+ const pluginNameNode = getASTNode (
80+ pluginNode . children ,
81+ 'Identifier' ,
82+ 'name'
83+ ) ;
84+ const pluginName = ( pluginNameNode ?. value as parse . LiteralNode )
85+ . value as string ;
86+ const enabledNode = getASTNode (
87+ pluginNode . children ,
88+ 'Identifier' ,
89+ 'enabled'
90+ ) ;
91+ const isEnabled = ( enabledNode ?. value as parse . LiteralNode )
92+ . value as boolean ;
93+ const pluginSnippet = pluginSnippets [ pluginName ] ;
94+ const requiresConfig = pluginSnippet . config
95+ ? pluginSnippet . config . required
96+ : false ;
97+
98+ if ( requiresConfig ) {
99+ // check to see if the plugin has a config section
100+ const configSectionNode = getASTNode (
67101 pluginNode . children ,
68102 'Identifier' ,
69- 'enabled '
103+ 'configSection '
70104 ) ;
71- const isEnabled = ( enabledNode ?. value as parse . LiteralNode )
72- . value as boolean ;
73- const pluginSnippet = pluginSnippets [ pluginName ] ;
74- const requiresConfig = pluginSnippet . config
75- ? pluginSnippet . config . required
76- : false ;
77-
78- if ( requiresConfig ) {
79- // check to see if the plugin has a config section
80- const configSectionNode = getASTNode (
81- pluginNode . children ,
105+ if ( ! configSectionNode ) {
106+ // there is no config section defined on the plugin instance
107+ diagnostics . push (
108+ new vscode . Diagnostic (
109+ getRangeFromASTNode ( pluginNode ) ,
110+ `${ pluginName } requires a config section.` ,
111+ isEnabled
112+ ? vscode . DiagnosticSeverity . Error
113+ : vscode . DiagnosticSeverity . Warning
114+ )
115+ ) ;
116+ } else {
117+ // check to see if the config section is in the document
118+ const configSectionName = (
119+ configSectionNode . value as parse . LiteralNode
120+ ) . value as string ;
121+ const configSection = getASTNode (
122+ documentNode . children ,
82123 'Identifier' ,
83- 'configSection'
124+ configSectionName
84125 ) ;
85- if ( ! configSectionNode ) {
86- // there is no config section defined on the plugin instance
126+
127+ if ( ! configSection ) {
87128 diagnostics . push (
88129 new vscode . Diagnostic (
89- getRangeFromASTNode ( pluginNode ) ,
90- `${ pluginName } requires a config section .` ,
130+ getRangeFromASTNode ( configSectionNode . value ) ,
131+ `${ configSectionName } config section is missing. Use ' ${ pluginSnippet . config ?. name } ' snippet to create one .` ,
91132 isEnabled
92133 ? vscode . DiagnosticSeverity . Error
93134 : vscode . DiagnosticSeverity . Warning
94135 )
95136 ) ;
96- } else {
97- // check to see if the config section is in the document
98- const configSectionName = (
99- configSectionNode . value as parse . LiteralNode
100- ) . value as string ;
101- const configSection = getASTNode (
102- documentNode . children ,
103- 'Identifier' ,
104- configSectionName
105- ) ;
106-
107- if ( ! configSection ) {
108- diagnostics . push (
109- new vscode . Diagnostic (
110- getRangeFromASTNode ( configSectionNode . value ) ,
111- `${ configSectionName } config section is missing. Use '${ pluginSnippet . config ?. name } ' snippet to create one.` ,
112- isEnabled
113- ? vscode . DiagnosticSeverity . Error
114- : vscode . DiagnosticSeverity . Warning
115- )
116- ) ;
117- }
118137 }
119138 }
120- } ) ;
121- }
122-
123- collection . set ( document . uri , diagnostics ) ;
124- } ;
139+ }
140+ } ) ;
141+ }
142+
143+ collection . set ( document . uri , diagnostics ) ;
144+ } ;
0 commit comments