Skip to content

Commit 00e9173

Browse files
committed
Fixed the comments and source map emit interaction.
Fixes #4003
1 parent cf13361 commit 00e9173

File tree

5 files changed

+122
-111
lines changed

5 files changed

+122
-111
lines changed

src/compiler/emitter.ts

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
163163
let writeComment = writeCommentRange;
164164

165165
/** Emit a node */
166-
let emit = emitNodeWithoutSourceMap;
166+
let emit = emitNodeWithCommentsAndWithoutSourcemap;
167167

168168
/** Called just before starting emit of a node */
169169
let emitStart = function (node: Node) { };
@@ -687,9 +687,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
687687
}
688688
}
689689
}
690+
691+
function emitNodeWithCommentsAndWithSourcemap(node: Node) {
692+
emitNodeConsideringCommentsOption(node, emitNodeWithSourceMap);
693+
}
690694

691695
writeEmittedFiles = writeJavaScriptAndSourceMapFile;
692-
emit = emitNodeWithSourceMap;
696+
emit = emitNodeWithCommentsAndWithSourcemap;
693697
emitStart = recordEmitNodeStartSpan;
694698
emitEnd = recordEmitNodeEndSpan;
695699
emitToken = writeTextWithSpanRecord;
@@ -2096,7 +2100,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
20962100
}
20972101

20982102
write(".");
2099-
emitNodeWithoutSourceMap(node.right);
2103+
emit(node.right);
21002104
}
21012105

21022106
function emitEntityNameAsExpression(node: EntityName, useFallback: boolean) {
@@ -2788,7 +2792,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
27882792
emitNodeWithoutSourceMap(counter);
27892793
write(" < ");
27902794

2791-
emitNodeWithoutSourceMap(rhsReference);
2795+
emitNodeWithCommentsAndWithoutSourcemap(rhsReference);
27922796
write(".length");
27932797

27942798
emitEnd(node.initializer);
@@ -2823,7 +2827,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
28232827
else {
28242828
// The following call does not include the initializer, so we have
28252829
// to emit it separately.
2826-
emitNodeWithoutSourceMap(declaration);
2830+
emitNodeWithCommentsAndWithoutSourcemap(declaration);
28272831
write(" = ");
28282832
emitNodeWithoutSourceMap(rhsIterationValue);
28292833
}
@@ -2846,7 +2850,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
28462850
emitDestructuring(assignmentExpression, /*isAssignmentExpressionStatement*/ true, /*value*/ undefined);
28472851
}
28482852
else {
2849-
emitNodeWithoutSourceMap(assignmentExpression);
2853+
emitNodeWithCommentsAndWithoutSourcemap(assignmentExpression);
28502854
}
28512855
}
28522856
emitEnd(node.initializer);
@@ -3002,7 +3006,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30023006
write("exports.");
30033007
}
30043008
}
3005-
emitNodeWithoutSourceMap(node.name);
3009+
emitNodeWithCommentsAndWithoutSourcemap(node.name);
30063010
emitEnd(node.name);
30073011
}
30083012

@@ -3048,7 +3052,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30483052
write("default");
30493053
}
30503054
else {
3051-
emitNodeWithoutSourceMap(node.name);
3055+
emitNodeWithCommentsAndWithoutSourcemap(node.name);
30523056
}
30533057
write(`", `);
30543058
emitDeclarationName(node);
@@ -3082,7 +3086,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30823086
if (compilerOptions.module === ModuleKind.System) {
30833087
emitStart(specifier.name);
30843088
write(`${exportFunctionForFile}("`);
3085-
emitNodeWithoutSourceMap(specifier.name);
3089+
emitNodeWithCommentsAndWithoutSourcemap(specifier.name);
30863090
write(`", `);
30873091
emitExpressionIdentifier(name);
30883092
write(")");
@@ -3092,7 +3096,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30923096
emitStart(specifier.name);
30933097
emitContainingModuleName(specifier);
30943098
write(".");
3095-
emitNodeWithoutSourceMap(specifier.name);
3099+
emitNodeWithCommentsAndWithoutSourcemap(specifier.name);
30963100
emitEnd(specifier.name);
30973101
write(" = ");
30983102
emitExpressionIdentifier(name);
@@ -3139,7 +3143,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
31393143

31403144
if (exportChanged) {
31413145
write(`${exportFunctionForFile}("`);
3142-
emitNodeWithoutSourceMap(name);
3146+
emitNodeWithCommentsAndWithoutSourcemap(name);
31433147
write(`", `);
31443148
}
31453149

@@ -3371,7 +3375,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
33713375

33723376
if (exportChanged) {
33733377
write(`${exportFunctionForFile}("`);
3374-
emitNodeWithoutSourceMap(node.name);
3378+
emitNodeWithCommentsAndWithoutSourcemap(node.name);
33753379
write(`", `);
33763380
}
33773381

@@ -3527,9 +3531,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
35273531
emitEnd(parameter);
35283532
write(" { ");
35293533
emitStart(parameter);
3530-
emitNodeWithoutSourceMap(paramName);
3534+
emitNodeWithCommentsAndWithoutSourcemap(paramName);
35313535
write(" = ");
3532-
emitNodeWithoutSourceMap(initializer);
3536+
emitNodeWithCommentsAndWithoutSourcemap(initializer);
35333537
emitEnd(parameter);
35343538
write("; }");
35353539
}
@@ -3552,7 +3556,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
35523556
emitLeadingComments(restParam);
35533557
emitStart(restParam);
35543558
write("var ");
3555-
emitNodeWithoutSourceMap(restParam.name);
3559+
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
35563560
write(" = [];");
35573561
emitEnd(restParam);
35583562
emitTrailingComments(restParam);
@@ -3573,7 +3577,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
35733577
increaseIndent();
35743578
writeLine();
35753579
emitStart(restParam);
3576-
emitNodeWithoutSourceMap(restParam.name);
3580+
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
35773581
write("[" + tempName + " - " + restIndex + "] = arguments[" + tempName + "];");
35783582
emitEnd(restParam);
35793583
decreaseIndent();
@@ -3594,7 +3598,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
35943598

