Skip to content

Commit b98571b

Browse files
authored
Improve positioning of unexpected property errors (#64)
1 parent 091340e commit b98571b

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

language-service/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#### 0.5.7
33
Allow boolean values to validate against string schema [#PR-62](https://github.com/microsoft/azure-pipelines-language-server/pull/62)
44
Remove consideration of firstProperty schema element when generating errors [#PR-63](https://github.com/microsoft/azure-pipelines-language-server/pull/63)
5+
Improve positioning of unexpected property errors [#PR-64](https://github.com/microsoft/azure-pipelines-language-server/pull/64)
56

67
#### 0.5.6
78
Cache schemas when using a custom schema provider to improve performance [#PR-60](https://github.com/Microsoft/azure-pipelines-language-server/pull/60)

language-service/src/parser/jsonParser.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ export class ObjectASTNode extends ASTNode {
914914

915915
if (typeof schema.additionalProperties === 'object') {
916916
unprocessedProperties.forEach((propertyName: string) => {
917-
let child = seenKeys[propertyName];
917+
const child = seenKeys[propertyName];
918918
if (child) {
919919
let propertyValidationResult = new ValidationResult();
920920
child.validate(<any>schema.additionalProperties, propertyValidationResult, matchingSchemas);
@@ -927,19 +927,44 @@ export class ObjectASTNode extends ASTNode {
927927
//Auto-complete can insert a "holder" node when parsing, do not count it as an error
928928
//against additionalProperties
929929
if (propertyName !== nodeHolder) {
930-
let child = seenKeys[propertyName];
930+
const child: ASTNode = seenKeys[propertyName];
931931
if (child) {
932-
let propertyNode = null;
933-
if(child.type !== "property"){
934-
propertyNode = <PropertyASTNode>child.parent;
935-
if(propertyNode.type === "object"){
936-
propertyNode = propertyNode.properties[0];
932+
let errorLocation: IRange = null;
933+
let errorNode: ASTNode = child;
934+
935+
if (errorNode.type !== "property" && errorNode.parent) {
936+
if (errorNode.parent.type === "property") {
937+
//This works for StringASTNode
938+
errorNode = errorNode.parent;
939+
} else if (errorNode.parent.type === "object") {
940+
//The tree structure and parent links can be weird
941+
//NullASTNode's parent will be the object and not the property
942+
const parentObject: ObjectASTNode = <ObjectASTNode>errorNode.parent;
943+
parentObject.properties.some((propNode: PropertyASTNode) => {
944+
if (propNode.value == child) {
945+
errorNode = propNode;
946+
return true;
947+
}
948+
return false;
949+
});
937950
}
938-
}else{
939-
propertyNode = child;
940951
}
952+
953+
if (errorNode.type === "property") {
954+
const propertyNode: PropertyASTNode = <PropertyASTNode>errorNode;
955+
errorLocation = {
956+
start: propertyNode.key.start,
957+
end: propertyNode.key.end
958+
};
959+
} else {
960+
errorLocation = {
961+
start: errorNode.start,
962+
end: errorNode.end
963+
};
964+
}
965+
941966
validationResult.addProblem({
942-
location: { start: propertyNode.key.start, end: propertyNode.key.end },
967+
location: errorLocation,
943968
severity: ProblemSeverity.Warning,
944969
getMessage: () => schema.errorMessage || localize('DisallowedExtraPropWarning', 'Unexpected property {0}', propertyName)
945970
});

0 commit comments

Comments
 (0)