Skip to content

Commit

Permalink
fix: start of line source location issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Aug 19, 2024
1 parent 74e6633 commit ce88d81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .changeset/silver-games-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@marko/babel-utils": patch
"@marko/compiler": patch
"marko": patch
"@marko/runtime-tags": patch
"@marko/translator-default": patch
"@marko/translator-interop-class-tags": patch
"@marko/translator-tags": patch
---

Fix "off by one" issue with source location information when the index was at the start of the line.
14 changes: 10 additions & 4 deletions packages/babel-utils/src/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getLineIndexes(file) {
let lineIndexes = file.metadata.marko[LINE_INDEX_KEY];

if (!lineIndexes) {
lineIndexes = [0];
lineIndexes = [-1];
for (let i = 0; i < file.code.length; i++) {
if (file.code[i] === "\n") {
lineIndexes.push(i);
Expand Down Expand Up @@ -58,13 +58,19 @@ function findLoc(lineIndexes, startLine, index) {
}

let lineIndex = lineIndexes[line];
if (lineIndex > index) {
if (lineIndex >= index) {
lineIndex = lineIndexes[--line];
}

if (line === 0) {
return {
line: 1,
column: index,
};
}

return {
index,
line: line + 1,
column: index === lineIndex ? 0 : index - lineIndex - (line === 0 ? 0 : 1),
column: index - lineIndex - 1,
};
}

0 comments on commit ce88d81

Please sign in to comment.