Skip to content

Commit ff9ccc9

Browse files
committed
Undo scanner/emitter changes
1 parent 21439a4 commit ff9ccc9

File tree

3 files changed

+38
-63
lines changed

3 files changed

+38
-63
lines changed

src/compiler/emitter.ts

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
851851
}
852852
}
853853

854-
function emitLinePreservingList(parent: Node, nodes: NodeArray<Node>, allowTrailingComma: boolean, spacesBetweenBraces: boolean, commentsBeforePunctuation: boolean) {
854+
function emitLinePreservingList(parent: Node, nodes: NodeArray<Node>, allowTrailingComma: boolean, spacesBetweenBraces: boolean) {
855855
Debug.assert(nodes.length > 0);
856856

857857
increaseIndent();
@@ -877,9 +877,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
877877
}
878878

879879
emit(nodes[i]);
880-
if (commentsBeforePunctuation) {
881-
emitLeadingCommentsAtEnd(nodes[i]);
882-
}
883880
}
884881

885882
if (nodes.hasTrailingComma && allowTrailingComma) {
@@ -898,7 +895,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
898895
}
899896
}
900897

901-
function emitList<TNode extends Node>(nodes: TNode[], start: number, count: number, multiLine: boolean, trailingComma: boolean, leadingComma?: boolean, noTrailingNewLine?: boolean, commentsBeforePunctuation?: boolean, emitNode?: (node: TNode) => void): number {
898+
function emitList<TNode extends Node>(nodes: TNode[], start: number, count: number, multiLine: boolean, trailingComma: boolean, leadingComma?: boolean, noTrailingNewLine?: boolean, emitNode?: (node: TNode) => void): number {
902899
if (!emitNode) {
903900
emitNode = emit;
904901
}
@@ -916,10 +913,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
916913
}
917914
}
918915
const node = nodes[start + i];
916+
// This emitting is to make sure we emit following comment properly
917+
// ...(x, /*comment1*/ y)...
918+
// ^ => node.pos
919+
// "comment1" is not considered leading comment for "y" but rather
920+
// considered as trailing comment of the previous node.
921+
emitTrailingCommentsOfPosition(node.pos);
919922
emitNode(node);
920-
if (commentsBeforePunctuation) {
921-
emitLeadingCommentsAtEnd(nodes[i]);
922-
}
923923
leadingComma = true;
924924
}
925925
if (trailingComma) {
@@ -1897,7 +1897,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
18971897
}
18981898
else if (languageVersion >= ScriptTarget.ES6 || !forEach(elements, isSpreadElementExpression)) {
18991899
write("[");
1900-
emitLinePreservingList(node, node.elements, elements.hasTrailingComma, /*spacesBetweenBraces*/ false, /*commentsBeforePunctuation*/ true);
1900+
emitLinePreservingList(node, node.elements, elements.hasTrailingComma, /*spacesBetweenBraces*/ false);
19011901
write("]");
19021902
}
19031903
else {
@@ -1921,7 +1921,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19211921
// then try to preserve the original shape of the object literal.
19221922
// Otherwise just try to preserve the formatting.
19231923
if (numElements === properties.length) {
1924-
emitLinePreservingList(node, properties, /*allowTrailingComma*/ languageVersion >= ScriptTarget.ES5, /*spacesBetweenBraces*/ true, /*commentsBeforePunctuation*/ true);
1924+
emitLinePreservingList(node, properties, /*allowTrailingComma*/ languageVersion >= ScriptTarget.ES5, /*spacesBetweenBraces*/ true);
19251925
}
19261926
else {
19271927
const multiLine = node.multiLine;
@@ -2166,6 +2166,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
21662166
function emitPropertyAssignment(node: PropertyDeclaration) {
21672167
emit(node.name);
21682168
write(": ");
2169+
// This is to ensure that we emit comment in the following case:
2170+
// For example:
2171+
// obj = {
2172+
// id: /*comment1*/ ()=>void
2173+
// }
2174+
// "comment1" is not considered to be leading comment for node.initializer
2175+
// but rather a trailing comment on the previous node.
2176+
emitTrailingCommentsOfPosition(node.initializer.pos);
21692177
emit(node.initializer);
21702178
}
21712179

@@ -2277,7 +2285,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22772285
}
22782286

22792287
emit(node.expression);
2280-
emitLeadingCommentsAtEnd(node.expression);
22812288
const dotRangeStart = nodeIsSynthesized(node.expression) ? -1 : node.expression.end;
22822289
const dotRangeEnd = nodeIsSynthesized(node.expression) ? -1 : skipTrivia(currentText, node.expression.end) + 1;
22832290
const dotToken = <TextRange>{ pos: dotRangeStart, end: dotRangeEnd };
@@ -2494,17 +2501,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
24942501
emitThis(expression);
24952502
if (node.arguments.length) {
24962503
write(", ");
2497-
emitTrailingCommentsOfPosition(node.arguments.pos);
24982504
emitCommaList(node.arguments);
2499-
emitLeadingCommentsAtEnd(node.arguments);
25002505
}
25012506
write(")");
25022507
}
25032508
else {
25042509
write("(");
2505-
emitTrailingCommentsOfPosition(node.arguments.pos);
25062510
emitCommaList(node.arguments);
2507-
emitLeadingCommentsAtEnd(node.arguments);
25082511
write(")");
25092512
}
25102513
}
@@ -2605,7 +2608,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
26052608

