Skip to content

Commit e2bef3c

Browse files
Merge branch 'main' into 1112-remove-trailing-comma
2 parents 104bf8a + 4a5c6ca commit e2bef3c

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/languageservice/services/validation/unused-anchors.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
1616
const result = [];
1717
const anchors = new Set<Scalar | YAMLMap | YAMLSeq>();
1818
const usedAnchors = new Set<Node>();
19+
const unIdentifiedAlias = new Set<Node>();
1920
const anchorParent = new Map<Scalar | YAMLMap | YAMLSeq, Node | Pair>();
2021

2122
visit(yamlDoc.internalDocument, (key, node, path) => {
@@ -27,7 +28,11 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
2728
anchorParent.set(node, path[path.length - 1] as Node);
2829
}
2930
if (isAlias(node)) {
30-
usedAnchors.add(node.resolve(yamlDoc.internalDocument));
31+
if (!node.resolve(yamlDoc.internalDocument)) {
32+
unIdentifiedAlias.add(node);
33+
} else {
34+
usedAnchors.add(node.resolve(yamlDoc.internalDocument));
35+
}
3136
}
3237
});
3338

@@ -39,13 +44,29 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
3944
document.positionAt(aToken.offset),
4045
document.positionAt(aToken.offset + aToken.source.length)
4146
);
42-
const warningDiagnostic = Diagnostic.create(range, `Unused anchor "${aToken.source}"`, DiagnosticSeverity.Hint, 0);
47+
const warningDiagnostic = Diagnostic.create(
48+
range,
49+
`Unused anchor "${aToken.source}"`,
50+
DiagnosticSeverity.Information,
51+
0
52+
);
4353
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
4454
result.push(warningDiagnostic);
4555
}
4656
}
4757
}
4858

59+
unIdentifiedAlias.forEach((node) => {
60+
const nodeRange = node.range;
61+
if (nodeRange) {
62+
const startOffset = nodeRange[0];
63+
const endOffset = nodeRange[1];
64+
const range = Range.create(document.positionAt(startOffset), document.positionAt(endOffset));
65+
const warningDiagnostic = Diagnostic.create(range, `Unresolved alias "${node}"`, DiagnosticSeverity.Information, 0);
66+
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
67+
result.push(warningDiagnostic);
68+
}
69+
});
4970
return result;
5071
}
5172
private getAnchorNode(parentNode: YamlNode, node: Node): CST.SourceToken | undefined {

test/utils/verifyError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function createUnusedAnchorDiagnostic(
6969
startCharacter,
7070
endLine,
7171
endCharacter,
72-
DiagnosticSeverity.Hint,
72+
DiagnosticSeverity.Information,
7373
'YAML'
7474
);
7575
diagnostic.tags = [DiagnosticTag.Unnecessary];

test/yamlValidation.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ some:
8484
ee: *g`;
8585
const result = await parseSetup(yaml);
8686
expect(result).is.not.empty;
87-
expect(result.length).to.be.equal(4);
87+
expect(result.length).to.be.equal(5);
8888
expect(result).to.include.deep.members([
8989
createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9),
9090
createUnusedAnchorDiagnostic('Unused anchor "&a"', 4, 2, 4, 4),
@@ -94,6 +94,16 @@ ee: *g`;
9494
});
9595
});
9696

97+
describe('Unresolved alias diagnostics', () => {
98+
it('should report unresolved alias', async () => {
99+
const yaml = 'foo: *bar';
100+
const result = await parseSetup(yaml);
101+
expect(result).is.not.empty;
102+
expect(result.length).to.be.equal(1);
103+
expect(result[0]).deep.equal(createUnusedAnchorDiagnostic('Unresolved alias "*bar"', 0, 5, 0, 9));
104+
});
105+
});
106+
97107
describe(`YAML styles test`, () => {
98108
it('should not report flow style', async () => {
99109
const yaml = `host: phl-42

0 commit comments

Comments
 (0)