Skip to content

Commit 2d01d76

Browse files
authored
Change es2015 transform to retain comments on for of loop bodies (microsoft#22396)
1 parent 9774cd5 commit 2d01d76

39 files changed

Lines changed: 777 additions & 154 deletions

File tree

src/compiler/transformers/es2015.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,32 +2348,26 @@ namespace ts {
23482348
}
23492349
}
23502350

2351-
let bodyLocation: TextRange;
2352-
let statementsLocation: TextRange;
23532351
if (convertedLoopBodyStatements) {
2354-
addRange(statements, convertedLoopBodyStatements);
2352+
return createSyntheticBlockForConvertedStatements(addRange(statements, convertedLoopBodyStatements));
23552353
}
23562354
else {
23572355
const statement = visitNode(node.statement, visitor, isStatement, liftToBlock);
23582356
if (isBlock(statement)) {
2359-
addRange(statements, statement.statements);
2360-
bodyLocation = statement;
2361-
statementsLocation = statement.statements;
2357+
return updateBlock(statement, setTextRange(createNodeArray(concatenate(statements, statement.statements)), statement.statements));
23622358
}
23632359
else {
23642360
statements.push(statement);
2361+
return createSyntheticBlockForConvertedStatements(statements);
23652362
}
23662363
}
2364+
}
23672365

2368-
// The old emitter does not emit source maps for the block.
2369-
// We add the location to preserve comments.
2366+
function createSyntheticBlockForConvertedStatements(statements: Statement[]) {
23702367
return setEmitFlags(
2371-
setTextRange(
2372-
createBlock(
2373-
setTextRange(createNodeArray(statements), statementsLocation),
2374-
/*multiLine*/ true
2375-
),
2376-
bodyLocation,
2368+
createBlock(
2369+
createNodeArray(statements),
2370+
/*multiLine*/ true
23772371
),
23782372
EmitFlags.NoSourceMap | EmitFlags.NoTokenSourceMaps
23792373
);

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ sourceFile:ES5For-of1.ts
2424
13> ^^^^^^^^^^^^^^
2525
14> ^^
2626
15> ^^^^
27+
16> ^^
2728
1 >
2829
2 >for (var v of
2930
3 > ['a', 'b', 'c']
@@ -39,6 +40,7 @@ sourceFile:ES5For-of1.ts
3940
13> ['a', 'b', 'c']
4041
14>
4142
15> ['a', 'b', 'c']
43+
16> )
4244
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
4345
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
4446
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
@@ -54,6 +56,7 @@ sourceFile:ES5For-of1.ts
5456
13>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
5557
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
5658
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
59+
16>Emitted(1, 62) Source(1, 32) + SourceIndex(0)
5760
---
5861
>>> var v = _a[_i];
5962
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ sourceFile:ES5For-of13.ts
2424
13> ^^^^^^^^^^^^^^
2525
14> ^^
2626
15> ^^^^
27+
16> ^^
2728
1 >
2829
2 >for (let v of
2930
3 > ['a', 'b', 'c']
@@ -39,6 +40,7 @@ sourceFile:ES5For-of13.ts
3940
13> ['a', 'b', 'c']
4041
14>
4142
15> ['a', 'b', 'c']
43+
16> )
4244
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
4345
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
4446
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
@@ -54,6 +56,7 @@ sourceFile:ES5For-of13.ts
5456
13>Emitted(1, 54) Source(1, 30) + SourceIndex(0)
5557
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
5658
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
59+
16>Emitted(1, 62) Source(1, 32) + SourceIndex(0)
5760
---
5861
>>> var v = _a[_i];
5962
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ sourceFile:ES5For-of25.ts
5757
7 > ^^^^^^^^^^^^^^^
5858
8 > ^^
5959
9 > ^^^^
60+
10> ^^
6061
1->
6162
>
6263
2 >for (var v of
@@ -67,6 +68,7 @@ sourceFile:ES5For-of25.ts
6768
7 > a
6869
8 >
6970
9 > a
71+
10> )
7072
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
7173
2 >Emitted(2, 6) Source(2, 15) + SourceIndex(0)
7274
3 >Emitted(2, 16) Source(2, 16) + SourceIndex(0)
@@ -76,6 +78,7 @@ sourceFile:ES5For-of25.ts
7678
7 >Emitted(2, 42) Source(2, 16) + SourceIndex(0)
7779
8 >Emitted(2, 44) Source(2, 15) + SourceIndex(0)
7880
9 >Emitted(2, 48) Source(2, 16) + SourceIndex(0)
81+
10>Emitted(2, 50) Source(2, 18) + SourceIndex(0)
7982
---
8083
>>> var v = a_1[_i];
8184
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ sourceFile:ES5For-of26.ts
2222
11> ^^^^^^^^^^^^^^
2323
12> ^^
2424
13> ^^^^
25-
14> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
25+
14> ^^
26+
15> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
2627
1 >
2728
2 >for (var [a = 0, b = 1] of
2829
3 > [2, 3]
@@ -36,6 +37,7 @@ sourceFile:ES5For-of26.ts
3637
11> [2, 3]
3738
12>
3839
13> [2, 3]
40+
14> )
3941
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
4042
2 >Emitted(1, 6) Source(1, 28) + SourceIndex(0)
4143
3 >Emitted(1, 16) Source(1, 34) + SourceIndex(0)
@@ -49,6 +51,7 @@ sourceFile:ES5For-of26.ts
4951
11>Emitted(1, 45) Source(1, 34) + SourceIndex(0)
5052
12>Emitted(1, 47) Source(1, 28) + SourceIndex(0)
5153
13>Emitted(1, 51) Source(1, 34) + SourceIndex(0)
54+
14>Emitted(1, 53) Source(1, 36) + SourceIndex(0)
5255
---
5356
>>> var _b = _a[_i], _c = _b[0], a = _c === void 0 ? 0 : _c, _d = _b[1], b = _d === void 0 ? 1 : _d;
5457
1->^^^^

tests/baselines/reference/ES5For-of33.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)