35953599
function emitDeclarationName(node: Declaration) {
35963600
if (node.name) {
3597-
emitNodeWithoutSourceMap(node.name);
3601+
emitNodeWithCommentsAndWithoutSourcemap(node.name);
35983602
}
35993603
else {
36003604
write(getGeneratedNameForNode(node));
@@ -3622,6 +3626,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
36223626
emitLeadingComments(node);
36233627
}
36243628

3629+
emitStart(node);
36253630
// For targeting below es6, emit functions-like declaration including arrow function using function keyword.
36263631
// When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead
36273632
if (!shouldEmitAsArrowFunction(node)) {
@@ -3647,6 +3652,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
36473652
if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.FunctionDeclaration && node.parent === currentSourceFile && node.name) {
36483653
emitExportMemberAssignments((<FunctionDeclaration>node).name);
36493654
}
3655+
3656+
emitEnd(node);
36503657
if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) {
36513658
emitTrailingComments(node);
36523659
}
@@ -4002,18 +4009,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
40024009
}
40034010

40044011
function emitMemberAccessForPropertyName(memberName: DeclarationName) {
4005-
// TODO: (jfreeman,drosen): comment on why this is emitNodeWithoutSourceMap instead of emit here.
4012+
// This does not emit source map because it is emitted by caller as caller
4013+
// is aware how the property name changes to the property access
4014+
// eg. public x = 10; becomes this.x and static x = 10 becomes className.x
40064015
if (memberName.kind === SyntaxKind.StringLiteral || memberName.kind === SyntaxKind.NumericLiteral) {
40074016
write("[");
4008-
emitNodeWithoutSourceMap(memberName);
4017+
emitNodeWithCommentsAndWithoutSourcemap(memberName);
40094018
write("]");
40104019
}
40114020
else if (memberName.kind === SyntaxKind.ComputedPropertyName) {
40124021
emitComputedPropertyName(<ComputedPropertyName>memberName);
40134022
}
40144023
else {
40154024
write(".");
4016-
emitNodeWithoutSourceMap(memberName);
4025+
emitNodeWithCommentsAndWithoutSourcemap(memberName);
40174026
}
40184027
}
40194028

@@ -4081,10 +4090,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
40814090
emitMemberAccessForPropertyName((<MethodDeclaration>member).name);
40824091
emitEnd((<MethodDeclaration>member).name);
40834092
write(" = ");
4084-
emitStart(member);
40854093
emitFunctionDeclaration(<MethodDeclaration>member);
40864094
emitEnd(member);
4087-
emitEnd(member);
40884095
write(";");
40894096
emitTrailingComments(member);
40904097
}
@@ -5501,11 +5508,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
55015508
emitStart(specifier);
55025509
emitContainingModuleName(specifier);
55035510
write(".");
5504-
emitNodeWithoutSourceMap(specifier.name);
5511+
emitNodeWithCommentsAndWithoutSourcemap(specifier.name);
55055512
write(" = ");
55065513
write(generatedName);
55075514
write(".");
5508-
emitNodeWithoutSourceMap(specifier.propertyName || specifier.name);
5515+
emitNodeWithCommentsAndWithoutSourcemap(specifier.propertyName || specifier.name);
55095516
write(";");
55105517
emitEnd(specifier);
55115518
}
@@ -5528,7 +5535,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
55285535
}
55295536
else {
55305537
if (!node.exportClause || resolver.isValueAliasDeclaration(node)) {
5531-
emitStart(node);
55325538
write("export ");
55335539
if (node.exportClause) {
55345540
// export { x, y, ... }
@@ -5541,10 +5547,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
55415547
}
55425548
if (node.moduleSpecifier) {
55435549
write(" from ");
5544-
emitNodeWithoutSourceMap(node.moduleSpecifier);
5550+
emit(node.moduleSpecifier);
55455551
}
55465552
write(";");
5547-
emitEnd(node);
55485553
}
55495554
}
55505555
}
@@ -5558,13 +5563,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
55585563
if (needsComma) {
55595564
write(", ");
55605565
}
5561-
emitStart(specifier);
55625566
if (specifier.propertyName) {
5563-
emitNodeWithoutSourceMap(specifier.propertyName);
5567+
emit(specifier.propertyName);
55645568
write(" as ");
55655569
}
5566-
emitNodeWithoutSourceMap(specifier.name);
5567-
emitEnd(specifier);
5570+
emit(specifier.name);
55685571
needsComma = true;
55695572
}
55705573
}
@@ -5851,7 +5854,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
58515854
writeLine();
58525855
write("'");
58535856
if (node.kind === SyntaxKind.Identifier) {
5854-
emitNodeWithoutSourceMap(node);
5857+
emitNodeWithCommentsAndWithoutSourcemap(node);
58555858
}
58565859
else {
58575860
emitDeclarationName(<Declaration>node);
@@ -6178,9 +6181,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
61786181
writeLine();
61796182
for (let e of (<ExportDeclaration>importNode).exportClause.elements) {
61806183
write(`${reexportsVariableName}["`);
6181-
emitNodeWithoutSourceMap(e.name);
6184+
emitNodeWithCommentsAndWithoutSourcemap(e.name);
61826185
write(`"] = ${parameterName}["`);
6183-
emitNodeWithoutSourceMap(e.propertyName || e.name);
6186+
emitNodeWithCommentsAndWithoutSourcemap(e.propertyName || e.name);
61846187
write(`"];`);
61856188
writeLine();
61866189
}
@@ -6594,28 +6597,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
65946597
emitLeadingComments(node.endOfFileToken);
65956598
}
65966599

