Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 23 additions & 2 deletions 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 @@ -39,13 +44,29 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
document.positionAt(aToken.offset),
document.positionAt(aToken.offset + aToken.source.length)
);
const warningDiagnostic = Diagnostic.create(range, `Unused anchor "${aToken.source}"`, DiagnosticSeverity.Hint, 0);
const warningDiagnostic = Diagnostic.create(
range,
`Unused anchor "${aToken.source}"`,
DiagnosticSeverity.Information,
0
);
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
result.push(warningDiagnostic);
}
}
}

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.Information, 0);
Comment thread
datho7561 marked this conversation as resolved.
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
result.push(warningDiagnostic);
}
});
return result;
}
private getAnchorNode(parentNode: YamlNode, node: Node): CST.SourceToken | undefined {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/verifyError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function createUnusedAnchorDiagnostic(
startCharacter,
endLine,
endCharacter,
DiagnosticSeverity.Hint,
DiagnosticSeverity.Information,
'YAML'
);
diagnostic.tags = [DiagnosticTag.Unnecessary];
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