All files / providers/linting/linters namespace-prefix-linter.ts

100% Statements 42/42
82.14% Branches 23/28
100% Functions 4/4
100% Lines 41/41

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                10x         10x         10x           52x 52x     45x 45x       289x   289x 2x     287x 287x   287x     45x   45x 45x 3x   3x                     45x   45x   45x 1x                       45x     46x 46x   46x 1x     46x     76x   76x   76x       287x       45x 45x   45x 42x 25x   9x 9x   9x                           45x      
import { IToken } from '@faubulous/mentor-rdf-parsers';
import { Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver-types';
import { getNamespaceDefinition } from '@src/utilities';
import { LintingContext, LintingProvider } from '..';
 
/**
 * The diagnostic code for duplicate namespace prefix declarations.
 */
export const DUPLICATE_PREFIX_CODE = 'DuplicatePrefix';
 
/**
 * The diagnostic code for invalid namespace URI declarations.
 */
export const INVALID_NAMESPACE_URI_CODE = 'InvalidNamespaceUri';
 
/**
 * The diagnostic code for unused namespace prefix declarations.
 */
export const UNUSED_NAMESPACE_PREFIX_CODE = 'UnusedNamespacePrefixHint';
 
/**
 * Detects duplicate prefix declarations, invalid namespace URIs, and unused prefixes.
 */
export class NamespacePrefixLinter implements LintingProvider {
	private _seenPrefixes: Record<string, boolean> = {};
	private _usedPrefixes = new Set<string>();
 
	reset(): void {
		this._seenPrefixes = {};
		this._usedPrefixes = new Set();
	}
 
	visitToken(context: LintingContext, token: IToken, index: number): Diagnostic[] {
		const type = token.tokenType?.name;
 
		if (!type || type === 'Unknown') {
			return [];
		}
 
		const { positionAt, tokens } = context;
		const result: Diagnostic[] = [];
 
		switch (type) {
			case 'PREFIX':
			case 'TTL_PREFIX': {
				const ns = getNamespaceDefinition(tokens, token);
 
				Eif (ns) {
					if (this._seenPrefixes[ns.prefix]) {
						const n = token.startLine ? token.startLine - 1 : 0;
 
						result.push({
							code: DUPLICATE_PREFIX_CODE,
							severity: DiagnosticSeverity.Warning,
							message: `The prefix '${ns.prefix}' is already defined.`,
							range: {
								start: { line: n, character: 0 },
								end: { line: n, character: Number.MAX_SAFE_INTEGER },
							}
						});
					}
 
					this._seenPrefixes[ns.prefix] = true;
 
					const u = tokens[index + 2];
 
					if (ns.uri === '') {
						result.push({
							code: INVALID_NAMESPACE_URI_CODE,
							severity: DiagnosticSeverity.Error,
							message: `Invalid namespace URI.`,
							range: {
								start: positionAt(u.startOffset),
								end: positionAt(u.endOffset ?? 0)
							}
						});
					}
				}
 
				break;
			}
			case 'PNAME_NS': {
				const prefix = token.image.split(':')[0];
				const previousType = tokens[index - 1]?.tokenType?.name;
 
				if (previousType !== 'PREFIX' && previousType !== 'TTL_PREFIX') {
					this._usedPrefixes.add(prefix);
				}
 
				break;
			}
			case 'PNAME_LN': {
				const prefix = token.image.split(':')[0];
 
				this._usedPrefixes.add(prefix);
 
				break;
			}
		}
 
		return result;
	}
 
	finalize(context: LintingContext): Diagnostic[] {
		const result: Diagnostic[] = [];
		const { tokens } = context;
 
		for (const prefix of Object.keys(this._seenPrefixes)) {
			if (!this._usedPrefixes.has(prefix)) {
				const prefixToken = tokens.find(t => t.image === `${prefix}:`);
 
				Eif (prefixToken) {
					const n = prefixToken.startLine ? prefixToken.startLine - 1 : 0;
 
					result.push({
						code: UNUSED_NAMESPACE_PREFIX_CODE,
						severity: DiagnosticSeverity.Hint,
						tags: [DiagnosticTag.Unnecessary],
						message: `Prefix '${prefix}' is declared but never used.`,
						range: {
							start: { line: n, character: 0 },
							end: { line: n, character: Number.MAX_SAFE_INTEGER },
						}
					});
				}
			}
		}
 
		return result;
	}
}