Skip to content

Commit

Permalink
fix: fix locations of YAML parse errors at the end of the source
Browse files Browse the repository at this point in the history
We previously generated locations where the start position was after the
end position.
  • Loading branch information
Yogu committed Jan 15, 2025
1 parent 132f34f commit 23fae71
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/schema/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,28 @@ function parseYAMLSource(

root.errors.forEach((error) => {
const severity = error.isWarning ? Severity.WARNING : Severity.ERROR;
const endPos = getLineEndPosition(error.mark.line + 1, projectSource);

// Errors reported do not have an end, only a start. We just choose the line end as the end.
// While there is a toLineEnd boolean, we ignore this, because what else would we really do?
let location: MessageLocation;
if (error.mark.position < projectSource.body.length) {
location = new MessageLocation(
projectSource,
new SourcePosition(error.mark.position, error.mark.line + 1, error.mark.column + 1),
getLineEndPosition(error.mark.line + 1, projectSource),
);
} else {
// This is an exception: An error can be reported at the EOL. Calculating the EOL would not work
// -> just report the error at the last character
location = new MessageLocation(
projectSource,
projectSource.body.length - 1,
projectSource.body.length,
);
}

validationContext.addMessage(
new ValidationMessage({
severity,
message: error.reason,
location: new MessageLocation(
projectSource,
new SourcePosition(
error.mark.position,
error.mark.line + 1,
error.mark.column + 1,
),
endPos,
),
}),
new ValidationMessage({ severity, message: error.reason, location }),
);
});

Expand Down

0 comments on commit 23fae71

Please sign in to comment.