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 all 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
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": "^2.2.1",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@jridgewell/trace-mapping": "^0.3.18",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

17 changes: 15 additions & 2 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping';
import { walk } from 'estree-walker';
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import { getLocator } from 'locate-character';
import Stats from '../Stats';
import { reserved, is_valid } from '../utils/names';
Expand Down Expand Up @@ -142,8 +143,20 @@ export default class Component {
.replace(process.cwd(), '')
.replace(regex_leading_directory_separator, '')
: compile_options.filename);
this.locate = getLocator(this.source, { offsetLine: 1 });

// line numbers in stack trace frames are 1-based. source maps are 0-based
const locator = getLocator(this.source, { offsetLine: 1 });
// @ts-ignore - fix the type of CompileOptions.sourcemap
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);
// originalPositionFor returns 0-based lines?
// https://github.com/jridgewell/trace-mapping/issues/27
location.line += 1;
Copy link
Member

Choose a reason for hiding this comment

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

judging from some other code in language tools and the fact that tracemapping says it is a drop-in replacement for source-map which has line-1-based return values, this should not be done

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

assert.deepEqual(h1.__svelte_meta.loc, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'main.svelte')),
// TODO: fix this. the h1 is on line 5
line: 4,
column: 0,
char: 53
});

assert.deepEqual(p.__svelte_meta.loc, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'Foo.svelte')),
// TODO: fix this. the p is on line 2
Copy link
Member

Choose a reason for hiding this comment

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

I think these tests are correct, as source maps are zero line based. There's actually code in Element/index.js where it substracts one from the returned location to account for that. Why do you think these are incorrect?

Copy link
Member

Choose a reason for hiding this comment

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

source maps are 0-based, but locator is 1-based because it gets printed out to the user and users would expect it to be 1-based:

`${warning.message} (${start.line}:${start.column})\n${frame}`

I didn't realize that this loc is different than what's in locator because I missed the -1 here:

b`@add_location(${this.var}, ${renderer.file_var}, ${loc.line - 1}, ${loc.column}, ${

it's pretty confusing to me to keep track of where things are 0-based and where they're 1-based. I wonder if we can use this opportunity to make it 0-based everywhere and just make it 1-based at the very last second when it's printed to a user

line: 1,
column: 1,
char: 7
Expand Down