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

Define decoding algorithms for source maps #119

Merged
merged 14 commits into from
Sep 15, 2024

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Aug 8, 2024

Preview: https://nicolo-ribaudo.github.io/source-map/#decoded-source-map

This PR introduces "decoded source map" data structures, which are
internal spec representations of the information encoded in source maps.

It also defines algorithms to decode source maps from either a JSON
string or infra representation.

The goal is:

  • use them to explicitly write down all the possible error cases
  • use them as a starting point to define new data structures, for
    example for the scopes proposal
  • eventually add algorithms such as "get the original location given a
    decoded source map and a generated location".

This PR also explicitly defines sources/sourceRoot resolution in terms
of the WHATWG URL spec.

There are a few points where I'm not sure about what the current/expected behavior is; I marked them with HTML comments (search for <!--). @takikawa Did you happen to figure out an answer to them while writing tests?
Also, I almost everywhere used "optionally report an error", but in a few cases I wrote "throw an error" because I don't think there is a way to recover from it (for example, invalid data in the mappings string). I didn't test what implementations do though.

This PR partially fixes the first and last points of #105. Closes #123.

This PR introduces "decoded source map" data structures, which are
internal spec representations of the information encoded in source maps.

It also defines algorithms to decode source maps from either a JSON
string or [infra](https://infra.spec.whatwg.org/) representation.

The goal is:
- use them to explicitly write down all the possible error cases
- use them as a starting point to define new data structures, for
  example for the scopes proposal
- eventually add algorithms such as "get the original location given a
  decoded source map and a generated location".

This PR also explicitly defines sources/sourceRoot resolution in terms
of the [WHATWG URL](https://url.spec.whatwg.org/) spec.
@nicolo-ribaudo nicolo-ribaudo force-pushed the wip-decoding-algorithms branch from 232723b to 41fa6a6 Compare August 8, 2024 13:52
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
@nicolo-ribaudo nicolo-ribaudo marked this pull request as ready for review August 26, 2024 15:11
source-map.bs Outdated Show resolved Hide resolved
source-map.bs Show resolved Hide resolved
@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Sep 5, 2024

PR updated to return i32::min for B, as discussed in the meeting last week.

source-map.bs Outdated Show resolved Hide resolved
source-map.bs Outdated Show resolved Hide resolved
1. Else, set |decodedSource|'s [=decoded source/URL=] to |sourceURL|.
1. If |index| is in |ignoredSources|, set |decodedSource|'s [=decoded source/ignored=] to true.
1. If |sourcesContent|'s [=list/size=] is greater than or equal to |index|, set
|decodedSource|'s [=decoded source/content=] to |sourcesContent|[|index|].
Copy link
Collaborator

Choose a reason for hiding this comment

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

The handling of sourcesContent here seems like a natural way to spec it, but when you have duplicate entries it currently conflicts with what implementations actually do. (see this issue for more details) In particular, it seems all the major browsers treat sources entries as being set-like so that duplicate source URLs point to the same sourcesContent (either the first or last one for that particular URL).

@nicolo-ribaudo nicolo-ribaudo added this to the October 2024 milestone Sep 11, 2024
@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Sep 13, 2024

@takikawa Given that two of the main debuggers behave one way and two another, I added a note saying that although we support source maps with multiple contents for the same URL, if a debuger does not support showing it they will probably just pick one of the contents in an implementation-defined way.

I am not sure that describing the expected behavior in this case is a problem that we necessarily have to figure out, because the same problem can happen also when not using source maps (https://issues.chromium.org/issues/361652150). At most we could say that source maps cannot have duplicate file URLs, but we cannot ban two different source maps from introducing the same original file with two different contents anyway.

source-map.bs Outdated
1. If |relativeSourceIndex| is not null, [=optionally report an error=].
1. Continue with the next iteration.
<!-- TODO: If there is a relativeSourceIndex but not a relativeOriginalLine,
should |sourceIndex| still be increased? -->
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this TODO can be deleted as the answer to this should be "no" right? (because if relativeOriginalLine is null, then relativeOriginalColumn must also be null due to where VLQ decoding returns null, and thus it optionally returns an error)

Copy link
Collaborator

@takikawa takikawa left a comment

Choose a reason for hiding this comment

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

LGTM, the new note about sources looks good thanks. Left a minor comment about a remaining TODO.

source-map.bs Outdated Show resolved Hide resolved
@nicolo-ribaudo nicolo-ribaudo merged commit 66a163d into tc39:main Sep 15, 2024
2 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the wip-decoding-algorithms branch September 15, 2024 18:57
github-actions bot added a commit that referenced this pull request Sep 15, 2024
SHA: 66a163d
Reason: push, by nicolo-ribaudo

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What to do about B and -2147483648 in mappings
3 participants