All files / languages/linters deprecated-workspace-uri-linter.ts

100% Statements 9/9
83.33% Branches 5/6
100% Functions 1/1
100% Lines 9/9

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                  20x           20x               223x 162x     61x   61x 57x     4x   4x                        
import { IToken, RdfToken } from '@faubulous/mentor-rdf-parsers';
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver/browser';
import { LintDiagnosticsContext } from '../linter-context';
import { Linter } from '../linter';
import { getIriFromIriReference } from '@src/utilities';
 
/**
 * The diagnostic code for deprecated `workspace:/path` URIs.
 */
export const DEPRECATED_WORKSPACE_URI_CODE = 'DeprecatedWorkspaceUri';
 
/**
 * Matches the deprecated `workspace:/path` format (single slash) but NOT the
 * canonical `workspace:///path` format (triple slash).
 */
const DEPRECATED_URI_REGEX = /^workspace:\/(?!\/)/;
 
/**
 * Detects deprecated `workspace:/path` URIs in IRIREF tokens and suggests
 * replacing them with `workspace:///path`.
 */
export class DeprecatedWorkspaceUriLinter implements Linter {
	visitToken(context: LintDiagnosticsContext, token: IToken, _index: number): Diagnostic[] {
		if (token.tokenType?.name !== RdfToken.IRIREF.name) {
			return [];
		}
 
		const iri = getIriFromIriReference(token.image);
 
		if (!DEPRECATED_URI_REGEX.test(iri)) {
			return [];
		}
 
		const canonical = iri.replace(/^workspace:\/(?!\/)/, 'workspace:///');
 
		return [{
			code: DEPRECATED_WORKSPACE_URI_CODE,
			severity: DiagnosticSeverity.Warning,
			message: `Deprecated workspace URI scheme. Use '${canonical}' instead.`,
			source: 'Mentor',
			range: {
				start: context.document.positionAt(token.startOffset),
				end: context.document.positionAt((token.endOffset ?? token.startOffset) + 1),
			}
		}];
	}
}