Skip to content

Commit

Permalink
Extract column check to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed Aug 28, 2023
1 parent 8576e31 commit 9ba5a88
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/assets/javascripts/components/annotations/line_of_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class LineOfCode extends ShadowlessLitElement {
.sort(compareAnnotationOrders);
}

hasValidColumn(annotation: Annotation): boolean {
return annotation.column !== undefined && annotation.column !== null && annotation.column >= 0 && annotation.column < this.codeLength;
}

/**
* Calculates the range of the code that is covered by the given annotation.
* If the annotation spans multiple lines, the range will be the whole line unless this is the first or last line.
Expand All @@ -82,7 +86,7 @@ export class LineOfCode extends ShadowlessLitElement {
let start = 0;
if (this.row === firstRow) {
// In rare cases, machine annotations start past the end of the line, resulting in errors if we don't constrain them to be in the line
if (annotation.column !== undefined && annotation.column !== null && annotation.column >= 0 && annotation.column < this.codeLength) {
if (this.hasValidColumn(annotation)) {
start = annotation.column;
} else {
start = 0;
Expand All @@ -91,7 +95,7 @@ export class LineOfCode extends ShadowlessLitElement {

let length = this.codeLength - start;
if (this.row === lastRow) {
if (annotation.column !== undefined && annotation.column !== null && annotation.column >= 0 && annotation.column < this.codeLength) {
if (this.hasValidColumn(annotation)) {
const defaultLength = isMachineAnnotation ? 0 : this.codeLength - start;
length = annotation.columns || defaultLength;
}
Expand Down

0 comments on commit 9ba5a88

Please sign in to comment.