From 3f1aa022c6d4898c6b1e9b748d52830afd667e41 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 15 Jul 2021 11:05:25 +0900 Subject: [PATCH 1/2] Fixed wrong error location when using ts with valid-compile rule --- src/rules/valid-compile.ts | 54 +++++++++++-------- .../invalid/ts/enum01-errors.json | 7 +++ .../invalid/ts/enum01-input.svelte | 6 +++ 3 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 tests/fixtures/rules/valid-compile/invalid/ts/enum01-errors.json create mode 100644 tests/fixtures/rules/valid-compile/invalid/ts/enum01-input.svelte diff --git a/src/rules/valid-compile.ts b/src/rules/valid-compile.ts index 73abc5324..d5fa928e0 100644 --- a/src/rules/valid-compile.ts +++ b/src/rules/valid-compile.ts @@ -205,17 +205,17 @@ 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!, } } } @@ -223,18 +223,23 @@ export default createRule("valid-compile", { } 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), } } } @@ -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: | { @@ -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 + } } } diff --git a/tests/fixtures/rules/valid-compile/invalid/ts/enum01-errors.json b/tests/fixtures/rules/valid-compile/invalid/ts/enum01-errors.json new file mode 100644 index 000000000..6cb0026eb --- /dev/null +++ b/tests/fixtures/rules/valid-compile/invalid/ts/enum01-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "The $ prefix is reserved, and cannot be used for variable and import names", + "line": 2, + "column": 3 + } +] diff --git a/tests/fixtures/rules/valid-compile/invalid/ts/enum01-input.svelte b/tests/fixtures/rules/valid-compile/invalid/ts/enum01-input.svelte new file mode 100644 index 000000000..708c91773 --- /dev/null +++ b/tests/fixtures/rules/valid-compile/invalid/ts/enum01-input.svelte @@ -0,0 +1,6 @@ + From 9561f7fe534079aec386c968a1497fcfbfd0168f Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 15 Jul 2021 11:07:11 +0900 Subject: [PATCH 2/2] fix --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index a46f06ecc..5fc024ddd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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