All files / services/validation shacl-diagnostics-mapper.ts

96.55% Statements 84/87
86.11% Branches 62/72
100% Functions 12/12
96.51% Lines 83/86

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249                                    27x   27x 27x 27x 27x   27x 27x 27x 27x       27x     27x                     27x   27x 4x       23x         23x     23x 7x   7x 3x         20x 10x   10x 5x         15x 14x   14x 14x   14x         1x                 27x   5x 6x         5x               4x 4x 4x   4x 4x 4x   4x 4x   4x             23x   23x               23x 1x         22x 29x       29x   29x 4x       22x               17x 5x     12x 18x     17x 4x     8x   8x             27x 25x     2x 1x     1x 1x                   27x 1x     26x 26x   26x   26x 15x     26x 11x     26x             68x   68x 56x     12x   12x 12x            
import * as vscode from 'vscode';
import { SH } from '@faubulous/mentor-rdf';
import { QuadContext, IToken } from '@faubulous/mentor-rdf-parsers';
import { countLeadingWhitespace, countTrailingWhitespace } from '@src/utilities';
import { IDocumentContext } from '@src/services/document/document-context.interface';
import { ShaclValidationResult, ShaclValidationResultEntry } from './shacl-validation-service';
 
/**
 * Maps SHACL validation results to VS Code diagnostics.
 */
export class ShaclDiagnosticsMapper {
	/**
	 * Map a SHACL validation result to VS Code diagnostics.
	 * @param result The validation result.
	 * @param context The document context for resolving focus node positions.
	 * @returns An array of VS Code diagnostics.
	 */
	mapToDiagnostics(result: ShaclValidationResult, context: IDocumentContext, quadContexts?: QuadContext[]): vscode.Diagnostic[] {
		const diagnostics: vscode.Diagnostic[] = [];
 
		for (const entry of result.results) {
			const range = this._resolveRange(entry, context, quadContexts);
			const severity = this._mapSeverity(entry.severity);
			const message = this._buildMessage(entry);
 
			const diagnostic = new vscode.Diagnostic(range, message, severity);
			diagnostic.source = 'SHACL';
			diagnostic.code = this._extractLocalName(entry.constraintComponent);
			(diagnostic as vscode.Diagnostic & { data?: { focusNode: string } }).data = {
				focusNode: entry.focusNode,
			};
 
			diagnostics.push(diagnostic);
		}
 
		return diagnostics;
	}
 
	/**
	 * Resolve the document range for a validation result entry.
	 * Prefers highlighting the offending predicate or value over the focus node.
	 */
	private _resolveRange(entry: ShaclValidationResultEntry, context: IDocumentContext, quadContexts?: QuadContext[]): vscode.Range {
		// 0. Try to resolve the exact object token position from quad contexts.
		//    This handles cases where the same subject appears on multiple non-contiguous
		//    lines (e.g. repeated predicates), which defeats the heuristic anchor window.
		const fromQuad = this._resolveRangeFromQuadContext(entry, quadContexts);
 
		if (fromQuad) {
			return fromQuad;
		}
 
		// Determine the focus node's start line so we can anchor relative lookups.
		const focusNodeStartLine = this._getFocusNodeStartLine(entry.focusNode, context);
 
		// Determine where the next subject starts so we can scope lookups
		// to only the focus node's block (avoids picking up predicates that
		// belong to a different subject further down in the document).
		const nextSubjectStartLine = this._getNextSubjectStartLine(focusNodeStartLine, context);
 
		// 1. Try the value IRI (most specific).
		if (entry.value) {
			const r = this._firstRangeAfterLine(context.references[entry.value], focusNodeStartLine, nextSubjectStartLine);
 
			if (r) {
				return r;
			}
		}
 
		// 2. Try the predicate path.
		if (entry.path) {
			const r = this._firstRangeAfterLine(context.references[entry.path], focusNodeStartLine, nextSubjectStartLine);
 
			if (r) {
				return r;
			}
		}
 
		// 3. Fall back to the focus node subject position.
		if (focusNodeStartLine !== undefined) {
			const allRanges = context.subjects[entry.focusNode] ?? context.references[entry.focusNode];
 
			Eif (allRanges?.length) {
				const r = allRanges[0];
 
				return new vscode.Range(r.start.line, r.start.character, r.end.line, r.end.character);
			}
		}
 
		// 4. Last resort: top of the file.
		return new vscode.Range(0, 0, 0, 0);
	}
 
