Skip to content

Commit fd2eb49

Browse files
authored
Merge pull request microsoft#25604 from mprobst/fix-exported-var-comments
Retain synthetic comments on exports.
2 parents 23eb591 + 7d44014 commit fd2eb49

11 files changed

+253
-89
lines changed

src/compiler/comments.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ namespace ts {
7272
const savedContainerEnd = containerEnd;
7373
const savedDeclarationListContainerEnd = declarationListContainerEnd;
7474

75-
if (!skipLeadingComments) {
75+
if (!skipLeadingComments || (pos >= 0 && (emitFlags & EmitFlags.NoLeadingComments) !== 0)) {
76+
// Advance the container position of comments get emitted or if they've been disabled explicitly using NoLeadingComments.
7677
containerPos = pos;
7778
}
7879

79-
if (!skipTrailingComments) {
80+
if (!skipTrailingComments || (end >= 0 && (emitFlags & EmitFlags.NoTrailingComments) !== 0)) {
81+
// As above.
8082
containerEnd = end;
8183

8284
// To avoid invalid comment emit in a down-level binding pattern, we
@@ -426,4 +428,4 @@ namespace ts {
426428
return isRecognizedTripleSlashComment(currentText, commentPos, commentEnd);
427429
}
428430
}
429-
}
431+
}

src/compiler/transformers/es2015.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ namespace ts {
11211121
}
11221122

11231123
// Perform the capture.
1124-
captureThisForNode(statements, ctor, superCallExpression || createActualThis(), firstStatement);
1124+
captureThisForNode(statements, ctor, superCallExpression || createActualThis());
11251125

11261126
// If we're actually replacing the original statement, we need to signal this to the caller.
11271127
if (superCallExpression) {
@@ -1443,7 +1443,7 @@ namespace ts {
14431443
}
14441444
}
14451445

1446-
function captureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined, originalStatement?: Statement): void {
1446+
function captureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void {
14471447
enableSubstitutionsForCapturedThis();
14481448
const captureThisStatement = createVariableStatement(
14491449
/*modifiers*/ undefined,
@@ -1456,7 +1456,6 @@ namespace ts {
14561456
])
14571457
);
14581458
setEmitFlags(captureThisStatement, EmitFlags.NoComments | EmitFlags.CustomPrologue);
1459-
setTextRange(captureThisStatement, originalStatement);
14601459
setSourceMapRange(captureThisStatement, node);
14611460
statements.push(captureThisStatement);
14621461
}

