|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 7 | +import { Diagnostic, DiagnosticSeverity, DiagnosticTag, Range } from 'vscode-languageserver-types'; |
| 8 | +import { isAlias, isCollection, isNode, isScalar, Node, Scalar, visit, YAMLMap, YAMLSeq, CST, Pair } from 'yaml'; |
| 9 | +import { YamlNode } from '../../jsonASTTypes'; |
| 10 | +import { SingleYAMLDocument } from '../../parser/yaml-documents'; |
| 11 | +import { AdditionalValidator } from './types'; |
| 12 | +import { isCollectionItem } from '../../../languageservice/utils/astUtils'; |
| 13 | + |
| 14 | +export class UnusedAnchorsValidator implements AdditionalValidator { |
| 15 | + validate(document: TextDocument, yamlDoc: SingleYAMLDocument): Diagnostic[] { |
| 16 | + const result = []; |
| 17 | + const anchors = new Set<Scalar | YAMLMap | YAMLSeq>(); |
| 18 | + const usedAnchors = new Set<Node>(); |
| 19 | + const anchorParent = new Map<Scalar | YAMLMap | YAMLSeq, Node | Pair>(); |
| 20 | + |
| 21 | + visit(yamlDoc.internalDocument, (key, node, path) => { |
| 22 | + if (!isNode(node)) { |
| 23 | + return; |
| 24 | + } |
| 25 | + if ((isCollection(node) || isScalar(node)) && node.anchor) { |
| 26 | + anchors.add(node); |
| 27 | + anchorParent.set(node, path[path.length - 1] as Node); |
| 28 | + } |
| 29 | + if (isAlias(node)) { |
| 30 | + usedAnchors.add(node.resolve(yamlDoc.internalDocument)); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + for (const anchor of anchors) { |
| 35 | + if (!usedAnchors.has(anchor)) { |
| 36 | + const aToken = this.getAnchorNode(anchorParent.get(anchor)); |
| 37 | + if (aToken) { |
| 38 | + const range = Range.create( |
| 39 | + document.positionAt(aToken.offset), |
| 40 | + document.positionAt(aToken.offset + aToken.source.length) |
| 41 | + ); |
| 42 | + const warningDiagnostic = Diagnostic.create(range, `Unused anchor "${aToken.source}"`, DiagnosticSeverity.Hint, 0); |
| 43 | + warningDiagnostic.tags = [DiagnosticTag.Unnecessary]; |
| 44 | + warningDiagnostic.data = { name: aToken.source }; |
| 45 | + result.push(warningDiagnostic); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + private getAnchorNode(parentNode: YamlNode): CST.SourceToken | undefined { |
| 53 | + if (parentNode && parentNode.srcToken) { |
| 54 | + const token = parentNode.srcToken; |
| 55 | + if (isCollectionItem(token)) { |
| 56 | + return getAnchorFromCollectionItem(token); |
| 57 | + } else if (CST.isCollection(token)) { |
| 58 | + for (const t of token.items) { |
| 59 | + const anchor = getAnchorFromCollectionItem(t); |
| 60 | + if (anchor) { |
| 61 | + return anchor; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return undefined; |
| 67 | + } |
| 68 | +} |
| 69 | +function getAnchorFromCollectionItem(token: CST.CollectionItem): CST.SourceToken | undefined { |
| 70 | + for (const t of token.start) { |
| 71 | + if (t.type === 'anchor') { |
| 72 | + return t; |
| 73 | + } |
| 74 | + } |
| 75 | + if (token.sep && Array.isArray(token.sep)) { |
| 76 | + for (const t of token.sep) { |
| 77 | + if (t.type === 'anchor') { |
| 78 | + return t; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments