You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This update changes the semantics of the Range Mapping such that it applies to character sequences that include newlines.
The encoding format/syntax is unchanged. This is a purely semantic change.
It is motivated by cases where most or all of a file contents could be expressed via a single range mapping, e.g. to concisely produce an identity range mapping.
Source: https://issues.chromium.org/issues/364917746
Copy file name to clipboardexpand all lines: proposals/range-mappings.md
+18-15
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ Tobias Koppers
15
15
## Motiviation
16
16
17
17
Currently mappings map locations in the source code to other locations in the source code.
18
-
This works well when trying to access a location that is defined in the SourceMap, but one looses percision when accessing locations that are not directly defined in the SourceMap.
18
+
This works well when trying to access a location that is defined in the SourceMap, but one loses precision when accessing locations that are not directly defined in the SourceMap.
19
19
In these cases tools usually fallback to next lower column that is actually mapped in the SourceMap.
20
-
So we are either loosing information or we need many mappings in the SourceMap to cover all possible locations.
20
+
So we are either losing information or we need many mappings in the SourceMap to cover all possible locations.
21
21
22
22
These information problem is especially problematic when applying a SourceMap to another SourceMap.
23
23
Here we can only use locations that are specified in both SourceMaps. We have to be lucky that locations match up.
@@ -26,7 +26,7 @@ Here we can only use locations that are specified in both SourceMaps. We have to
26
26
27
27
As an example let's look at a build process when a TypeScript file is converted to JavaScript first and that is minified afterwards.
28
28
29
-
The TypeScript to JavaScript transformation is mostly keeping codeidentical, but removing type annotations.
29
+
A simplistic TypeScript to JavaScript transformation such as SWC's [`strip_types`](https://play.swc.rs/?version=1.10.7&code=H4sIAAAAAAAAA0WMQQqDMBBF93OKv6wgPYBpu5HewAvEQTA0Tcpkggvx7k2E4OrD%2B4%2FH3qaE0epjemEn4Jdn7xjb6tJnkTQg5O%2B8iLkutc4PmAwVxDEklcwa5cYxB21%2B37TurAJagvdWxROnba6r6gXXqfSgg0hXiRveIqXemT8eTB9GqwAAAA%3D%3D&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3KFi6NA79BCIOhERAYQdqSjK3QsJpM1mv4%2Ff8yqkhIkMPOVaxrJEnQjTuReEsmf9KQhwjkgm2chw6yxTpQbtCHdoOxhgnUbk6kJSd6WaA1wIhN3RsNl6O%2BT%2FTBPmmJDoKqxS7UeH10TRUmEO72Un2y%2B179HgAT9RDzsPg6VXd3JaUGxfBMLf3xcBAAA%3D&strip-types=) keeps the runtime code identical, whilst removing type annotations.
30
30
Theoretically only a few SourceMap mappings are needs, as most code stays identical.
31
31
32
32
Minifying is a bigger transformation of the code, which one it's own would result in a lot of SourceMap mappings to be generated.
@@ -40,7 +40,7 @@ The TypeScript SourceMap would behave identical to a SourceMap mapping every sin
40
40
## Proposal
41
41
42
42
Add a boolean flag for each mapping to convert it into a "range mapping".
43
-
For a range mapping, tools should assume that every char that follows the mapping (until the next mapping), is mapped to the specified original location plus the offset in the generated code.
43
+
For a range mapping, tools should assume that every char (including newlines) that follows the mapping (until the next mapping), is mapped to the specified original location plus the offset in the generated code.
44
44
This means all chars in the generated code that is covered by the range mapping, are mapped char by char to the same range in the original code.
45
45
(Usually this only makes sense when generated and original are identical for that range)
46
46
@@ -49,27 +49,30 @@ This means all chars in the generated code that is covered by the range mapping,
49
49
Generated Code:
50
50
51
51
```js
52
-
console.log("hello world");
52
+
console.log(
53
+
"hello world");
53
54
```
54
55
55
56
Original Code:
56
57
57
58
```js
58
-
// Copyright 2023
59
-
console.log("hello world");
59
+
// Copyright 2023
60
+
console.log(
61
+
"hello world");
60
62
```
61
63
62
64
With a normal mapping:
63
65
64
66
```
65
67
Source Map:
66
68
Generate Line 1 Column 0 -> Original Line 2 Column 2
69
+
Generate Line 2 Column 0 -> Original Line 3 Column 0
67
70
```
68
71
69
72
```js
70
-
console.log("hello world");
71
-
^^^
72
-
||+ maps to Original Line 2 Column 2
73
+
console.log(\n"hello world");
74
+
^^^
75
+
||+ maps to Original Line 3 Column 0
73
76
|+ maps to Original Line 2 Column 2
74
77
+ maps to Original Line 2 Column 2
75
78
```
@@ -82,17 +85,17 @@ Generate Line 1 Column 0 -> Original Line 2 Column 2 (range mapping)
82
85
```
83
86
84
87
```js
85
-
console.log("hello world");
86
-
^^^
87
-
||+ maps to Original Line 2 Column 14
88
+
console.log(\n"hello world");
89
+
^^^
90
+
||+ maps to Original Line 3 Column 0
88
91
|+ maps to Original Line 2 Column 10
89
92
+ maps to Original Line 2 Column 2
90
93
```
91
94
92
95
### Encoding
93
96
94
97
To avoid a breaking change to the `mappings` field, a new field named `rangeMappings` is added.
95
-
It contains encoded data perline in the generated code.
98
+
It contains encoded data per-line in the generated code.
96
99
Each line is separated by `;`.
97
100
The data contains a bit per mapping in that line.
98
101
When the bit is set, the mapping is a range mapping, otherwise it is a normal mapping.
@@ -111,5 +114,5 @@ Line 1: 0b000000 0b000000 0b000001 => the 13th mapping is a range mapping
111
114
Line 3: 0b100000 => the 6th mapping is a range mapping
112
115
```
113
116
114
-
> Note: The perline encoding is chosen to make it easier to generate SourceMap line by line.
117
+
> Note: The per-line encoding is chosen to make it easier to generate SourceMap line by line.
115
118
> It also looks similar to the `mappings` field, so should allow good compression.
0 commit comments