forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunused-anchors.ts
More file actions
81 lines (76 loc) · 2.98 KB
/
unused-anchors.ts
File metadata and controls
81 lines (76 loc) · 2.98 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Diagnostic, DiagnosticSeverity, DiagnosticTag, Range } from 'vscode-languageserver-types';
import { isAlias, isCollection, isNode, isScalar, Node, Scalar, visit, YAMLMap, YAMLSeq, CST, Pair } from 'yaml';
import { YamlNode } from '../../jsonASTTypes';
import { SingleYAMLDocument } from '../../parser/yaml-documents';
import { AdditionalValidator } from './types';
import { isCollectionItem } from '../../../languageservice/utils/astUtils';
export class UnusedAnchorsValidator implements AdditionalValidator {
validate(document: TextDocument, yamlDoc: SingleYAMLDocument): Diagnostic[] {
const result = [];
const anchors = new Set<Scalar | YAMLMap | YAMLSeq>();
const usedAnchors = new Set<Node>();
const anchorParent = new Map<Scalar | YAMLMap | YAMLSeq, Node | Pair>();
visit(yamlDoc.internalDocument, (key, node, path) => {
if (!isNode(node)) {
return;
}
if ((isCollection(node) || isScalar(node)) && node.anchor) {
anchors.add(node);
anchorParent.set(node, path[path.length - 1] as Node);
}
if (isAlias(node)) {
usedAnchors.add(node.resolve(yamlDoc.internalDocument));
}
});
for (const anchor of anchors) {
if (!usedAnchors.has(anchor)) {
const aToken = this.getAnchorNode(anchorParent.get(anchor));
if (aToken) {
const range = Range.create(
document.positionAt(aToken.offset),
document.positionAt(aToken.offset + aToken.source.length)
);
const warningDiagnostic = Diagnostic.create(range, `Unused anchor "${aToken.source}"`, DiagnosticSeverity.Hint, 0);
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
result.push(warningDiagnostic);
}
}
}
return result;
}
private getAnchorNode(parentNode: YamlNode): CST.SourceToken | undefined {
if (parentNode && parentNode.srcToken) {
const token = parentNode.srcToken;
if (isCollectionItem(token)) {
return getAnchorFromCollectionItem(token);
} else if (CST.isCollection(token)) {
for (const t of token.items) {
const anchor = getAnchorFromCollectionItem(t);
if (anchor) {
return anchor;
}
}
}
}
return undefined;
}
}
function getAnchorFromCollectionItem(token: CST.CollectionItem): CST.SourceToken | undefined {
for (const t of token.start) {
if (t.type === 'anchor') {
return t;
}
}
if (token.sep && Array.isArray(token.sep)) {
for (const t of token.sep) {
if (t.type === 'anchor') {
return t;
}
}
}
}