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 | 10x 10x 10x 152x 152x 83x 83x 294x 294x 4x 290x 290x 290x 33x 33x 33x 5x 5x 33x 33x 33x 2x 31x 2x 33x 35x 35x 35x 2x 35x 11x 11x 11x 290x 83x 83x 83x 28x 38x 16x 16x 16x 83x | import { IToken } from '@faubulous/mentor-rdf-parsers';
import { Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver/browser';
import { LintDiagnosticsContext } from '../linter-context';
import { Linter } from '../linter';
import { getNamespaceDefinition } from '@src/utilities';
/**
* 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 Linter {
private _seenPrefixes: Record<string, boolean> = {};
private _usedPrefixes = new Set<string>();
reset(): void {
this._seenPrefixes = {};
this._usedPrefixes = new Set();
}
visitToken(context: LintDiagnosticsContext, token: IToken, index: number): Diagnostic[] {
const type = token.tokenType?.name;
if (!type || type === 'Unknown') {
return [];
}
const { document, 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: document.positionAt(u.startOffset),
end: document.positionAt(u.endOffset ?? 0)
}
});
} else if (!ns.uri.endsWith('/') && !ns.uri.endsWith('#') && !ns.uri.endsWith('_') && !ns.uri.endsWith('=') && !ns.uri.endsWith(':')) {
result.push({
severity: DiagnosticSeverity.Warning,
message: `An RDF namespace URI should end with a '/', '#', '_', '=' or ':' character.`,
range: {
start: document.positionAt(u.startOffset),
end: document.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: LintDiagnosticsContext): 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;
}
}
|