	/**
	 * Resolves the range for a validation result entry by finding the exact matching
	 * quad in the quad context list and returning the range of its object token.
	 * Returns undefined if no quad context is available or no match is found.
	 */
	private _resolveRangeFromQuadContext(entry: ShaclValidationResultEntry, quadContexts?: QuadContext[]): vscode.Range | undefined {
		if (!entry.value || !entry.path || !quadContexts?.length) return undefined;
 
		const match = quadContexts.find(qc =>
			qc.subject.value === entry.focusNode &&
			qc.predicate.value === entry.path &&
			qc.object.value === entry.value
		);
 
		return match ? this._rangeFromToken(match.objectToken) : undefined;
	}
 
	/**
	 * Converts a 1-based chevrotain IToken to a 0-based VS Code Range,
	 * adjusting for any leading/trailing whitespace in the token image.
	 */
	private _rangeFromToken(token: IToken): vscode.Range {
		const startLine = token.startLine ? token.startLine - 1 : 0;
		const startCharacter = token.startColumn ? token.startColumn - 1 : 0;
		const startWhitespace = countLeadingWhitespace(token.image);
 
		const endLine = token.endLine ? token.endLine - 1 : 0;
		const endCharacter = token.endColumn ? token.endColumn - 1 : 0;
		const endWhitespace = countTrailingWhitespace(token.image);
 
		const start = new vscode.Position(startLine, startCharacter + startWhitespace);
		const end = new vscode.Position(endLine, endCharacter - endWhitespace + 1);
 
		return new vscode.Range(start, end);
	}
 
	/**
	 * Returns the start line of the focus node in the document, or undefined if not found.
	 */
	private _getFocusNodeStartLine(focusNode: string, context: IDocumentContext): number | undefined {
		const ranges = context.subjects[focusNode] ?? context.references[focusNode];
 
		return ranges?.length ? ranges[0].start.line : undefined;
	}
 
	/**
	 * Returns the smallest subject start line that is strictly greater than
	 * the given focus node start line, or undefined if no such subject exists.
	 */
	private _getNextSubjectStartLine(focusNodeStartLine: number | undefined, context: IDocumentContext): number | undefined {
		if (focusNodeStartLine === undefined) {
			return undefined;
		}
 
		let nearest: number | undefined;
 
		for (const ranges of Object.values(context.subjects)) {
			Iif (!ranges?.length) {
				continue;
			}
 
			const line = ranges[0].start.line;
 
			if (line > focusNodeStartLine && (nearest === undefined || line < nearest)) {
				nearest = line;
			}
		}
 
		return nearest;
	}
 
	/**
	 * Returns the first range from the given array whose start line is >= anchorLine
	 * and < beforeLine (if given). Returns undefined if no matching range exists.
	 */
	private _firstRangeAfterLine(ranges: import('vscode-languageserver-types').Range[] | undefined, anchorLine: number | undefined, beforeLine?: number): vscode.Range | undefined {
		if (!ranges?.length) {
			return undefined;
		}
 
		const candidates = anchorLine !== undefined
			? ranges.filter(r => r.start.line >= anchorLine && (beforeLine === undefined || r.start.line < beforeLine))
			: ranges;
 
		if (!candidates.length) {
			return undefined;
		}
 
		const r = candidates[0];
 
		return new vscode.Range(r.start.line, r.start.character, r.end.line, r.end.character);
	}
 
	/**
	 * Map a SHACL severity IRI to a VS Code diagnostic severity.
	 */
	private _mapSeverity(severity: string): vscode.DiagnosticSeverity {
		if (severity === SH.Violation) {
			return vscode.DiagnosticSeverity.Error;
		}
 
		if (severity === SH.Warning) {
			return vscode.DiagnosticSeverity.Warning;
		}
 
		Eif (severity === SH.Info) {
			return vscode.DiagnosticSeverity.Information;
		}
 
		return vscode.DiagnosticSeverity.Error;
	}
 
	/**
	 * Build a human-readable message from a validation result entry.
	 */
	private _buildMessage(entry: ShaclValidationResultEntry): string {
		if (entry.messages.length > 0) {
			return entry.messages.join('; ');
		}
 
		const parts: string[] = [];
		const component = this._extractLocalName(entry.constraintComponent);
		
		parts.push(`Constraint violation: ${component}`);
 
		if (entry.path) {
			parts.push(`Path: ${this._extractLocalName(entry.path)}`);
		}
 
		if (entry.value) {
			parts.push(`Value: ${entry.value}`);
		}
 
		return parts.join(' | ');
	}
 
	/**
	 * Extract the local name from an IRI.
	 */
	private _extractLocalName(iri: string): string {
		const hashIndex = iri.lastIndexOf('#');
 
		if (hashIndex >= 0) {
			return iri.substring(hashIndex + 1);
		}
 
		const slashIndex = iri.lastIndexOf('/');
 
		Eif (slashIndex >= 0) {
			return iri.substring(slashIndex + 1);
		}
 
		return iri;
	}
}