|
| 1 | +/** |
| 2 | + * @fileoverview Handles scanning and providing diagnostics regarding an Apache Dispatcher Config file (e.g., duplicate properties). |
| 3 | + * @author Darian Benam <[email protected]> |
| 4 | + */ |
| 5 | + |
| 6 | +import { PROPERTY_PREFIX_CHARACTER } from "@language-server/constants/trigger-character"; |
| 7 | +import { ApacheDispatcherConfigToken } from "@language-server/core/tree-sitter"; |
| 8 | +import { convertValueToMd5Hash } from "@language-server/utils/crypto"; |
| 9 | +import { getSyntaxNodeRange } from "@language-server/utils/range"; |
| 10 | +import { removeOuterQuotes } from "@language-server/utils/string"; |
| 11 | +import { Diagnostic, DiagnosticSeverity, Range } from "vscode-languageserver"; |
| 12 | +import Parser = require("web-tree-sitter"); |
| 13 | + |
| 14 | +function createDuplicatePropertyDiagnostic( |
| 15 | + propertyName: string, |
| 16 | + range: Range, |
| 17 | + scopeId: string |
| 18 | +): Diagnostic { |
| 19 | + return { |
| 20 | + message: `The property '${propertyName}' is already defined in the current scope (Scope ID: ${scopeId}).`, |
| 21 | + range: range, |
| 22 | + severity: DiagnosticSeverity.Warning |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function createDuplicateStringDiagnostic( |
| 27 | + stringValue: string, |
| 28 | + range: Range, |
| 29 | + scopeId: string |
| 30 | +): Diagnostic { |
| 31 | + return { |
| 32 | + message: `The string '${removeOuterQuotes(stringValue)}' is already defined in the current scope (Scope ID: ${scopeId}).`, |
| 33 | + range: range, |
| 34 | + severity: DiagnosticSeverity.Warning |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function getSyntaxNodeScopeIdentifier(syntaxNode: Parser.SyntaxNode | null): string { |
| 39 | + return syntaxNode === null |
| 40 | + ? "root" |
| 41 | + : `${syntaxNode.startPosition.row}`; |
| 42 | +} |
| 43 | + |
| 44 | +function* buildSyntaxNodeScopeMapRecursively( |
| 45 | + syntaxNode: Parser.SyntaxNode, |
| 46 | + identifier: string |
| 47 | +): Generator<[ string, Parser.SyntaxNode ], void, unknown> { |
| 48 | + const scopeIdentifier: string = syntaxNode.type === ApacheDispatcherConfigToken.ScopedProperty |
| 49 | + ? getSyntaxNodeScopeIdentifier(syntaxNode) |
| 50 | + : identifier; |
| 51 | + |
| 52 | + if ( |
| 53 | + syntaxNode.type === ApacheDispatcherConfigToken.PropertyName || |
| 54 | + syntaxNode.type === ApacheDispatcherConfigToken.String |
| 55 | + ) { |
| 56 | + yield [ scopeIdentifier, syntaxNode ]; |
| 57 | + } |
| 58 | + |
| 59 | + for (const childSyntaxNode of syntaxNode.children) { |
| 60 | + yield* buildSyntaxNodeScopeMapRecursively(childSyntaxNode, scopeIdentifier); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export function getDocumentParseTreeDiagnostics( |
| 65 | + documentParseTree: Parser.Tree | undefined |
| 66 | +): Diagnostic[] | undefined { |
| 67 | + if (documentParseTree === undefined) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + |
| 71 | + const diagnostics: Diagnostic[] = []; |
| 72 | + const rootNode: Parser.SyntaxNode = documentParseTree.rootNode; |
| 73 | + const syntaxNodeScopeMap = new Map<string, Parser.SyntaxNode[]>(); |
| 74 | + |
| 75 | + for (const [ scopeKey, node ] of buildSyntaxNodeScopeMapRecursively(rootNode, "global")) { |
| 76 | + if (!syntaxNodeScopeMap.has(scopeKey)) { |
| 77 | + syntaxNodeScopeMap.set(scopeKey, []); |
| 78 | + } |
| 79 | + |
| 80 | + syntaxNodeScopeMap.get(scopeKey)!.push(node); |
| 81 | + } |
| 82 | + |
| 83 | + syntaxNodeScopeMap.forEach(function(currentScopeSyntaxNodes: Parser.SyntaxNode[], rawScopeId: string) { |
| 84 | + const scopeIdMd5Hash: string = convertValueToMd5Hash(rawScopeId); |
| 85 | + const propertySyntaxNodeOccurrences: Map<string, Parser.SyntaxNode[]> = new Map(); |
| 86 | + const stringSyntaxNodeOccurrences: Map<string, Parser.SyntaxNode[]> = new Map(); |
| 87 | + |
| 88 | + for (const syntaxNode of currentScopeSyntaxNodes) { |
| 89 | + const syntaxNodeTextValue: string = syntaxNode.text.trim(); |
| 90 | + |
| 91 | + // Ignore properties that are just '/' as we'll assume the user hasn't finished typing |
| 92 | + if (syntaxNodeTextValue === PROPERTY_PREFIX_CHARACTER) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const currentSyntaxNodeRange: Range = getSyntaxNodeRange(syntaxNode); |
| 97 | + let syntaxNodeOccurrences: Parser.SyntaxNode[]; |
| 98 | + |
| 99 | + if (syntaxNode.type === ApacheDispatcherConfigToken.PropertyName) { |
| 100 | + syntaxNodeOccurrences = propertySyntaxNodeOccurrences.get(syntaxNodeTextValue) ?? []; |
| 101 | + syntaxNodeOccurrences.push(syntaxNode); |
| 102 | + |
| 103 | + propertySyntaxNodeOccurrences.set(syntaxNodeTextValue, syntaxNodeOccurrences); |
| 104 | + |
| 105 | + if (syntaxNodeOccurrences.length === 2) { |
| 106 | + diagnostics.push(createDuplicatePropertyDiagnostic(syntaxNodeTextValue, getSyntaxNodeRange(syntaxNodeOccurrences[0]), scopeIdMd5Hash)); |
| 107 | + } |
| 108 | + |
| 109 | + if (syntaxNodeOccurrences.length > 1) { |
| 110 | + diagnostics.push(createDuplicatePropertyDiagnostic(syntaxNodeTextValue, currentSyntaxNodeRange, scopeIdMd5Hash)); |
| 111 | + } |
| 112 | + } else if (syntaxNode.type === ApacheDispatcherConfigToken.String) { |
| 113 | + const syntaxNodeParent: Parser.SyntaxNode | null = syntaxNode.parent; |
| 114 | + |
| 115 | + if ( |
| 116 | + syntaxNodeParent !== null && |
| 117 | + syntaxNodeParent.type === ApacheDispatcherConfigToken.Property |
| 118 | + ) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + const stringValueWithoutQuotes: string = removeOuterQuotes(syntaxNodeTextValue); |
| 123 | + |
| 124 | + syntaxNodeOccurrences = stringSyntaxNodeOccurrences.get(stringValueWithoutQuotes) ?? []; |
| 125 | + syntaxNodeOccurrences.push(syntaxNode); |
| 126 | + |
| 127 | + stringSyntaxNodeOccurrences.set(stringValueWithoutQuotes, syntaxNodeOccurrences); |
| 128 | + |
| 129 | + if (syntaxNodeOccurrences.length === 2) { |
| 130 | + diagnostics.push(createDuplicateStringDiagnostic(syntaxNodeTextValue, getSyntaxNodeRange(syntaxNodeOccurrences[0]), scopeIdMd5Hash)); |
| 131 | + } |
| 132 | + |
| 133 | + if (syntaxNodeOccurrences.length > 1) { |
| 134 | + diagnostics.push(createDuplicateStringDiagnostic(syntaxNodeTextValue, currentSyntaxNodeRange, scopeIdMd5Hash)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + return diagnostics; |
| 141 | +} |
0 commit comments