Skip to content

Commit 1c93744

Browse files
authored
Much better comment preservation (#22141)
* Retain comments on (and produce sourcemaps on) the equals token in initializers * Improved comments/sourcemaps for await, yield, while, and for * Retain comments on block curly-braces * Emit comments for if statements * Improved switch case comment emit * Improve comment and sourcemap emit for try/catch, throw, and continue statements * Improve sourcemap emit and comments for with statements * More accurate sourcemaps+comments emit for new, typeof, void, and delete * Improve comment emit for element access expressions * Preserve more comments on imports and exports * Make function a bit more defensive like other usages of emitTrailingCommentsOfPosition * Support preserving comments within empty lists * Handle leading comments of tokens, conditionally indent leading comments * Stop heuristically sourcemapping tokens When the transform was trivial it worked, but was unneeded, but when it was complex, it was brittle - best leave source mapping up to the transformers * Fix unneeded +1 * Tighten up element access comments * Handle comments on parenthesized expression tokens * Fix nit
1 parent 25525bc commit 1c93744

File tree

951 files changed

+22314
-25785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

951 files changed

+22314
-25785
lines changed

src/compiler/emitter.ts

Lines changed: 119 additions & 115 deletions
Large diffs are not rendered by default.

tests/baselines/reference/ES5For-of1.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/ES5For-of1.sourcemap.txt

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,50 @@ sourceFile:ES5For-of1.ts
1010
-------------------------------------------------------------------
1111
>>>for (var _i = 0, _a = ['a', 'b', 'c']; _i < _a.length; _i++) {
1212
1 >
13-
2 >^^^
14-
3 > ^
15-
4 > ^
16-
5 > ^^^^^^^^^^
17-
6 > ^^
18-
7 > ^^^^^^
19-
8 > ^^^
20-
9 > ^^
21-
10> ^^^
22-
11> ^^
23-
12> ^^^
24-
13> ^
25-
14> ^^
26-
15> ^^^^^^^^^^^^^^
27-
16> ^^
28-
17> ^^^^
13+
2 >^^^^^
14+
3 > ^^^^^^^^^^
15+
4 > ^^
16+
5 > ^^^^^^
17+
6 > ^^^
18+
7 > ^^
19+
8 > ^^^
20+
9 > ^^
21+
10> ^^^
22+
11> ^
23+
12> ^^
24+
13> ^^^^^^^^^^^^^^
25+
14> ^^
26+
15> ^^^^
2927
1 >
30-
2 >for
31-
3 >
32-
4 > (var v of
33-
5 > ['a', 'b', 'c']
34-
6 >
35-
7 > [
36-
8 > 'a'
37-
9 > ,
38-
10> 'b'
39-
11> ,
40-
12> 'c'
41-
13> ]
42-
14>
43-
15> ['a', 'b', 'c']
44-
16>
45-
17> ['a', 'b', 'c']
28+
2 >for (var v of
29+
3 > ['a', 'b', 'c']
30+
4 >
31+
5 > [
32+
6 > 'a'
33+
7 > ,
34+
8 > 'b'
35+
9 > ,
36+
10> 'c'
37+
11> ]
38+
12>
39+
13> ['a', 'b', 'c']
40+
14>
41+
15> ['a', 'b', 'c']
4642
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
47-
2 >Emitted(1, 4) Source(1, 4) + SourceIndex(0)
48-
3 >Emitted(1, 5) Source(1, 5) + SourceIndex(0)
49-
4 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
50-
5 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
51-
6 >Emitted(1, 18) Source(1, 15) + SourceIndex(0)
52-
7 >Emitted(1, 24) Source(1, 16) + SourceIndex(0)
53-
8 >Emitted(1, 27) Source(1, 19) + SourceIndex(0)
54-
9 >Emitted(1, 29) Source(1, 21) + SourceIndex(0)
55-
10>Emitted(1, 32) Source(1, 24) + SourceIndex(0)
56-
11>Emitted(1, 34) Source(1, 26) + SourceIndex(0)
57-
12>Emitted(1, 37) Source(1, 29) + SourceIndex(0)
58-
13>Emitted(1, 38) Source(1, 30) + SourceIndex(0)
59-
14>Emitted(1, 40) Source(1, 15) + SourceIndex(0)
60-
15>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
61-
16>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
62-
17>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
43+
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
44+
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
45+
4 >Emitted(1, 18) Source(1, 15) + SourceIndex(0)
46+
5 >Emitted(1, 24) Source(1, 16) + SourceIndex(0)
47+
6 >Emitted(1, 27) Source(1, 19) + SourceIndex(0)
48+
7 >Emitted(1, 29) Source(1, 21) + SourceIndex(0)
49+
8 >Emitted(1, 32) Source(1, 24) + SourceIndex(0)
50+
9 >Emitted(1, 34) Source(1, 26) + SourceIndex(0)
51+
10>Emitted(1, 37) Source(1, 29) + SourceIndex(0)
52+
11>Emitted(1, 38) Source(1, 30) + SourceIndex(0)
53+
12>Emitted(1, 40) Source(1, 15) + SourceIndex(0)
54+
13>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
55+
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
56+
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
6357
---
6458
>>> var v = _a[_i];
6559
1 >^^^^

tests/baselines/reference/ES5For-of13.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/ES5For-of13.sourcemap.txt

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,50 @@ sourceFile:ES5For-of13.ts
1010
-------------------------------------------------------------------
1111
>>>for (var _i = 0, _a = ['a', 'b', 'c']; _i < _a.length; _i++) {
1212
1 >
13-
2 >^^^
14-
3 > ^
15-
4 > ^
16-
5 > ^^^^^^^^^^
17-
6 > ^^
18-
7 > ^^^^^^
19-
8 > ^^^
20-
9 > ^^
21-
10> ^^^
22-
11> ^^
23-
12> ^^^
24-
13> ^
25-
14> ^^
26-
15> ^^^^^^^^^^^^^^
27-
16> ^^
28-
17> ^^^^
13+
2 >^^^^^
14+
3 > ^^^^^^^^^^
15+
4 > ^^
16+
5 > ^^^^^^
17+
6 > ^^^
18+
7 > ^^
19+
8 > ^^^
20+
9 > ^^
21+
10> ^^^
22+
11> ^
23+
12> ^^
24+
13> ^^^^^^^^^^^^^^
25+
14> ^^
26+
15> ^^^^
2927
1 >
30-
2 >for
31-
3 >
32-
4 > (let v of
33-
5 > ['a', 'b', 'c']
34-
6 >
35-
7 > [
36-
8 > 'a'
37-
9 > ,
38-
10> 'b'
39-
11> ,
40-
12> 'c'
41-
13> ]
42-
14>
43-
15> ['a', 'b', 'c']
44-
16>
45-
17> ['a', 'b', 'c']
28+
2 >for (let v of
29+
3 > ['a', 'b', 'c']
30+
4 >
31+
5 > [
32+
6 > 'a'
33+
7 > ,
34+
8 > 'b'
35+
9 > ,
36+
10> 'c'
37+
11> ]
38+
12>
39+
13> ['a', 'b', 'c']
40+
14>
41+
15> ['a', 'b', 'c']
4642
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
47-
2 >Emitted(1, 4) Source(1, 4) + SourceIndex(0)
48-
3 >Emitted(1, 5) Source(1, 5) + SourceIndex(0)
49-
4 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
50-
5 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
51-
6 >Emitted(1, 18) Source(1, 15) + SourceIndex(0)
52-
7 >Emitted(1, 24) Source(1, 16) + SourceIndex(0)
53-
8 >Emitted(1, 27) Source(1, 19) + SourceIndex(0)
54-
9 >Emitted(1, 29) Source(1, 21) + SourceIndex(0)
55-
10>Emitted(1, 32) Source(1, 24) + SourceIndex(0)
56-
11>Emitted(1, 34) Source(1, 26) + SourceIndex(0)
57-
12>Emitted(1, 37) Source(1, 29) + SourceIndex(0)
58-
13>Emitted(1, 38) Source(1, 30) + SourceIndex(0)
59-
14>Emitted(1, 40) Source(1, 15) + SourceIndex(0)
60-
15>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
61-
16>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
62-
17>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
43+
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
44+
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
45+
4 >Emitted(1, 18) Source(1, 15) + SourceIndex(0)
46+
5 >Emitted(1, 24) Source(1, 16) + SourceIndex(0)
47+
6 >Emitted(1, 27) Source(1, 19) + SourceIndex(0)
48+
7 >Emitted(1, 29) Source(1, 21) + SourceIndex(0)
49+
8 >Emitted(1, 32) Source(1, 24) + SourceIndex(0)
50+
9 >Emitted(1, 34) Source(1, 26) + SourceIndex(0)
51+
10>Emitted(1, 37) Source(1, 29) + SourceIndex(0)
52+
11>Emitted(1, 38) Source(1, 30) + SourceIndex(0)
53+
12>Emitted(1, 40) Source(1, 15) + SourceIndex(0)
54+
13>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
55+
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
56+
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
6357
---
6458
>>> var v = _a[_i];
6559
1 >^^^^

tests/baselines/reference/ES5For-of25.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/ES5For-of25.sourcemap.txt

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,33 @@ sourceFile:ES5For-of25.ts
4949
---
5050
>>>for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
5151
1->
52-
2 >^^^
53-
3 > ^
54-
4 > ^
55-
5 > ^^^^^^^^^^
56-
6 > ^^
57-
7 > ^^^^^^^
58-
8 > ^^
59-
9 > ^^^^^^^^^^^^^^^
60-
10> ^^
61-
11> ^^^^
52+
2 >^^^^^
53+
3 > ^^^^^^^^^^
54+
4 > ^^
55+
5 > ^^^^^^^
56+
6 > ^^
57+
7 > ^^^^^^^^^^^^^^^
58+
8 > ^^
59+
9 > ^^^^
6260
1->
6361
>
64-
2 >for
65-
3 >
66-
4 > (var v of
67-
5 > a
68-
6 >
69-
7 > a
70-
8 >
71-
9 > a
72-
10>
73-
11> a
62+
2 >for (var v of
63+
3 > a
64+
4 >
65+
5 > a
66+
6 >
67+
7 > a
68+
8 >
69+
9 > a
7470
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
75-
2 >Emitted(2, 4) Source(2, 4) + SourceIndex(0)
76-
3 >Emitted(2, 5) Source(2, 5) + SourceIndex(0)
77-
4 >Emitted(2, 6) Source(2, 15) + SourceIndex(0)
78-
5 >Emitted(2, 16) Source(2, 16) + SourceIndex(0)
79-
6 >Emitted(2, 18) Source(2, 15) + SourceIndex(0)
80-
7 >Emitted(2, 25) Source(2, 16) + SourceIndex(0)
81-
8 >Emitted(2, 27) Source(2, 15) + SourceIndex(0)
82-
9 >Emitted(2, 42) Source(2, 16) + SourceIndex(0)
83-
10>Emitted(2, 44) Source(2, 15) + SourceIndex(0)
84-
11>Emitted(2, 48) Source(2, 16) + SourceIndex(0)
71+
2 >Emitted(2, 6) Source(2, 15) + SourceIndex(0)
72+
3 >Emitted(2, 16) Source(2, 16) + SourceIndex(0)
73+
4 >Emitted(2, 18) Source(2, 15) + SourceIndex(0)
74+
5 >Emitted(2, 25) Source(2, 16) + SourceIndex(0)
75+
6 >Emitted(2, 27) Source(2, 15) + SourceIndex(0)
76+
7 >Emitted(2, 42) Source(2, 16) + SourceIndex(0)
77+
8 >Emitted(2, 44) Source(2, 15) + SourceIndex(0)
78+
9 >Emitted(2, 48) Source(2, 16) + SourceIndex(0)
8579
---
8680
>>> var v = a_1[_i];
8781
1 >^^^^

tests/baselines/reference/ES5For-of26.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/ES5For-of26.sourcemap.txt

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,45 @@ sourceFile:ES5For-of26.ts
1010
-------------------------------------------------------------------
1111
>>>for (var _i = 0, _a = [2, 3]; _i < _a.length; _i++) {
1212
1 >
13-
2 >^^^
14-
3 > ^
15-
4 > ^
16-
5 > ^^^^^^^^^^
17-
6 > ^^
18-
7 > ^^^^^^
19-
8 > ^
20-
9 > ^^
21-
10> ^
22-
11> ^
23-
12> ^^
24-
13> ^^^^^^^^^^^^^^
25-
14> ^^
26-
15> ^^^^
27-
16> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
13+
2 >^^^^^
14+
3 > ^^^^^^^^^^
15+
4 > ^^
16+
5 > ^^^^^^
17+
6 > ^
18+
7 > ^^
19+
8 > ^
20+
9 > ^
21+
10> ^^
22+
11> ^^^^^^^^^^^^^^
23+
12> ^^
24+
13> ^^^^
25+
14> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
2826
1 >
29-
2 >for
30-
3 >
31-
4 > (var [a = 0, b = 1] of
32-
5 > [2, 3]
33-
6 >
34-
7 > [
35-
8 > 2
36-
9 > ,
37-
10> 3
38-
11> ]
39-
12>
40-
13> [2, 3]
41-
14>
42-
15> [2, 3]
27+
2 >for (var [a = 0, b = 1] of
28+
3 > [2, 3]
29+
4 >
30+
5 > [
31+
6 > 2
32+
7 > ,
33+
8 > 3
34+
9 > ]
35+
10>
36+
11> [2, 3]
37+
12>
38+
13> [2, 3]
4339
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
44-
2 >Emitted(1, 4) Source(1, 4) + SourceIndex(0)
45-
3 >Emitted(1, 5) Source(1, 5) + SourceIndex(0)
46-
4 >Emitted(1, 6) Source(1, 28) + SourceIndex(0)
47-
5 >Emitted(1, 16) Source(1, 34) + SourceIndex(0)
48-
6 >Emitted(1, 18) Source(1, 28) + SourceIndex(0)
49-
7 >Emitted(1, 24) Source(1, 29) + SourceIndex(0)
50-
8 >Emitted(1, 25) Source(1, 30) + SourceIndex(0)
51-
9 >Emitted(1, 27) Source(1, 32) + SourceIndex(0)
52-
10>Emitted(1, 28) Source(1, 33) + SourceIndex(0)
53-
11>Emitted(1, 29) Source(1, 34) + SourceIndex(0)
54-
12>Emitted(1, 31) Source(1, 28) + SourceIndex(0)
55-
13>Emitted(1, 45) Source(1, 34) + SourceIndex(0)
56-
14>Emitted(1, 47) Source(1, 28) + SourceIndex(0)
57-
15>Emitted(1, 51) Source(1, 34) + SourceIndex(0)
40+
2 >Emitted(1, 6) Source(1, 28) + SourceIndex(0)
41+
3 >Emitted(1, 16) Source(1, 34) + SourceIndex(0)
42+
4 >Emitted(1, 18) Source(1, 28) + SourceIndex(0)
43+
5 >Emitted(1, 24) Source(1, 29) + SourceIndex(0)
44+
6 >Emitted(1, 25) Source(1, 30) + SourceIndex(0)
45+
7 >Emitted(1, 27) Source(1, 32) + SourceIndex(0)
46+
8 >Emitted(1, 28) Source(1, 33) + SourceIndex(0)
47+
9 >Emitted(1, 29) Source(1, 34) + SourceIndex(0)
48+
10>Emitted(1, 31) Source(1, 28) + SourceIndex(0)
49+
11>Emitted(1, 45) Source(1, 34) + SourceIndex(0)
50+
12>Emitted(1, 47) Source(1, 28) + SourceIndex(0)
51+
13>Emitted(1, 51) Source(1, 34) + SourceIndex(0)
5852
---
5953
>>> var _b = _a[_i], _c = _b[0], a = _c === void 0 ? 0 : _c, _d = _b[1], b = _d === void 0 ? 1 : _d;
6054
1->^^^^

tests/baselines/reference/ES5For-of3.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)