Skip to content

Commit e3609e2

Browse files
Rename Comment->CommentRange.
1 parent 276a735 commit e3609e2

File tree

5 files changed

+60
-33
lines changed

5 files changed

+60
-33
lines changed

src/compiler/emitter.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ module ts {
182182
});
183183
}
184184

185-
function emitComments(comments: Comment[], trailingSeparator: boolean, writer: EmitTextWriter, writeComment: (comment: Comment, writer: EmitTextWriter) => void) {
185+
function emitComments(comments: CommentRange[], trailingSeparator: boolean, writer: EmitTextWriter, writeComment: (comment: CommentRange, writer: EmitTextWriter) => void) {
186186
var emitLeadingSpace = !trailingSeparator;
187187
forEach(comments, comment => {
188188
if (emitLeadingSpace) {
@@ -203,15 +203,15 @@ module ts {
203203
});
204204
}
205205

206-
function emitNewLineBeforeLeadingComments(node: TextRange, leadingComments: Comment[], writer: EmitTextWriter) {
206+
function emitNewLineBeforeLeadingComments(node: TextRange, leadingComments: CommentRange[], writer: EmitTextWriter) {
207207
// If the leading comments start on different line than the start of node, write new line
208208
if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos &&
209209
getLineOfLocalPosition(node.pos) !== getLineOfLocalPosition(leadingComments[0].pos)) {
210210
writer.writeLine();
211211
}
212212
}
213213

214-
function writeCommentRange(comment: Comment, writer: EmitTextWriter) {
214+
function writeCommentRange(comment: CommentRange, writer: EmitTextWriter) {
215215
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
216216
var firstCommentLineAndCharacter = currentSourceFile.getLineAndCharacterFromPosition(comment.pos);
217217
var firstCommentLineIndent: number;
@@ -585,7 +585,7 @@ module ts {
585585
sourceMapNameIndices.pop();
586586
};
587587

588-
function writeCommentRangeWithMap(comment: Comment, writer: EmitTextWriter) {
588+
function writeCommentRangeWithMap(comment: CommentRange, writer: EmitTextWriter) {
589589
recordSourceMapSpan(comment.pos);
590590
writeCommentRange(comment, writer);
591591
recordSourceMapSpan(comment.end);
@@ -2108,7 +2108,7 @@ module ts {
21082108

21092109
function getLeadingCommentsWithoutDetachedComments() {
21102110
// get the leading comments from detachedPos
2111-
var leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
2111+
var leadingComments = getLeadingCommentRanges(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
21122112
if (detachedCommentsInfo.length - 1) {
21132113
detachedCommentsInfo.pop();
21142114
}
@@ -2122,14 +2122,14 @@ module ts {
21222122
function getLeadingCommentsToEmit(node: Node) {
21232123
// Emit the leading comments only if the parent's pos doesn't match because parent should take care of emitting these comments
21242124
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
2125-
var leadingComments: Comment[];
2125+
var leadingComments: CommentRange[];
21262126
if (hasDetachedComments(node.pos)) {
21272127
// get comments without detached comments
21282128
leadingComments = getLeadingCommentsWithoutDetachedComments();
21292129
}
21302130
else {
21312131
// get the leading comments from the node
2132-
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
2132+
leadingComments = getLeadingCommentRangesOfNode(node, currentSourceFile);
21332133
}
21342134
return leadingComments;
21352135
}
@@ -2145,32 +2145,32 @@ module ts {
21452145
function emitTrailingDeclarationComments(node: Node) {
21462146
// Emit the trailing comments only if the parent's end doesn't match
21472147
if (node.parent.kind === SyntaxKind.SourceFile || node.end !== node.parent.end) {
2148-
var trailingComments = getTrailingComments(currentSourceFile.text, node.end);
2148+
var trailingComments = getTrailingCommentRanges(currentSourceFile.text, node.end);
21492149
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
21502150
emitComments(trailingComments, /*trailingSeparator*/ false, writer, writeComment);
21512151
}
21522152
}
21532153

21542154
function emitLeadingCommentsOfLocalPosition(pos: number) {
2155-
var leadingComments: Comment[];
2155+
var leadingComments: CommentRange[];
21562156
if (hasDetachedComments(pos)) {
21572157
// get comments without detached comments
21582158
leadingComments = getLeadingCommentsWithoutDetachedComments();
21592159
}
21602160
else {
21612161
// get the leading comments from the node
2162-
leadingComments = getLeadingComments(currentSourceFile.text, pos);
2162+
leadingComments = getLeadingCommentRanges(currentSourceFile.text, pos);
21632163
}
21642164
emitNewLineBeforeLeadingComments({ pos: pos, end: pos }, leadingComments, writer);
21652165
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
21662166
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
21672167
}
21682168

21692169
function emitDetachedCommentsAtPosition(node: TextRange) {
2170-
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
2170+
var leadingComments = getLeadingCommentRanges(currentSourceFile.text, node.pos);
21712171
if (leadingComments) {
2172-
var detachedComments: Comment[] = [];
2173-
var lastComment: Comment;
2172+
var detachedComments: CommentRange[] = [];
2173+
var lastComment: CommentRange;
21742174

21752175
forEach(leadingComments, comment => {
21762176
if (lastComment) {
@@ -2214,7 +2214,7 @@ module ts {
22142214
function emitPinnedOrTripleSlashCommentsOfNode(node: Node) {
22152215
var pinnedComments = ts.filter(getLeadingCommentsToEmit(node), isPinnedOrTripleSlashComment);
22162216

2217-
function isPinnedOrTripleSlashComment(comment: Comment) {
2217+
function isPinnedOrTripleSlashComment(comment: CommentRange) {
22182218
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
22192219
return currentSourceFile.text.charCodeAt(comment.pos + 2) === CharacterCodes.exclamation;
22202220
}

src/compiler/parser.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,27 @@ module ts {
138138
return (<Identifier>(<ExpressionStatement>node).expression).text === "use strict";
139139
}
140140

141-
export function getLeadingCommentsOfNode(node: Node, sourceFileOfNode: SourceFile) {
141+
export function getLeadingCommentRangesOfNode(node: Node, sourceFileOfNode?: SourceFile) {
142+
sourceFileOfNode = sourceFileOfNode || getSourceFileOfNode(node);
143+
142144
// If parameter/type parameter, the prev token trailing comments are part of this node too
143145
if (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter) {
144146
// e.g. (/** blah */ a, /** blah */ b);
145-
return concatenate(getTrailingComments(sourceFileOfNode.text, node.pos),
147+
return concatenate(getTrailingCommentRanges(sourceFileOfNode.text, node.pos),
146148
// e.g.: (
147149
// /** blah */ a,
148150
// /** blah */ b);
149-
getLeadingComments(sourceFileOfNode.text, node.pos));
151+
getLeadingCommentRanges(sourceFileOfNode.text, node.pos));
150152
}
151153
else {
152-
return getLeadingComments(sourceFileOfNode.text, node.pos);
154+
return getLeadingCommentRanges(sourceFileOfNode.text, node.pos);
153155
}
154156
}
155157

156158
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
157-
return filter(getLeadingCommentsOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
159+
return filter(getLeadingCommentRangesOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
158160

159-
function isJsDocComment(comment: Comment) {
161+
function isJsDocComment(comment: CommentRange) {
160162
// True if the comment starts with '/**' but not if it is '/**/'
161163
return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
162164
sourceFileOfNode.text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&

src/compiler/scanner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ module ts {
371371
// between the given position and the next line break are returned. The return value is an array containing a TextRange for each
372372
// comment. Single-line comment ranges include the beginning '//' characters but not the ending line break. Multi-line comment
373373
// ranges include the beginning '/* and ending '*/' characters. The return value is undefined if no comments were found.
374-
function getCommentRanges(text: string, pos: number, trailing: boolean): Comment[] {
375-
var result: Comment[];
374+
function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] {
375+
var result: CommentRange[];
376376
var collecting = trailing || pos === 0;
377377
while (true) {
378378
var ch = text.charCodeAt(pos);
@@ -440,11 +440,11 @@ module ts {
440440
}
441441
}
442442

443-
export function getLeadingComments(text: string, pos: number): Comment[] {
443+
export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] {
444444
return getCommentRanges(text, pos, /*trailing*/ false);
445445
}
446446

447-
export function getTrailingComments(text: string, pos: number): Comment[] {
447+
export function getTrailingCommentRanges(text: string, pos: number): CommentRange[] {
448448
return getCommentRanges(text, pos, /*trailing*/ true);
449449
}
450450

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ module ts {
529529
filename: string;
530530
}
531531

532-
export interface Comment extends TextRange {
532+
export interface CommentRange extends TextRange {
533533
hasTrailingNewLine?: boolean;
534534
}
535535

src/services/services.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ module ts {
9797
private _children: Node[];
9898

9999
public getSourceFile(): SourceFile {
100-
var node: Node = this;
101-
while (node.kind !== SyntaxKind.SourceFile) node = node.parent;
102-
return <SourceFile>node;
100+
return getSourceFileOfNode(this);
103101
}
104102

105103
public getStart(sourceFile?: SourceFile): number {
@@ -225,19 +223,44 @@ module ts {
225223
flags: SymbolFlags;
226224
name: string;
227225
declarations: Declaration[];
226+
documentationComment: string;
227+
228228
constructor(flags: SymbolFlags, name: string) {
229229
this.flags = flags;
230230
this.name = name;
231231
}
232+
232233
getFlags(): SymbolFlags {
233234
return this.flags;
234235
}
236+
235237
getName(): string {
236238
return this.name;
237239
}
240+
238241
getDeclarations(): Declaration[] {
239242
return this.declarations;
240243
}
244+
245+
//getDocumentationComment(): string {
246+
// if (this.documentationComment === undefined) {
247+
// var result = "";
248+
249+
// var declarations = this.getDeclarations();
250+
// if (declarations) {
251+
// for (var i = 0, n = declarations.length; i < n; i++) {
252+
// var declaration = declarations[0];
253+
254+
// var commentRanges = getLeadingCommentRangesOfNode(declaration);
255+
256+
// }
257+
// }
258+
259+
// this.documentationComment = result;
260+
// }
261+
262+
// return this.documentationComment;
263+
//}
241264
}
242265

243266
class TypeObject implements Type {
@@ -657,7 +680,8 @@ module ts {
657680
constructor(public kind: string,
658681
public kindModifiers: string,
659682
public textSpan: TypeScript.TextSpan,
660-
public displayParts: SymbolDisplayPart[]) {
683+
public displayParts: SymbolDisplayPart[],
684+
public documentation: SymbolDisplayPart[]) {
661685
}
662686

663687
public toJSON() {
@@ -2344,7 +2368,8 @@ module ts {
23442368
getSymbolKind(symbol),
23452369
getSymbolModifiers(symbol),
23462370
new TypeScript.TextSpan(node.getStart(), node.getWidth()),
2347-
totalParts);
2371+
totalParts,
2372+
[]/*convertDocumentation(symbol)*/);
23482373
}
23492374

23502375
function getTypeAtPosition(fileName: string, position: number): TypeInfo {
@@ -4015,8 +4040,8 @@ module ts {
40154040
}
40164041

40174042
// Looks to be within the trivia. See if we can find the comment containing it.
4018-
if (!getContainingComment(getTrailingComments(fileContents, token.getFullStart()), matchPosition) &&
4019-
!getContainingComment(getLeadingComments(fileContents, token.getFullStart()), matchPosition)) {
4043+
if (!getContainingComment(getTrailingCommentRanges(fileContents, token.getFullStart()), matchPosition) &&
4044+
!getContainingComment(getLeadingCommentRanges(fileContents, token.getFullStart()), matchPosition)) {
40204045
continue;
40214046
}
40224047

@@ -4103,7 +4128,7 @@ module ts {
41034128
return new RegExp(regExpString, "gim");
41044129
}
41054130

4106-
function getContainingComment(comments: Comment[], position: number): Comment {
4131+
function getContainingComment(comments: CommentRange[], position: number): CommentRange {
41074132
if (comments) {
41084133
for (var i = 0, n = comments.length; i < n; i++) {
41094134
var comment = comments[i];

0 commit comments

Comments
 (0)