26062609
write("(");
26072610
emit(node.expression);
2608-
emitLeadingCommentsAtEnd(node.expression);
26092611
write(")");
26102612
}
26112613

@@ -2884,7 +2886,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
28842886
}
28852887
else {
28862888
emit(node.left);
2887-
emitLeadingCommentsAtEnd(node.left);
28882889
// Add indentation before emit the operator if the operator is on different line
28892890
// For example:
28902891
// 3
@@ -3897,7 +3898,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
38973898
}
38983899
emitNodeWithCommentsAndWithoutSourcemap(node.name);
38993900
emitEnd(node.name);
3900-
emitLeadingCommentsAtEnd(node.name);
39013901
}
39023902

39033903
function createVoidZero(): Expression {
@@ -4040,7 +4040,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
40404040
emit(name);
40414041
}
40424042

4043-
emitLeadingCommentsAtEnd(name);
40444043
write(" = ");
40454044
emit(value);
40464045
});
@@ -4592,7 +4591,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
45924591
emitStart(restParam);
45934592
write("var ");
45944593
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
4595-
emitLeadingCommentsAtEnd(restParam.name);
45964594
write(" = [];");
45974595
emitEnd(restParam);
45984596
emitTrailingComments(restParam);
@@ -4614,7 +4612,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46144612
writeLine();
46154613
emitStart(restParam);
46164614
emitNodeWithCommentsAndWithoutSourcemap(restParam.name);
4617-
emitLeadingCommentsAtEnd(restParam.name);
46184615
write("[" + tempName + " - " + restIndex + "] = arguments[" + tempName + "];");
46194616
emitEnd(restParam);
46204617
decreaseIndent();
@@ -4636,7 +4633,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46364633
function emitDeclarationName(node: Declaration) {
46374634
if (node.name) {
46384635
emitNodeWithCommentsAndWithoutSourcemap(node.name);
4639-
emitLeadingCommentsAtEnd(node.name);
46404636
}
46414637
else {
46424638
write(getGeneratedNameForNode(node));
@@ -4664,7 +4660,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46644660
const { kind, parent } = node;
46654661
if (kind !== SyntaxKind.MethodDeclaration &&
46664662
kind !== SyntaxKind.MethodSignature &&
4667-
kind !== SyntaxKind.ArrowFunction &&
46684663
parent &&
46694664
parent.kind !== SyntaxKind.PropertyAssignment &&
46704665
parent.kind !== SyntaxKind.CallExpression &&
@@ -4739,11 +4734,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
47394734
const parameters = node.parameters;
47404735
const skipCount = node.parameters.length && (<Identifier>node.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword ? 1 : 0;
47414736
const omitCount = languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node) ? 1 : 0;
4742-
emitTrailingCommentsOfPosition(node.parameters.pos);
4743-
emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false, /*leadingComma*/ false, /*noTrailingNewLine*/ false, /*commentsBeforePunctuation*/ true);
4744-
if ((parameters.length - omitCount - skipCount) <= 0) {
4745-
emitLeadingCommentsAtEnd(node.parameters);
4746-
}
4737+
emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false);
47474738
}
47484739
write(")");
47494740
decreaseIndent();
@@ -5800,7 +5791,7 @@ const _super = (function (geti, seti) {
58005791
writeLine();
58015792

58025793
const decoratorCount = decorators ? decorators.length : 0;
5803-
let argumentsWritten = emitList(decorators, 0, decoratorCount, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ false, /*noTrailingNewLine*/ true, /*commentsBeforePunctuation*/ false,
5794+
let argumentsWritten = emitList(decorators, 0, decoratorCount, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ false, /*noTrailingNewLine*/ true,
58045795
decorator => emit(decorator.expression));
58055796
if (firstParameterDecorator) {
58065797
argumentsWritten += emitDecoratorsOfParameters(constructor, /*leadingComma*/ argumentsWritten > 0);
@@ -5900,7 +5891,7 @@ const _super = (function (geti, seti) {
59005891
writeLine();
59015892

59025893
const decoratorCount = decorators ? decorators.length : 0;
5903-
let argumentsWritten = emitList(decorators, 0, decoratorCount, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ false, /*noTrailingNewLine*/ true, /*commentsBeforePunctuation*/ false,
5894+
let argumentsWritten = emitList(decorators, 0, decoratorCount, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ false, /*noTrailingNewLine*/ true,
59045895
decorator => emit(decorator.expression));
59055896

59065897
if (firstParameterDecorator) {
@@ -5942,7 +5933,7 @@ const _super = (function (geti, seti) {
59425933
for (const parameter of node.parameters) {
59435934
if (nodeIsDecorated(parameter)) {
59445935
const decorators = parameter.decorators;
5945-
argumentsWritten += emitList(decorators, 0, decorators.length, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ leadingComma, /*noTrailingNewLine*/ true, /*commentsBeforePunctuation*/ false, decorator => {
5936+
argumentsWritten += emitList(decorators, 0, decorators.length, /*multiLine*/ true, /*trailingComma*/ false, /*leadingComma*/ leadingComma, /*noTrailingNewLine*/ true, decorator => {
59465937
write(`__param(${parameterIndex}, `);
59475938
emit(decorator.expression);
59485939
write(")");
@@ -6356,7 +6347,6 @@ const _super = (function (geti, seti) {
63566347
emitExpressionForPropertyName(node.name);
63576348
emitEnd(node);
63586349
write(";");
6359-
emitLeadingCommentsAtEnd(node.name);
63606350
}
63616351

63626352
function writeEnumMemberDeclarationValue(member: EnumMember) {
@@ -8319,11 +8309,8 @@ const _super = (function (geti, seti) {
83198309

83208310
/**
83218311
* Emit trailing comments at the position. The term trailing comment is used here to describe following comment:
8322-
* x, /*comment1* / // comment2
8323-
* ^ => pos; the function will emit "comment1" and "comment2" in the emitJS
8324-
* However,
83258312
* x /*comment1* /, y
8326-
* 'comment1' is actually a leading comment of ',' and needs to be emitted using emitLeadingCommentsAtEnd
8313+
* ^ => pos; the function will emit "comment1"
83278314
*/
83288315
function emitTrailingCommentsOfPosition(pos: number) {
83298316
if (compilerOptions.removeComments) {
@@ -8336,18 +8323,7 @@ const _super = (function (geti, seti) {
83368323
emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ true, newLine, writeComment);
83378324
}
83388325

8339-
/**
8340-
* Emit leading comments for the token after the current token.
8341-
* This is only needed for punctuation tokens. For example, in
8342-
* x /*comment1* /, y
8343-
* 'comment1' is a leading comment of ',' but needs to be emitted
8344-
* from x because there is no token for ','.
8345-
*/
8346-
function emitLeadingCommentsAtEnd(node: Node | NodeArray<any>) {
8347-
emitLeadingCommentsOfPositionWorker(node.end, /*trailingSeparator*/ false);
8348-
}
8349-
8350-
function emitLeadingCommentsOfPositionWorker(pos: number, trailingSeparator = true) {
8326+
function emitLeadingCommentsOfPositionWorker(pos: number) {
83518327
if (compilerOptions.removeComments) {
83528328
return;
83538329
}
@@ -8365,7 +8341,7 @@ const _super = (function (geti, seti) {
83658341
emitNewLineBeforeLeadingComments(currentLineMap, writer, { pos: pos, end: pos }, leadingComments);
83668342

83678343
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
8368-
emitComments(currentText, currentLineMap, writer, leadingComments, trailingSeparator, newLine, writeComment);
8344+
emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment);
83698345
}
83708346

83718347
function emitDetachedCommentsAndUpdateCommentsInfo(node: TextRange) {

src/compiler/scanner.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,10 @@ namespace ts {
599599
* If false, whitespace is skipped until the first line break and comments between that location
600600
* and the next token are returned.
601601
* If true, comments occurring between the given position and the next line break are returned.
602-
* If there is no line break, then no comments are returned.
603602
*/
604603
function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] {
605604
let result: CommentRange[];
606-
let needToSkipTrailingComment = pos > 0;
605+
let collecting = trailing || pos === 0;
607606
while (pos < text.length) {
608607
const ch = text.charCodeAt(pos);
609608
switch (ch) {
@@ -616,11 +615,7 @@ namespace ts {
616615
if (trailing) {
617616
return result;
618617
}
619-
else if (needToSkipTrailingComment) {
620-
// skip the first line if not trailing (it'll be part of the trailing comment).
621-
needToSkipTrailingComment = false;
622-
result = undefined;
623-
}
618+
collecting = true;
624619
if (result && result.length) {
625620
lastOrUndefined(result).hasTrailingNewLine = true;
626621
}
@@ -656,13 +651,11 @@ namespace ts {
656651
pos++;
657652
}
658653
}
659-
660-
// if we are at the end of the file, don't add trailing comments.
661-
// end-of-file comments are added as leading comment of the end-of-file token.
662-
if (pos < text.length || !trailing) {
654+
if (collecting) {
663655
if (!result) {
664656
result = [];
665657
}
658+
666659
result.push({ pos: startPos, end: pos, hasTrailingNewLine, kind });
667660
}
668661
continue;
@@ -678,10 +671,10 @@ namespace ts {
678671
}
679672
break;
680673
}
681-
return !trailing ? result : undefined;
674+
return result;
682675
}
683676

684-
return !trailing ? result : undefined;
677+
return result;
685678
}
686679

687680
export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] {

src/compiler/utilities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,13 @@ namespace ts {
596596
}
597597

598598
export function getJsDocCommentsFromText(node: Node, text: string) {
599-
return filter(getLeadingCommentRanges(text, node.pos), isJsDocComment);
599+
const commentRanges = (node.kind === SyntaxKind.Parameter ||
600+
node.kind === SyntaxKind.TypeParameter ||
601+
node.kind === SyntaxKind.FunctionExpression ||
602+
node.kind === SyntaxKind.ArrowFunction) ?
603+
concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) :
604+
getLeadingCommentRangesOfNodeFromText(node, text);
605+
return filter(commentRanges, isJsDocComment);
600606

601607
function isJsDocComment(comment: CommentRange) {
602608
// True if the comment starts with '/**' but not if it is '/**/'

0 commit comments

Comments
 (0)