Skip to content
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"pnpm": {
"overrides": {
"@vitest/coverage-v8>ast-v8-to-istanbul": "./"
},
"patchedDependencies": {
"istanbul-lib-source-maps": "patches/istanbul-lib-source-maps.patch"
Comment on lines +80 to +82

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this patch temporarily to make the test pass.

}
},
"prettier": {}
Expand Down
36 changes: 36 additions & 0 deletions patches/istanbul-lib-source-maps.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
diff --git a/CHANGELOG.md b/CHANGELOG.md
deleted file mode 100644
index dad8c06aac26cb8cfced31da5f35ac8e95915f15..0000000000000000000000000000000000000000
diff --git a/lib/get-mapping.js b/lib/get-mapping.js
index 187a02ed65104d8e3a058d6bfe7dc8bd7c285369..10dbf17549a6a0795e868bbc2ea6d0df661da514 100644
--- a/lib/get-mapping.js
+++ b/lib/get-mapping.js
@@ -36,13 +36,26 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
// generated file end location. Note however that this position on its
// own is not useful because it is the position of the _start_ of the range
// on the original file, and we want the _end_ of the range.
- const beforeEndMapping = originalPositionTryBoth(
+ let beforeEndMapping = originalPositionTryBoth(
sourceMap,
generatedEnd.line,
generatedEnd.column - 1
);
if (beforeEndMapping.source === null) {
- return null;
+ for (
+ let line = generatedEnd.line;
+ line > 0 && beforeEndMapping.source === null;
+ line--
+ ) {
+ beforeEndMapping = originalPositionTryBoth(
+ sourceMap,
+ line,
+ Number.MAX_SAFE_INTEGER
+ );
+ }
+ if (beforeEndMapping.source === null) {
+ return null;
+ }
}

// Convert that original position back to a generated one, with a bump
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ export class Locator {

let end = getPosition(endNeedle, this.#map);

// e.g. tsc that doesnt include } in source maps
if (end === null) {
endNeedle.column++;
end = getPosition(endNeedle, this.#map);
}

if (end === null) {
// search the previous lines as the mapping was not found on the same line
// e.g. tsc that doesnt include } in source maps
for (
let line = endNeedle.line;
line >= startNeedle.line && end === null;
line--
) {
end = getPosition({ line, column: Infinity }, this.#map);
}
// Does not exist in source maps, e.g. generated code
return null;
if (end === null) return null;
}

const loc = { start, end };
Expand Down
36 changes: 36 additions & 0 deletions test/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,39 @@ function sum(a, b) {
expect(locator.offsetToNeedle(start)).toEqual({ line: 2, column: 9 });
expect(locator.offsetToNeedle(end)).toEqual({ line: 2, column: 12 });
});

test("getLoc returns mapped location for positions without end mapping", () => {
const code = `\
export function isEven(a: number) {
return a % 2 === 0
}

export function isOdd(a: number) {
return !isEven(a)
}
`;
const map = {
version: 3 as const,
mappings:
"AAAA,OAAO,SAAS,OAAO,GAAW;AAChC,QAAO,IAAI,MAAM;;AAGnB,OAAO,SAAS,MAAM,GAAW;AAC/B,QAAO,CAAC,OAAO,EAAE",
sources: ["even.ts"],
names: [],
};

const locator = new Locator(code, new TraceMap(map), "");

expect(locator.getLoc({ start: 25, end: 50 })).toEqual({
start: {
filename: "even.ts",
line: 1,
column: 23,
},
end: {
source: "even.ts",
filename: "even.ts",
line: 2,
column: 19,
name: null,
},
});
});
4 changes: 2 additions & 2 deletions test/svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test("svelte for loop", async ({ actual, expected }) => {
expect(actual).toMatchInlineSnapshot(`
{
"branches": "0/0 (100%)",
"functions": "0/3 (0%)",
"functions": "0/4 (0%)",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to vue, #each is transformed to $.each(..., ..., () => ..., () => ..., () => { ... }) and this last callback is now mapped correctly.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the Vue test are here mostly to verify this library and Istanbul packages remap the same code similarly. In Svelte and Vue, I wouldn't necessarily call any of this "correct", but it's what their compilers output:

"lines": "0/3 (0%)",
"statements": "0/5 (0%)",
"statements": "0/6 (0%)",
}
`);

Expand Down
6 changes: 3 additions & 3 deletions test/vue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test("vue for loop", async ({ actual, expected }) => {
expect(actual).toMatchInlineSnapshot(`
{
"branches": "0/0 (100%)",
"functions": "0/0 (100%)",
"lines": "0/2 (0%)",
"statements": "0/2 (0%)",
"functions": "0/1 (0%)",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v-for is transformed to _renderList(..., () => { ... }) and this callback is now mapped correctly.

"lines": "0/3 (0%)",
"statements": "0/3 (0%)",
}
`);

Expand Down
Loading