Skip to content

Commit 9e2ebf6

Browse files
committed
Adjusts source map emit for classes and down-level rest parameters
1 parent 43914ff commit 9e2ebf6

8 files changed

Lines changed: 98 additions & 66 deletions

File tree

src/compiler/printer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,29 @@ const _super = (function (geti, seti) {
13771377
}
13781378

13791379
function emitForStatement(node: ForStatement) {
1380+
if (getNodeEmitFlags(node) & NodeEmitFlags.SourceMapAdjustRestParameterLoop) {
1381+
// TODO(rbuckton): This should be removed once source maps are aligned with the old
1382+
// emitter and new baselines are taken. This exists solely to
1383+
// align with the old emitter.
1384+
const openParenPos = writeToken(SyntaxKind.ForKeyword, node.pos);
1385+
write(" ");
1386+
writeToken(SyntaxKind.OpenParenToken, openParenPos);
1387+
setNodeEmitFlags(node.initializer, NodeEmitFlags.NoTrailingSourceMap | getNodeEmitFlags(node.initializer));
1388+
emitForBinding(node.initializer);
1389+
write(";");
1390+
emitEnd(node.initializer);
1391+
setNodeEmitFlags(node.condition, NodeEmitFlags.NoTrailingSourceMap | getNodeEmitFlags(node.condition));
1392+
write(" ");
1393+
emitExpression(node.condition);
1394+
write(";");
1395+
emitEnd(node.condition);
1396+
write(" ");
1397+
emitExpression(node.incrementor);
1398+
write(")");
1399+
emitEmbeddedStatement(node.statement);
1400+
return;
1401+
}
1402+
13801403
const openParenPos = writeToken(SyntaxKind.ForKeyword, node.pos);
13811404
write(" ");
13821405
writeToken(SyntaxKind.OpenParenToken, openParenPos);

src/compiler/transformers/es6.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,20 @@ namespace ts {
544544
// return C;
545545
// }());
546546

547-
return startOnNewLine(
548-
createVariableStatement(
549-
/*modifiers*/ undefined,
550-
createVariableDeclarationList([
551-
createVariableDeclaration(
552-
getDeclarationName(node, /*allowComments*/ true),
553-
transformClassLikeDeclarationToExpression(node)
554-
)
555-
]),
556-
node
557-
)
547+
const statement = createVariableStatement(
548+
/*modifiers*/ undefined,
549+
createVariableDeclarationList([
550+
createVariableDeclaration(
551+
getDeclarationName(node, /*allowComments*/ true),
552+
transformClassLikeDeclarationToExpression(node)
553+
)
554+
]),
555+
/*location*/ node
558556
);
557+
558+
setOriginalNode(statement, node);
559+
startOnNewLine(statement);
560+
return statement;
559561
}
560562

561563
/**
@@ -1013,33 +1015,35 @@ namespace ts {
10131015
// for (var _i = restIndex; _i < arguments.length; _i++) {
10141016
// param[_i - restIndex] = arguments[_i];
10151017
// }
1016-
statements.push(
1017-
createFor(
1018-
createVariableDeclarationList([
1019-
createVariableDeclaration(temp, createLiteral(restIndex))
1020-
], /*location*/ parameter),
1021-
createLessThan(
1022-
temp,
1023-
createPropertyAccess(createIdentifier("arguments"), "length"),
1024-
/*location*/ parameter
1025-
),
1026-
createPostfixIncrement(temp, /*location*/ parameter),
1027-
createBlock([
1028-
startOnNewLine(
1029-
createStatement(
1030-
createAssignment(
1031-
createElementAccess(
1032-
expressionName,
1033-
createSubtract(temp, createLiteral(restIndex))
1034-
),
1035-
createElementAccess(createIdentifier("arguments"), temp)
1018+
const forStatement = createFor(
1019+
createVariableDeclarationList([
1020+
createVariableDeclaration(temp, createLiteral(restIndex))
1021+
], /*location*/ parameter),
1022+
createLessThan(
1023+
temp,
1024+
createPropertyAccess(createIdentifier("arguments"), "length"),
1025+
/*location*/ parameter
1026+
),
1027+
createPostfixIncrement(temp, /*location*/ parameter),
1028+
createBlock([
1029+
startOnNewLine(
1030+
createStatement(
1031+
createAssignment(
1032+
createElementAccess(
1033+
expressionName,
1034+
createSubtract(temp, createLiteral(restIndex))
10361035
),
1037-
/*location*/ parameter
1038-
)
1036+
createElementAccess(createIdentifier("arguments"), temp)
1037+
),
1038+
/*location*/ parameter
10391039
)
1040-
])
1041-
)
1040+
)
1041+
])
10421042
);
1043+
1044+
setNodeEmitFlags(forStatement, NodeEmitFlags.SourceMapAdjustRestParameterLoop);
1045+
startOnNewLine(forStatement);
1046+
statements.push(forStatement);
10431047
}
10441048

10451049
/**

src/compiler/transformers/ts.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,18 +448,22 @@ namespace ts {
448448
// ${modifiers} class ${name} ${heritageClauses} {
449449
// ${members}
450450
// }
451-
addNode(statements,
452-
setOriginalNode(
453-
createClassDeclaration(
454-
visitNodes(node.modifiers, visitor, isModifier),
455-
name,
456-
visitNodes(node.heritageClauses, visitor, isHeritageClause),
457-
transformClassMembers(node, hasExtendsClause),
458-
/*location*/ node
459-
),
460-
node
461-
)
451+
const classDeclaration = createClassDeclaration(
452+
visitNodes(node.modifiers, visitor, isModifier),
453+
name,
454+
visitNodes(node.heritageClauses, visitor, isHeritageClause),
455+
transformClassMembers(node, hasExtendsClause),
456+
/*location*/ node
462457
);
458+
setOriginalNode(classDeclaration, node);
459+
460+
// To better align with the old emitter, we should not emit a trailing source map
461+
// entry if the class has static properties.
462+
if (staticProperties.length > 0) {
463+
setNodeEmitFlags(classDeclaration, NodeEmitFlags.NoTrailingSourceMap | getNodeEmitFlags(classDeclaration));
464+
}
465+
466+
statements.push(classDeclaration);
463467
}
464468
else {
465469
decoratedClassAlias = addClassDeclarationHeadWithDecorators(statements, node, name, hasExtendsClause);

src/compiler/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,10 +2943,11 @@ namespace ts {
29432943
Indented = 1 << 18, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
29442944

29452945
// SourceMap Specialization.
2946-
// TODO(rbuckton): This should be removed once source maps are aligned with the old
2946+
// TODO(rbuckton): These should be removed once source maps are aligned with the old
29472947
// emitter and new baselines are taken. This exists solely to
29482948
// align with the old emitter.
2949-
SourceMapEmitOpenBraceAsToken = 1 << 19,
2949+
SourceMapEmitOpenBraceAsToken = 1 << 19, // Emits the open brace of a block function body as a source mapped token.
2950+
SourceMapAdjustRestParameterLoop = 1 << 20, // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3.
29502951
}
29512952

29522953
/** Additional context provided to `visitEachChild` */

tests/baselines/reference/sourceMapValidationClass.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/sourceMapValidationClass.sourcemap.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ sourceFile:sourceMapValidationClass.ts
3737
---
3838
>>> for (var _i = 1; _i < arguments.length; _i++) {
3939
1->^^^^^^^^^^^^^
40-
2 > ^^^^^^^^^^
41-
3 > ^^
42-
4 > ^^^^^^^^^^^^^^^^^^^^^
43-
5 > ^^
40+
2 > ^^^^^^^^^^^
41+
3 > ^
42+
4 > ^^^^^^^^^^^^^^^^^^^^^^
43+
5 > ^
4444
6 > ^^^^
4545
1->
4646
2 > ...b: string[]
47-
3 >
47+
3 >
4848
4 > ...b: string[]
49-
5 >
49+
5 >
5050
6 > ...b: string[]
5151
1->Emitted(4, 14) Source(2, 42) + SourceIndex(0)
52-
2 >Emitted(4, 24) Source(2, 56) + SourceIndex(0)
52+
2 >Emitted(4, 25) Source(2, 56) + SourceIndex(0)
5353
3 >Emitted(4, 26) Source(2, 42) + SourceIndex(0)
54-
4 >Emitted(4, 47) Source(2, 56) + SourceIndex(0)
54+
4 >Emitted(4, 48) Source(2, 56) + SourceIndex(0)
5555
5 >Emitted(4, 49) Source(2, 42) + SourceIndex(0)
5656
6 >Emitted(4, 53) Source(2, 56) + SourceIndex(0)
5757
---

tests/baselines/reference/sourceMapValidationClasses.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/sourceMapValidationClasses.sourcemap.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,21 @@ sourceFile:sourceMapValidationClasses.ts
439439
---
440440
>>> for (var _i = 1; _i < arguments.length; _i++) {
441441
1->^^^^^^^^^^^^^^^^^
442-
2 > ^^^^^^^^^^
443-
3 > ^^
444-
4 > ^^^^^^^^^^^^^^^^^^^^^
445-
5 > ^^
442+
2 > ^^^^^^^^^^^
443+
3 > ^
444+
4 > ^^^^^^^^^^^^^^^^^^^^^^
445+
5 > ^
446446
6 > ^^^^
447447
1->
448448
2 > ...restGreetings /* more greeting */: string[]
449-
3 >
449+
3 >
450450
4 > ...restGreetings /* more greeting */: string[]
451-
5 >
451+
5 >
452452
6 > ...restGreetings /* more greeting */: string[]
453453
1->Emitted(22, 18) Source(21, 37) + SourceIndex(0)
454-
2 >Emitted(22, 28) Source(21, 83) + SourceIndex(0)
454+
2 >Emitted(22, 29) Source(21, 83) + SourceIndex(0)
455455
3 >Emitted(22, 30) Source(21, 37) + SourceIndex(0)
456-
4 >Emitted(22, 51) Source(21, 83) + SourceIndex(0)
456+
4 >Emitted(22, 52) Source(21, 83) + SourceIndex(0)
457457
5 >Emitted(22, 53) Source(21, 37) + SourceIndex(0)
458458
6 >Emitted(22, 57) Source(21, 83) + SourceIndex(0)
459459
---

0 commit comments

Comments
 (0)