Skip to content

Commit 15780a9

Browse files
committed
Fixes some comment emit and other changes
1 parent 794e0ce commit 15780a9

24 files changed

Lines changed: 419 additions & 243 deletions

src/compiler/comments.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace ts {
55
export interface CommentWriter {
66
reset(): void;
77
setSourceFile(sourceFile: SourceFile): void;
8-
getLeadingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
8+
getLeadingComments(range: Node, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean): CommentRange[];
99
getLeadingComments(range: TextRange): CommentRange[];
1010
getLeadingCommentsOfPosition(pos: number): CommentRange[];
11-
getTrailingComments(range: Node, getAdditionalRange?: (range: Node) => Node): CommentRange[];
11+
getTrailingComments(range: Node, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean): CommentRange[];
1212
getTrailingComments(range: TextRange): CommentRange[];
1313
getTrailingCommentsOfPosition(pos: number): CommentRange[];
14-
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void;
15-
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void;
14+
emitLeadingComments(range: TextRange, comments: CommentRange[]): void;
15+
emitTrailingComments(range: TextRange, comments: CommentRange[]): void;
1616
emitDetachedComments(range: TextRange): void;
1717
}
1818

@@ -40,12 +40,12 @@ namespace ts {
4040
return {
4141
reset,
4242
setSourceFile,
43-
getLeadingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
43+
getLeadingComments(range: TextRange, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean): CommentRange[] { return undefined; },
4444
getLeadingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
45-
getTrailingComments(range: TextRange, getAdditionalRange?: (range: TextRange) => TextRange): CommentRange[] { return undefined; },
45+
getTrailingComments(range: TextRange, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean): CommentRange[] { return undefined; },
4646
getTrailingCommentsOfPosition(pos: number): CommentRange[] { return undefined; },
47-
emitLeadingComments(range: TextRange, comments?: CommentRange[]): void { },
48-
emitTrailingComments(range: TextRange, comments?: CommentRange[]): void { },
47+
emitLeadingComments(range: TextRange, comments: CommentRange[]): void { },
48+
emitTrailingComments(range: TextRange, comments: CommentRange[]): void { },
4949
emitDetachedComments,
5050
};
5151

@@ -68,38 +68,48 @@ namespace ts {
6868
emitDetachedComments,
6969
};
7070

71-
function getLeadingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
72-
let comments = getLeadingCommentsOfPosition(range.pos);
73-
if (getAdditionalRange) {
74-
let additionalRange = getAdditionalRange(<Node>range);
75-
while (additionalRange) {
76-
comments = concatenate(
77-
getLeadingCommentsOfPosition(additionalRange.pos),
78-
comments
79-
);
80-
81-
additionalRange = getAdditionalRange(additionalRange);
71+
function getLeadingComments(range: TextRange | Node, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean) {
72+
if (shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback(<Node>range)) {
73+
// If the node will not be emitted in JS, remove all the comments(normal, pinned and ///) associated with the node,
74+
// unless it is a triple slash comment at the top of the file.
75+
// For Example:
76+
// /// <reference-path ...>
77+
// declare var x;
78+
// /// <reference-path ...>
79+
// interface F {}
80+
// The first /// will NOT be removed while the second one will be removed even though both nodes will not be emitted
81+
if (range.pos === 0) {
82+
return filter(getLeadingCommentsOfPosition(0), isTripleSlashComment);
8283
}
84+
85+
return undefined;
8386
}
8487

85-
return comments;
88+
return getLeadingCommentsOfPosition(range.pos);
8689
}
8790

88-
function getTrailingComments(range: TextRange | Node, getAdditionalRange?: (range: Node) => Node) {
89-
let comments = getTrailingCommentsOfPosition(range.end);
90-
if (getAdditionalRange) {
91-
let additionalRange = getAdditionalRange(<Node>range);
92-
while (additionalRange) {
93-
comments = concatenate(
94-
comments,
95-
getTrailingCommentsOfPosition(additionalRange.end)
96-
);
97-
98-
additionalRange = getAdditionalRange(additionalRange);
99-
}
91+
/**
92+
* Determine if the given comment is a triple-slash
93+
**/
94+
function isTripleSlashComment(comment: CommentRange) {
95+
// Verify this is /// comment, but do the regexp match only when we first can find /// in the comment text
96+
// so that we don't end up computing comment string and doing match for all // comments
97+
if (currentText.charCodeAt(comment.pos + 1) === CharacterCodes.slash &&
98+
comment.pos + 2 < comment.end &&
99+
currentText.charCodeAt(comment.pos + 2) === CharacterCodes.slash) {
100+
const textSubStr = currentText.substring(comment.pos, comment.end);
101+
return fullTripleSlashReferencePathRegEx.test(textSubStr)
102+
|| fullTripleSlashAMDReferencePathRegEx.test(textSubStr);
103+
}
104+
return false;
105+
}
106+
107+
function getTrailingComments(range: TextRange | Node, shouldSkipCommentsForNodeCallback?: (node: Node) => boolean) {
108+
if (shouldSkipCommentsForNodeCallback && shouldSkipCommentsForNodeCallback(<Node>range)) {
109+
return undefined;
100110
}
101111

102-
return comments;
112+
return getTrailingCommentsOfPosition(range.end);
103113
}
104114

105115
function getLeadingCommentsOfPosition(pos: number) {
@@ -124,14 +134,14 @@ namespace ts {
124134
return consumeCommentRanges(comments);
125135
}
126136

127-
function emitLeadingComments(range: TextRange, comments = getLeadingComments(range)) {
137+
function emitLeadingComments(range: TextRange, comments: CommentRange[]) {
128138
emitNewLineBeforeLeadingComments(currentLineMap, writer, range, comments);
129139

130140
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
131141
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeComment);
132142
}
133143

134-
function emitTrailingComments(range: TextRange, comments = getTrailingComments(range)) {
144+
function emitTrailingComments(range: TextRange, comments: CommentRange[]) {
135145
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
136146
emitComments(currentText, currentLineMap, writer, comments, /*leadingSeparator*/ true, /*trailingSeparator*/ false, newLine, writeComment);
137147
}

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10361036
return;
10371037
}
10381038

1039-
const emitOuterParens = isPartOfExpression(node.parent)
1039+
const emitOuterParens = node.parent.kind !== SyntaxKind.ArrowFunction
1040+
&& isPartOfExpression(node.parent)
10401041
&& templateNeedsParens(node, <Expression>node.parent);
10411042

10421043
if (emitOuterParens) {

0 commit comments

Comments
 (0)