Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix yaml validation locations #349

Merged
merged 2 commits into from
Jan 15, 2025
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
7 changes: 7 additions & 0 deletions src/model/validation/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export class MessageLocation {
value: new ProjectSource(this.sourceName, ''),
});
}

if (typeof _start !== 'number' && !(_start instanceof SourcePosition)) {
throw new Error(`start must be either a number or a SourcePosition`);
}
if (typeof _end !== 'number' && !(_end instanceof SourcePosition)) {
throw new Error(`end must be either a number or a SourcePosition`);
}
}

static from(location: LocationLike | undefined): MessageLocation | undefined {
Expand Down
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
2 changes: 1 addition & 1 deletion src/schema/schema-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,5 @@ export function getLineEndPosition(targetLine: number, source: ProjectSource): S
}
}

return { line: targetLine, offset: curIndex - 1, column: column };
return new SourcePosition(curIndex - 1, targetLine, column);
}
Loading