src/compiler/transformers/module/module.ts

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ namespace ts {
767767
if (moduleKind !== ModuleKind.AMD) {
768768
if (!node.importClause) {
769769
// import "mod";
770-
return setTextRange(createExpressionStatement(createRequireCall(node)), node);
770+
return setOriginalNode(setTextRange(createExpressionStatement(createRequireCall(node)), node), node);
771771
}
772772
else {
773773
const variables: VariableDeclaration[] = [];
@@ -806,15 +806,17 @@ namespace ts {
806806
}
807807

808808
statements = append(statements,
809-
setTextRange(
810-
createVariableStatement(
811-
/*modifiers*/ undefined,
812-
createVariableDeclarationList(
813-
variables,
814-
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
815-
)
816-
),
817-
/*location*/ node
809+
setOriginalNode(
810+
setTextRange(
811+
createVariableStatement(
812+
/*modifiers*/ undefined,
813+
createVariableDeclarationList(
814+
variables,
815+
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
816+
)
817+
),
818+
/*location*/ node),
819+
/*original*/ node
818820
)
819821
);
820822
}
@@ -826,13 +828,15 @@ namespace ts {
826828
/*modifiers*/ undefined,
827829
createVariableDeclarationList(
828830
[
829-
setTextRange(
830-
createVariableDeclaration(
831-
getSynthesizedClone(namespaceDeclaration.name),
832-
/*type*/ undefined,
833-
getGeneratedNameForNode(node)
834-
),
835-
/*location*/ node
831+
setOriginalNode(
832+
setTextRange(
833+
createVariableDeclaration(
834+
getSynthesizedClone(namespaceDeclaration.name),
835+
/*type*/ undefined,
836+
getGeneratedNameForNode(node)
837+
),
838+
/*location*/ node),
839+
/*original*/ node
836840
)
837841
],
838842
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
@@ -880,33 +884,37 @@ namespace ts {
880884
if (moduleKind !== ModuleKind.AMD) {
881885
if (hasModifier(node, ModifierFlags.Export)) {
882886
statements = append(statements,
883-
setTextRange(
884-
createExpressionStatement(
885-
createExportExpression(
886-
node.name,
887-
createRequireCall(node)
888-
)
889-
),
887+
setOriginalNode(
888+
setTextRange(
889+
createExpressionStatement(
890+
createExportExpression(
891+
node.name,
892+
createRequireCall(node)
893+
)
894+
),
895+
node),
890896
node
891897
)
892898
);
893899
}
894900
else {
895901
statements = append(statements,
896-
setTextRange(
897-
createVariableStatement(
898-
/*modifiers*/ undefined,
899-
createVariableDeclarationList(
900-
[
901-
createVariableDeclaration(
902-
getSynthesizedClone(node.name),
903-
/*type*/ undefined,
904-
createRequireCall(node)
905-
)
906-
],
907-
/*flags*/ languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
908-
)
909-
),
902+
setOriginalNode(
903+
setTextRange(
904+
createVariableStatement(
905+
/*modifiers*/ undefined,
906+
createVariableDeclarationList(
907+
[
908+
createVariableDeclaration(
909+
getSynthesizedClone(node.name),
910+
/*type*/ undefined,
911+
createRequireCall(node)
912+
)
913+
],
914+
/*flags*/ languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
915+
)
916+
),
917+
node),
910918
node
911919
)
912920
);
@@ -915,10 +923,12 @@ namespace ts {
915923
else {
916924
if (hasModifier(node, ModifierFlags.Export)) {
917925
statements = append(statements,
918-
setTextRange(
919-
createExpressionStatement(
920-
createExportExpression(getExportName(node), getLocalName(node))
921-
),
926+
setOriginalNode(
927+
setTextRange(
928+
createExpressionStatement(
929+
createExportExpression(getExportName(node), getLocalName(node))
930+
),
931+
node),
922932
node
923933
)
924934
);
@@ -956,18 +966,20 @@ namespace ts {
956966
// export { x, y } from "mod";
957967
if (moduleKind !== ModuleKind.AMD) {
958968
statements.push(
959-
setTextRange(
960-
createVariableStatement(
961-
/*modifiers*/ undefined,
962-
createVariableDeclarationList([
963-
createVariableDeclaration(
964-
generatedName,
965-
/*type*/ undefined,
966-
createRequireCall(node)
967-
)
968-
])
969-
),
970-
/*location*/ node
969+
setOriginalNode(
970+
setTextRange(
971+
createVariableStatement(
972+
/*modifiers*/ undefined,
973+
createVariableDeclarationList([
974+
createVariableDeclaration(
975+
generatedName,
976+
/*type*/ undefined,
977+
createRequireCall(node)
978+
)
979+
])
980+
),
981+
/*location*/ node),
982+
/* original */ node
971983
)
972984
);
973985
}
@@ -977,10 +989,12 @@ namespace ts {
977989
specifier.propertyName || specifier.name
978990
);
979991
statements.push(
980-
setTextRange(
981-
createExpressionStatement(
982-
createExportExpression(getExportName(specifier), exportedValue)
983-
),
992+
setOriginalNode(
993+
setTextRange(
994+
createExpressionStatement(
995+
createExportExpression(getExportName(specifier), exportedValue)
996+
),
997+
specifier),
984998
specifier
985999
)
9861000
);
@@ -990,10 +1004,12 @@ namespace ts {
9901004
}
9911005
else {
9921006
// export * from "mod";
993-
return setTextRange(
994-
createExpressionStatement(
995-
createExportStarHelper(context, moduleKind !== ModuleKind.AMD ? createRequireCall(node) : generatedName)
996-
),
1007+
return setOriginalNode(
1008+
setTextRange(
1009+
createExpressionStatement(
1010+
createExportStarHelper(context, moduleKind !== ModuleKind.AMD ? createRequireCall(node) : generatedName)
1011+
),
1012+
node),
9971013
node
9981014
);
9991015
}
@@ -1140,7 +1156,7 @@ namespace ts {
11401156
}
11411157

11421158
if (expressions) {
1143-
statements = append(statements, setTextRange(createExpressionStatement(inlineExpressions(expressions)), node));
1159+
statements = append(statements, setOriginalNode(setTextRange(createExpressionStatement(inlineExpressions(expressions)), node), node));
11441160
}
11451161
}
11461162
else {

src/compiler/transformers/ts.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ namespace ts {
12451245
const statement = createExpressionStatement(transformInitializedProperty(property, receiver));
12461246
setSourceMapRange(statement, moveRangePastModifiers(property));
12471247
setCommentRange(statement, property);
1248+
setOriginalNode(statement, property);
12481249
statements.push(statement);
12491250
}
12501251
}
@@ -1262,6 +1263,7 @@ namespace ts {
12621263
startOnNewLine(expression);
12631264
setSourceMapRange(expression, moveRangePastModifiers(property));
12641265
setCommentRange(expression, property);
1266+
setOriginalNode(expression, property);
12651267
expressions.push(expression);
12661268
}
12671269

@@ -2631,7 +2633,8 @@ namespace ts {
26312633
// If needed, we should emit a variable declaration for the enum. If we emit
26322634
// a leading variable declaration, we should not emit leading comments for the
26332635
// enum body.
2634-
if (addVarForEnumOrModuleDeclaration(statements, node)) {
2636+
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
2637+
if (varAdded) {
26352638
// We should still emit the comments if we are emitting a system module.
26362639
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
26372640
emitFlags |= EmitFlags.NoLeadingComments;
@@ -2689,8 +2692,13 @@ namespace ts {
26892692
);
26902693

26912694
setOriginalNode(enumStatement, node);
2695+
if (varAdded) {
2696+
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2697+
setSyntheticLeadingComments(enumStatement, undefined);
2698+
setSyntheticTrailingComments(enumStatement, undefined);
2699+
}
26922700
setTextRange(enumStatement, node);
2693-
setEmitFlags(enumStatement, emitFlags);
2701+
addEmitFlags(enumStatement, emitFlags);
26942702
statements.push(enumStatement);
26952703

26962704
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
@@ -2880,7 +2888,7 @@ namespace ts {
28802888
// })(m1 || (m1 = {})); // trailing comment module
28812889
//
28822890
setCommentRange(statement, node);
2883-
setEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
2891+
addEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
28842892
statements.push(statement);
28852893
return true;
28862894
}
@@ -2920,7 +2928,8 @@ namespace ts {
29202928
// If needed, we should emit a variable declaration for the module. If we emit
29212929
// a leading variable declaration, we should not emit leading comments for the
29222930
// module body.
2923-
if (addVarForEnumOrModuleDeclaration(statements, node)) {
2931+
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
2932+
if (varAdded) {
29242933
// We should still emit the comments if we are emitting a system module.
29252934
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
29262935
emitFlags |= EmitFlags.NoLeadingComments;
@@ -2977,8 +2986,13 @@ namespace ts {
29772986
);
29782987

29792988
setOriginalNode(moduleStatement, node);
2989+
if (varAdded) {
2990+
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2991+
setSyntheticLeadingComments(moduleStatement, undefined);
2992+
setSyntheticTrailingComments(moduleStatement, undefined);
2993+
}
29802994
setTextRange(moduleStatement, node);
2981-
setEmitFlags(moduleStatement, emitFlags);
2995+
addEmitFlags(moduleStatement, emitFlags);
29822996
statements.push(moduleStatement);
29832997

29842998
// Add a DeclarationMarker for the namespace to preserve trailing comments and mark

0 commit comments

Comments
 (0)