From 196c8f39daab70ce88f62865241ad392c3ec7050 Mon Sep 17 00:00:00 2001 From: Garry Trinder Date: Mon, 16 Dec 2024 10:39:58 +0000 Subject: [PATCH] Add warning diagnostic when a configSection exists but is not connected to a plugin. Closes #173 --- CHANGELOG.md | 1 + README.md | 1 + src/diagnostics.ts | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3c7969..b3d56d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Diagnostics: Ensure at least one plugin is enabled - Diagnostics: Information added to pluginName value when plugin can be configured with a configSection +- Diagnostics: Warning added to config sections not connected to a plugin ### Changed: diff --git a/README.md b/README.md index b2f329e..ad4de8c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ The following sections describe the features that the extension contributes to V - Check that reporters are placed after plugins - Check that at least one plugin is enabled - Check that a plugin can be configured with a configSection +- Check for configSections that are not used in plugins ### Editor Actions diff --git a/src/diagnostics.ts b/src/diagnostics.ts index a4cafcc..893b64b 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -19,6 +19,7 @@ export const updateConfigFileDiagnostics = ( checkSchemaCompatibility(documentNode, devProxyInstall, diagnostics); checkPlugins(pluginsNode, diagnostics, documentNode); + checkConfigSection(documentNode, diagnostics); collection.set(document.uri, diagnostics); }; @@ -41,6 +42,40 @@ export const updateFileDiagnostics = ( collection.set(document.uri, diagnostics); }; +const checkConfigSection = (documentNode: parse.ObjectNode, diagnostics: vscode.Diagnostic[]) => { + const objects = documentNode.children.filter((node) => node.type === 'Property' && (node as parse.PropertyNode).value.type === 'Object'); + + objects.forEach((object) => { + const objectNode = object as parse.PropertyNode; + const objectName = objectNode.key.value as string; + const pluginNodes = getPluginsNode(documentNode); + + if (pluginNodes && pluginNodes.value.type === 'Array') { + const plugins = (pluginNodes.value as parse.ArrayNode).children as parse.ObjectNode[]; + const matchFound = plugins.some((plugin) => { + const configSectionNode = getASTNode( + plugin.children, + 'Identifier', + 'configSection' + ); + return configSectionNode && (configSectionNode.value as parse.LiteralNode).value === objectName; + }); + + if (matchFound) { + return; + } + } + + const diagnostic = new vscode.Diagnostic( + getRangeFromASTNode(objectNode), + `Config section '${objectName}' does not correspond to any plugin. Remove it or add a plugin with a matching configSection.`, + vscode.DiagnosticSeverity.Warning + ); + diagnostic.code = 'invalidConfigSection'; + diagnostics.push(diagnostic); + }); +}; + const checkSchemaCompatibility = (documentNode: parse.ObjectNode, devProxyInstall: DevProxyInstall, diagnostics: vscode.Diagnostic[]) => { const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema'); if (schemaNode) {