diff --git a/.changeset/silver-games-hide.md b/.changeset/silver-games-hide.md new file mode 100644 index 0000000000..f0bbb5a4f2 --- /dev/null +++ b/.changeset/silver-games-hide.md @@ -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. diff --git a/packages/babel-utils/src/loc.js b/packages/babel-utils/src/loc.js index 30d85bc80e..337fe6109e 100644 --- a/packages/babel-utils/src/loc.js +++ b/packages/babel-utils/src/loc.js @@ -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); @@ -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, }; }