Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/languageservice/services/validation/unused-anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
const result = [];
const anchors = new Set<Scalar | YAMLMap | YAMLSeq>();
const usedAnchors = new Set<Node>();
const unIdentifiedAlias = new Set<Node>();
const anchorParent = new Map<Scalar | YAMLMap | YAMLSeq, Node | Pair>();

visit(yamlDoc.internalDocument, (key, node, path) => {
Expand All @@ -27,7 +28,11 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
anchorParent.set(node, path[path.length - 1] as Node);
}
if (isAlias(node)) {
usedAnchors.add(node.resolve(yamlDoc.internalDocument));
if (!node.resolve(yamlDoc.internalDocument)) {
unIdentifiedAlias.add(node);
} else {
usedAnchors.add(node.resolve(yamlDoc.internalDocument));
}
}
});

Expand All @@ -46,6 +51,17 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
}
}

unIdentifiedAlias.forEach((node) => {
const nodeRange = node.range;
if (nodeRange) {
const startOffset = nodeRange[0];
const endOffset = nodeRange[1];
const range = Range.create(document.positionAt(startOffset), document.positionAt(endOffset));
const warningDiagnostic = Diagnostic.create(range, `Unresolved alias "${node}"`, DiagnosticSeverity.Hint, 0);
Comment thread
datho7561 marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also maybe worth adding "it will be treated as a string instead" into the error message

warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
result.push(warningDiagnostic);
}
});
return result;
}
private getAnchorNode(parentNode: YamlNode, node: Node): CST.SourceToken | undefined {
Expand Down
12 changes: 11 additions & 1 deletion test/yamlValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ some:
ee: *g`;
const result = await parseSetup(yaml);
expect(result).is.not.empty;
expect(result.length).to.be.equal(4);
expect(result.length).to.be.equal(5);
expect(result).to.include.deep.members([
createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9),
createUnusedAnchorDiagnostic('Unused anchor "&a"', 4, 2, 4, 4),
Expand All @@ -94,6 +94,16 @@ ee: *g`;
});
});

describe('Unresolved alias diagnostics', () => {
it('should report unresolved alias', async () => {
const yaml = 'foo: *bar';
const result = await parseSetup(yaml);
expect(result).is.not.empty;
expect(result.length).to.be.equal(1);
expect(result[0]).deep.equal(createUnusedAnchorDiagnostic('Unresolved alias "*bar"', 0, 5, 0, 9));
});
});

describe(`YAML styles test`, () => {
it('should not report flow style', async () => {
const yaml = `host: phl-42
Expand Down