All files / languages/sparql sparql-language-server.ts

100% Statements 61/61
87.8% Branches 36/41
100% Functions 4/4
100% Lines 60/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                                                11x         9x     9x   9x               9x 9x   9x 9x 9x   9x 55x   55x 1x         54x         8x             8x   8x 8x 8x   8x       2x 2x   2x     8x     8x 1x 1x   8x     8x     8x 1x 1x   8x         14x 14x 14x     14x 5x   5x 1x       14x 8x     14x   14x       7x 7x 7x   7x           9x 7x   7x     9x             8x     8x 2x     6x     6x 2x   2x                         6x      
import { Connection, Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver/browser';
import { SparqlLexer, SparqlParser, IToken, RdfToken } from '@faubulous/mentor-rdf-parsers';
import { LanguageServerBase } from '@src/languages/language-server';
import { TextDocument } from 'vscode-languageserver-textdocument';
 
/**
 * Represents information about a SPARQL query scope (SELECT, CONSTRUCT, etc.).
 */
interface QueryScope {
	/**
	 * Whether this scope uses SELECT * (star select).
	 */
	isStarSelect: boolean;
 
	/**
	 * The depth of curly braces when this scope was created.
	 */
	depth: number;
 
	/**
	 * Variables and their occurrence tokens within this scope.
	 */
	variables: Map<string, IToken[]>;
 
	/**
	 * Variables that are projection targets in the SELECT clause (AS ?x).
	 */
	projectionVariables: Set<string>;
}
 
export class SparqlLanguageServer extends LanguageServerBase {
	constructor(connection: Connection) {
		super(connection, 'sparql', 'SPARQL', new SparqlLexer(), new SparqlParser(), true);
	}
 
	override getLintDiagnostics(document: TextDocument, content: string, tokens: IToken[]): Diagnostic[] {
		// Get base diagnostics from parent class (prefix checks, etc.)
		const result = super.getLintDiagnostics(document, content, tokens);
 
		// Add SPARQL-specific unused variable diagnostics
		result.push(...this._getUnusedVariableDiagnostics(document, tokens));
 
		return result;
	}
 
	/**
	 * Get diagnostics for unused variables in SPARQL queries.
	 * A variable is considered unused if it appears only once and the query doesn't use SELECT *.
	 */
	private _getUnusedVariableDiagnostics(document: TextDocument, tokens: IToken[]): Diagnostic[] {
		const diagnostics: Diagnostic[] = [];
		const scopeStack: QueryScope[] = [];
 
		let currentDepth = 0;
		let expectingSelectClause = false;
		let inSelectClause = false;
 
		for (let i = 0; i < tokens.length; i++) {
			const token = tokens[i];
 
			if (!token.tokenType) {
				continue;
			}
 
			// Only check the variable usage in SELECT/CONSTRUCT/DESCRIBE queries.
			// In ASK queries, all variables are considered used.
			switch (token.tokenType.name) {
				case RdfToken.SELECT.name:
				case RdfToken.CONSTRUCT.name:
				case RdfToken.DESCRIBE.name: {
					// Start tracking a new query scope
					const newScope: QueryScope = {
						isStarSelect: false,
						depth: currentDepth,
						variables: new Map(),
						projectionVariables: new Set()
					};
 
					scopeStack.push(newScope);
 
					Eif (token.tokenType.name === RdfToken.SELECT.name) {
						expectingSelectClause = true;
						inSelectClause = true;
					}
					break;
				}
				case RdfToken.STAR.name: {
					// Check if this is a SELECT * (star in select clause)
					Eif (inSelectClause && scopeStack.length > 0) {
						scopeStack[scopeStack.length - 1].isStarSelect = true;
					}
					break;
				}
				case RdfToken.LCURLY.name: {
					currentDepth++;
 
					// End of SELECT clause when we hit the first curly brace
					if (expectingSelectClause) {
						expectingSelectClause = false;
						inSelectClause = false;
					}
					break;
				}
				case RdfToken.RCURLY.name: {
					currentDepth--;
 
					// Check if any scopes should be closed
					while (scopeStack.length > 0 && scopeStack[scopeStack.length - 1].depth > currentDepth) {
						const closedScope = scopeStack.pop()!;
						diagnostics.push(...this._checkScopeForUnusedVariables(document, closedScope));
					}
					break;
				}
				case RdfToken.VAR1.name:
				case RdfToken.VAR2.name: {
					// Track variable occurrences in the current scope
					Eif (scopeStack.length > 0) {
						const currentScope = scopeStack[scopeStack.length - 1];
						const varName = token.image;
 
						// Check if this variable is a projection target (preceded by AS in SELECT clause)
						if (inSelectClause && i > 0) {
							const prevToken = tokens[i - 1];
 
							if (prevToken?.tokenType?.name === RdfToken.AS_KW.name) {
								currentScope.projectionVariables.add(varName);
							}
						}
 
						if (!currentScope.variables.has(varName)) {
							currentScope.variables.set(varName, []);
						}
 
						currentScope.variables.get(varName)!.push(token);
					}
					break;
				}
				case RdfToken.WHERE.name: {
					// End of SELECT clause
					Eif (expectingSelectClause) {
						expectingSelectClause = false;
						inSelectClause = false;
					}
					break;
				}
			}
		}
 
		// Check any remaining scopes at end of document
		while (scopeStack.length > 0) {
			const closedScope = scopeStack.pop()!;
 
			diagnostics.push(...this._checkScopeForUnusedVariables(document, closedScope));
		}
 
		return diagnostics;
	}
 
	/**
	 * Check a query scope for unused variables and return diagnostics.
	 */
	private _checkScopeForUnusedVariables(document: TextDocument, scope: QueryScope): Diagnostic[] {
		const diagnostics: Diagnostic[] = [];
 
		// Don't report unused variables if this is a SELECT * query
		if (scope.isStarSelect) {
			return diagnostics;
		}
 
		for (const [varName, occurrences] of scope.variables) {
			// A variable that appears only once is considered unused,
			// unless it's a projection target in the SELECT clause (AS ?x)
			if (occurrences.length === 1 && !scope.projectionVariables.has(varName)) {
				const token = occurrences[0];
 
				diagnostics.push({
					code: 'UnusedVariableHint',
					severity: DiagnosticSeverity.Hint,
					tags: [DiagnosticTag.Unnecessary],
					message: `Variable '${varName}' is used only once.`,
					range: {
						start: document.positionAt(token.startOffset),
						end: document.positionAt((token.endOffset ?? token.startOffset) + 1)
					}
				});
			}
		}
 
		return diagnostics;
	}
}