Skip to content
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

fix: improve source code location metadata accuracy #8362

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
11fc31e
fix: improve source code location metadata accuracy by accounting for…
dominikg Mar 8, 2023
f42aa9c
Update src/compiler/compile/Component.ts
dummdidumm Mar 14, 2023
d89022f
fix: add ts-expect-error for invalid trace-mapping type
dominikg Mar 14, 2023
22eaaf2
fix: format
dominikg Mar 14, 2023
2dea46b
Merge branch 'master' into fix/svelte-meta-pos
benmccann Apr 7, 2023
765023d
fix: html space entities lost in component slot (#8464)
xxkl1 Apr 11, 2023
75aec41
breaking: send in/out to transition fn (#8318)
tivac Apr 11, 2023
c729829
chore: remove node<14 tests (#8482)
dummdidumm Apr 11, 2023
61312c8
TypeScript 4
benmccann Apr 11, 2023
7d3b368
merge version-4 branch
benmccann Apr 11, 2023
4547d3f
add a new @ts-ignore
benmccann Apr 11, 2023
e8c1db9
lockfile update
benmccann Apr 11, 2023
eedacc9
chore: simplify Svelte 4 CI (#8487)
benmccann Apr 12, 2023
abd760d
chore: bump engines field (#8489)
benmccann Apr 12, 2023
1e2cfa4
chore: upgrade to TypeScript 5 (#8488)
benmccann Apr 12, 2023
42e0f7d
chore: Svelte 4 dependency upgrades (#8486)
benmccann Apr 12, 2023
c9ccd6e
chore: upgrade rollup (#8491)
benmccann Apr 12, 2023
8c9198d
merge version-4 branch
benmccann Apr 12, 2023
16ab649
package-lock cleanup
benmccann Apr 12, 2023
1533c9f
update trace-mapping library
benmccann Apr 12, 2023
7410321
Update src/compiler/compile/Component.ts
benmccann Apr 12, 2023
56146d0
fix most tests
benmccann Apr 12, 2023
eab99a4
revert unrelated changes and add TODOs
benmccann Apr 12, 2023
2c92c2f
merge version-4 branch
benmccann Apr 19, 2023
9aca05b
fix changelog
dummdidumm Apr 20, 2023
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
38 changes: 38 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"devDependencies": {
"@ampproject/remapping": "^0.3.0",
"@jridgewell/sourcemap-codec": "^1.4.14",
"@jridgewell/trace-mapping": "^0.3.17",
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-expect-error Svelte 3's ancient TS version can't look up the types for this package properly yet
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import { walk } from 'estree-walker';
import { getLocator } from 'locate-character';
import Stats from '../Stats';
Expand Down Expand Up @@ -143,8 +145,16 @@ export default class Component {
.replace(process.cwd(), '')
.replace(regex_leading_directory_separator, '')
: compile_options.filename);
this.locate = getLocator(this.source, { offsetLine: 1 });

const locator = getLocator(this.source, { offsetLine: 1 });
const tracer = compile_options.sourcemap ? new TraceMap(compile_options.sourcemap) : undefined;
this.locate = (c) => {
let location:{line:number,column:number} = locator(c);
if (tracer) {
location = originalPositionFor(tracer, location);
}
location.line += 1; // make line one based
Copy link
Member Author

Choose a reason for hiding this comment

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

is this really just zero based vs one based or another cause?

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering why this all still works the same, since you're always adding +1 to the line, regardless of a source map existing or not.

Copy link
Member

Choose a reason for hiding this comment

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

you're already adding 1 above: https://github.com/sveltejs/svelte/pull/8362/files#r1135771594

so I think this is really adding 2 which seems wrong to me

Copy link
Member Author

Choose a reason for hiding this comment

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

in a locally linked vite project with sveltekit-ts it produces correct positions in __svelte_meta.

I agree that this looks off and so far havn't been able to understand why it produces the correct result.

return location;
};
// styles
this.stylesheet = new Stylesheet({
source,
Expand Down
4 changes: 2 additions & 2 deletions test/runtime/samples/element-source-location/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default {

assert.deepEqual(h1.__svelte_meta.loc, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'main.svelte')),
line: 4,
line: 5,
Copy link
Member Author

Choose a reason for hiding this comment

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

looking at main.svelte, the h1 sits at line 5, not line 4

Copy link
Member

Choose a reason for hiding this comment

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

I've reverted these changes since it seems like there's some other bug that was present previously. Perhaps something like Rich-Harris/magic-string#248 which was causing some of our other source map tests to assert unexpected behavior

column: 0,
char: 53
});

assert.deepEqual(p.__svelte_meta.loc, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'Foo.svelte')),
line: 1,
line: 2,
Copy link
Member Author

@dominikg dominikg Mar 8, 2023

Choose a reason for hiding this comment

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

same for the p, the previous combination of line 1, col 1 and char 7 would require cramming 6 chars at col 0.

column: 1,
char: 7
});
Expand Down