Skip to content

Fixed wrong error location when using ts with valid-compile rule #40

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

Merged
merged 2 commits into from
Jul 15, 2021
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
!/.github
/prettier-playground
/tests/fixtures/rules/indent/invalid/ts
/tests/fixtures/rules/valid-compile/invalid/ts
/tests/fixtures/rules/valid-compile/valid/ts
54 changes: 33 additions & 21 deletions src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,36 +205,41 @@ export default createRule("valid-compile", {
} {
decoded ??= decode(JSON.parse(output.sourceMapText!).mappings)

const lineMaps = decoded[pos.line]
const lineMaps = decoded[pos.line - 1]

if (!lineMaps?.length) {
for (let line = pos.line - 1; line >= 0; line--) {
const prevLineMaps = decoded[line]
if (prevLineMaps?.length) {
const [, , remapLine, remapCol] =
const [, , sourceCodeLine, sourceCodeColumn] =
prevLineMaps[prevLineMaps.length - 1]
return {
line: remapLine!,
column: remapCol!,
line: sourceCodeLine! + 1,
column: sourceCodeColumn!,
}
}
}
return { line: -1, column: -1 }
}

for (let index = 0; index < lineMaps.length - 1; index++) {
const [col, , remapLine, remapCol] = lineMaps[index]
if (col <= pos.column && pos.column < lineMaps[index + 1][0]) {
const [generateCodeColumn, , sourceCodeLine, sourceCodeColumn] =
lineMaps[index]
if (
generateCodeColumn <= pos.column &&
pos.column < lineMaps[index + 1][0]
) {
return {
line: remapLine!,
column: remapCol!,
line: sourceCodeLine! + 1,
column: sourceCodeColumn! + (pos.column - generateCodeColumn),
}
}
}
const [, , remapLine, remapCol] = lineMaps[lineMaps.length - 1]
const [generateCodeColumn, , sourceCodeLine, sourceCodeColumn] =
lineMaps[lineMaps.length - 1]
return {
line: remapLine!,
column: remapCol!,
line: sourceCodeLine! + 1,
column: sourceCodeColumn! + (pos.column - generateCodeColumn),
}
}
}
Expand All @@ -258,6 +263,7 @@ export default createRule("valid-compile", {
column: number
}
} {
const mapIndexes = this.mapIndexes
const locs = (this.locs ??= new LinesAndColumns(this.code))
let start:
| {
Expand All @@ -273,24 +279,30 @@ export default createRule("valid-compile", {
| undefined = undefined
if (points.start) {
const index = locs.getIndexFromLoc(points.start)
for (const mapIndex of this.mapIndexes) {
if (mapIndex.range[0] <= index && index < mapIndex.range[1]) {
start = sourceCode.getLocFromIndex(mapIndex.remap(index))
break
}
const remapped = remapIndex(index)
if (remapped) {
start = sourceCode.getLocFromIndex(remapped)
}
}
if (points.end) {
const index = locs.getIndexFromLoc(points.end)
for (const mapIndex of this.mapIndexes) {
if (mapIndex.range[0] <= index && index <= mapIndex.range[1]) {
end = sourceCode.getLocFromIndex(mapIndex.remap(index))
break
}
const remapped = remapIndex(index - 1 /* include index */)
if (remapped) {
end = sourceCode.getLocFromIndex(remapped + 1 /* restore */)
}
}

return { start, end }

/** remap index */
function remapIndex(index: number) {
for (const mapIndex of mapIndexes) {
if (mapIndex.range[0] <= index && index < mapIndex.range[1]) {
return mapIndex.remap(index)
}
}
return null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "The $ prefix is reserved, and cannot be used for variable and import names",
"line": 2,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
enum $E {
a = "tutorial/image.gif",
b = "Rick Astley",
}
</script>