6597-
function emitNodeWithoutSourceMap(node: Node): void {
6598-
if (!node) {
6599-
return;
6600-
}
6600+
function emitNodeWithCommentsAndWithoutSourcemap(node: Node): void {
6601+
emitNodeConsideringCommentsOption(node, emitNodeWithoutSourceMap);
6602+
}
66016603

6602-
if (node.flags & NodeFlags.Ambient) {
6603-
return emitOnlyPinnedOrTripleSlashComments(node);
6604-
}
6604+
function emitNodeConsideringCommentsOption(node: Node, emitNodeConsideringSourcemap: (node: Node) => void): void {
6605+
if (node) {
6606+
if (node.flags & NodeFlags.Ambient) {
6607+
return emitOnlyPinnedOrTripleSlashComments(node);
6608+
}
66056609

6606-
let emitComments = shouldEmitLeadingAndTrailingComments(node);
6607-
if (emitComments) {
6608-
emitLeadingComments(node);
6609-
}
6610+
if (isSpecializedCommentHandling(node)) {
6611+
// This is the node that will handle its own comments and sourcemap
6612+
return emitNodeWithoutSourceMap(node);
6613+
}
6614+
6615+
let emitComments = shouldEmitLeadingAndTrailingComments(node);
6616+
if (emitComments) {
6617+
emitLeadingComments(node);
6618+
}
66106619

6611-
emitJavaScriptWorker(node);
6620+
emitNodeConsideringSourcemap(node);
66126621

6613-
if (emitComments) {
6614-
emitTrailingComments(node);
6622+
if (emitComments) {
6623+
emitTrailingComments(node);
6624+
}
66156625
}
66166626
}
66176627

6618-
function shouldEmitLeadingAndTrailingComments(node: Node) {
6628+
function emitNodeWithoutSourceMap(node: Node): void {
6629+
if (node) {
6630+
emitJavaScriptWorker(node);
6631+
}
6632+
}
6633+
6634+
function isSpecializedCommentHandling(node: Node): boolean {
66196635
switch (node.kind) {
66206636
// All of these entities are emitted in a specialized fashion. As such, we allow
66216637
// the specialized methods for each to handle the comments on the nodes.
@@ -6625,8 +6641,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
66256641
case SyntaxKind.ImportEqualsDeclaration:
66266642
case SyntaxKind.TypeAliasDeclaration:
66276643
case SyntaxKind.ExportAssignment:
6628-
return false;
6644+
return true;
6645+
}
6646+
}
66296647

6648+
function shouldEmitLeadingAndTrailingComments(node: Node) {
6649+
switch (node.kind) {
66306650
case SyntaxKind.VariableStatement:
66316651
return shouldEmitLeadingAndTrailingCommentsForVariableStatement(<VariableStatement>node);
66326652

@@ -6641,6 +6661,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
66416661
return shouldEmitEnumDeclaration(<EnumDeclaration>node);
66426662
}
66436663

6664+
// If the node is emitted in specialized fashion, dont emit comments as this node will handle
6665+
// emitting comments when emitting itself
6666+
Debug.assert(!isSpecializedCommentHandling(node));
6667+
66446668
// If this is the expression body of an arrow function that we're down-leveling,
66456669
// then we don't want to emit comments when we emit the body. It will have already
66466670
// been taken care of when we emitted the 'return' statement for the function

0 commit comments

Comments
 (0)