Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report unused anchors #587

Closed
remcohaszing opened this issue Nov 12, 2021 · 0 comments · Fixed by #621
Closed

Report unused anchors #587

remcohaszing opened this issue Nov 12, 2021 · 0 comments · Fixed by #621
Assignees
Milestone

Comments

@remcohaszing
Copy link
Contributor

Is your enhancement related to a problem? Please describe.

If an anchor isn’t referenced, it’s unused. The language server protocol explicitly supports unused ranges using the Diagnostic#tags. Monaco editor / VS Code also support this by rendering them as faded.

Describe the solution you would like

I would like the yaml validation to report unused anchors in addition to JSON schema validation errors.

I suggest to report them using a diagnostic with the DiagnosticTag.Deprecated tag without a severity.

Describe alternatives you have considered

N/A

Additional context

I already fiddled a bit myself, but I couldn’t find a way to get the anchor range.

import { visit, isNode, Node, isAlias } from 'yaml';

// Added in https://github.com/redhat-developer/yaml-language-server/blob/13adb0e02199ce673e582bb5387577f9936a66a1/src/languageservice/services/yamlValidation.ts#L89
const anchors = new Set<Node>();
const usedAnchors = new Set<Node>();
visit(currentYAMLDoc.internalDocument, (key, node) => {
  if (!isNode(node)) {
    return;
  }
  if (node.anchor) {
    anchors.add(node);
  }
  if (isAlias(node)) {
    usedAnchors.add(node.resolve(currentYAMLDoc.internalDocument));
  }
});
for (const anchor of anchors) {
  if (!usedAnchors.has(anchor)) {
    console.dir(anchor);
  }
}

This finds unused anchors, but I don’t know if this is the right approach. I hope it helps.

@remcohaszing remcohaszing changed the title Report unused aliases Report unused anchors Nov 12, 2021
@evidolob evidolob added this to the 1.3.0 milestone Dec 1, 2021
@evidolob evidolob self-assigned this Dec 13, 2021
@evidolob evidolob modified the milestones: 1.3.0, 1.4.0 Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants