Skip to content

Commit 6e4d0ef

Browse files
committed
Merge pull request #452 from Microsoft/emitComments
Emit comments in the .js and .d.ts files
2 parents 8ad66bb + 8050959 commit 6e4d0ef

File tree

2,004 files changed

+25985
-16682
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,004 files changed

+25985
-16682
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ module ts {
6868
}
6969

7070
export function concatenate<T>(array1: T[], array2: T[]): T[] {
71-
if (!array2.length) return array1;
72-
if (!array1.length) return array2;
71+
if (!array2 || !array2.length) return array1;
72+
if (!array1 || !array1.length) return array2;
7373
return array1.concat(array2);
7474
}
7575

src/compiler/emitter.ts

Lines changed: 415 additions & 67 deletions
Large diffs are not rendered by default.

src/compiler/parser.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ module ts {
139139
return (<Identifier>(<ExpressionStatement>node).expression).text === "use strict";
140140
}
141141

142+
export function getLeadingCommentsOfNode(node: Node, sourceFileOfNode: SourceFile) {
143+
// If parameter/type parameter, the prev token trailing comments are part of this node too
144+
if (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter) {
145+
// eg (/** blah */ a, /** blah */ b);
146+
return concatenate(getTrailingComments(sourceFileOfNode.text, node.pos),
147+
// eg: (
148+
// /** blah */ a,
149+
// /** blah */ b);
150+
getLeadingComments(sourceFileOfNode.text, node.pos));
151+
}
152+
else {
153+
return getLeadingComments(sourceFileOfNode.text, node.pos);
154+
}
155+
}
156+
157+
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
158+
return filter(getLeadingCommentsOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
159+
160+
function isJsDocComment(comment: Comment) {
161+
// js doc is if comment is starting with /** but not if it is /**/
162+
return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
163+
sourceFileOfNode.text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&
164+
sourceFileOfNode.text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash;
165+
}
166+
}
167+
142168
// Invokes a callback for each child of the given node. The 'cbNode' callback is invoked for all child nodes
143169
// stored in properties. If a 'cbNodes' callback is specified, it is invoked for embedded arrays; otherwise,
144170
// embedded arrays are flattened and the 'cbNode' callback is invoked for each element. If a callback returns
@@ -546,6 +572,13 @@ module ts {
546572
return getLineAndCharacterOfPosition(lineStarts, position);
547573
}
548574

575+
function getPositionFromSourceLineAndCharacter(line: number, character: number): number {
576+
if (!lineStarts) {
577+
lineStarts = getLineStarts(sourceText);
578+
}
579+
return getPositionFromLineAndCharacter(lineStarts, line, character);
580+
}
581+
549582
function error(message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void {
550583
var start = scanner.getTokenPos();
551584
var length = scanner.getTextPos() - start;
@@ -3525,6 +3558,7 @@ module ts {
35253558
file.filename = normalizePath(filename);
35263559
file.text = sourceText;
35273560
file.getLineAndCharacterFromPosition = getLineAndCharacterlFromSourcePosition;
3561+
file.getPositionFromLineAndCharacter = getPositionFromSourceLineAndCharacter;
35283562
file.syntacticErrors = [];
35293563
file.semanticErrors = [];
35303564
var referenceComments = processReferenceComments();

src/compiler/scanner.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ module ts {
264264
return result;
265265
}
266266

267+
export function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
268+
Debug.assert(line > 0);
269+
return lineStarts[line - 1] + character - 1;
270+
}
271+
267272
export function getLineAndCharacterOfPosition(lineStarts: number[], position: number) {
268273
var lineNumber = binarySearch(lineStarts, position);
269274
if (lineNumber < 0) {
@@ -286,13 +291,13 @@ module ts {
286291

287292
var hasOwnProperty = Object.prototype.hasOwnProperty;
288293

289-
function isWhiteSpace(ch: number): boolean {
294+
export function isWhiteSpace(ch: number): boolean {
290295
return ch === CharacterCodes.space || ch === CharacterCodes.tab || ch === CharacterCodes.verticalTab || ch === CharacterCodes.formFeed ||
291296
ch === CharacterCodes.nonBreakingSpace || ch === CharacterCodes.ogham || ch >= CharacterCodes.enQuad && ch <= CharacterCodes.zeroWidthSpace ||
292297
ch === CharacterCodes.narrowNoBreakSpace || ch === CharacterCodes.mathematicalSpace || ch === CharacterCodes.ideographicSpace || ch === CharacterCodes.byteOrderMark;
293298
}
294299

295-
function isLineBreak(ch: number): boolean {
300+
export function isLineBreak(ch: number): boolean {
296301
return ch === CharacterCodes.lineFeed || ch === CharacterCodes.carriageReturn || ch === CharacterCodes.lineSeparator || ch === CharacterCodes.paragraphSeparator;
297302
}
298303

@@ -359,9 +364,9 @@ module ts {
359364
// between the given position and the next line break are returned. The return value is an array containing a TextRange for each
360365
// comment. Single-line comment ranges include the the beginning '//' characters but not the ending line break. Multi-line comment
361366
// ranges include the beginning '/* and ending '*/' characters. The return value is undefined if no comments were found.
362-
function getCommentRanges(text: string, pos: number, trailing: boolean): TextRange[] {
363-
var result: TextRange[];
364-
var collecting = trailing;
367+
function getCommentRanges(text: string, pos: number, trailing: boolean): Comment[] {
368+
var result: Comment[];
369+
var collecting = trailing || pos === 0;
365370
while (true) {
366371
var ch = text.charCodeAt(pos);
367372
switch (ch) {
@@ -373,6 +378,9 @@ module ts {
373378
return result;
374379
}
375380
collecting = true;
381+
if (result && result.length) {
382+
result[result.length - 1].hasTrailingNewLine = true;
383+
}
376384
continue;
377385
case CharacterCodes.tab:
378386
case CharacterCodes.verticalTab:
@@ -382,12 +390,14 @@ module ts {
382390
continue;
383391
case CharacterCodes.slash:
384392
var nextChar = text.charCodeAt(pos + 1);
393+
var hasTrailingNewLine = false;
385394
if (nextChar === CharacterCodes.slash || nextChar === CharacterCodes.asterisk) {
386395
var startPos = pos;
387396
pos += 2;
388397
if (nextChar === CharacterCodes.slash) {
389398
while (pos < text.length) {
390399
if (isLineBreak(text.charCodeAt(pos))) {
400+
hasTrailingNewLine = true;
391401
break;
392402
}
393403
pos++;
@@ -404,13 +414,16 @@ module ts {
404414
}
405415
if (collecting) {
406416
if (!result) result = [];
407-
result.push({ pos: startPos, end: pos });
417+
result.push({ pos: startPos, end: pos, hasTrailingNewLine: hasTrailingNewLine });
408418
}
409419
continue;
410420
}
411421
break;
412422
default:
413423
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
424+
if (result && result.length && isLineBreak(ch)) {
425+
result[result.length - 1].hasTrailingNewLine = true;
426+
}
414427
pos++;
415428
continue;
416429
}
@@ -420,11 +433,11 @@ module ts {
420433
}
421434
}
422435

423-
export function getLeadingComments(text: string, pos: number): TextRange[] {
436+
export function getLeadingComments(text: string, pos: number): Comment[] {
424437
return getCommentRanges(text, pos, /*trailing*/ false);
425438
}
426439

427-
export function getTrailingComments(text: string, pos: number): TextRange[] {
440+
export function getTrailingComments(text: string, pos: number): Comment[] {
428441
return getCommentRanges(text, pos, /*trailing*/ true);
429442
}
430443

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,15 @@ module ts {
516516
filename: string;
517517
}
518518

519+
export interface Comment extends TextRange {
520+
hasTrailingNewLine?: boolean;
521+
}
522+
519523
export interface SourceFile extends Block {
520524
filename: string;
521525
text: string;
522526
getLineAndCharacterFromPosition(position: number): { line: number; character: number };
527+
getPositionFromLineAndCharacter(line: number, character: number): number;
523528
amdDependencies: string[];
524529
referencedFiles: FileReference[];
525530
syntacticErrors: Diagnostic[];

src/harness/harness.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,14 +697,17 @@ module Harness {
697697
sys.newLine = setting.value;
698698
break;
699699

700+
case 'comments':
701+
options.removeComments = setting.value === 'false';
702+
break;
703+
700704
case 'mapsourcefiles':
701705
case 'maproot':
702706
case 'generatedeclarationfiles':
703707
case 'usecasesensitivefileresolution':
704708
case 'gatherDiagnostics':
705709
case 'codepage':
706710
case 'createFileLog':
707-
case 'comments':
708711
case 'filename':
709712
case 'propagateenumconstants':
710713
case 'removecomments':

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ module ts {
310310
public filename: string;
311311
public text: string;
312312
public getLineAndCharacterFromPosition(position: number): { line: number; character: number } { return null; }
313+
public getPositionFromLineAndCharacter(line: number, character: number): number { return -1; }
313314
public amdDependencies: string[];
314315
public referencedFiles: FileReference[];
315316
public syntacticErrors: Diagnostic[];

tests/baselines/reference/AmbientModuleAndAmbientWithSameNameAndCommonRoot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ var p = new A.Point(0, 0); // unexpected error here, bug 840000
2828
//// [test.js]
2929
var p;
3030
var p = A.Point.Origin;
31-
var p = new A.Point(0, 0);
31+
var p = new A.Point(0, 0); // unexpected error here, bug 840000

tests/baselines/reference/AmbientModuleAndNonAmbientClassWithSameNameAndCommonRoot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ var A;
3737
//// [test.js]
3838
var p;
3939
var p = A.Point.Origin;
40-
var p = new A.Point(0, 0);
40+
var p = new A.Point(0, 0); // unexpected error here, bug 840000

tests/baselines/reference/ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module clodule4 {
5050

5151

5252
//// [ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.js]
53+
// all expected to be errors
5354
var clodule1 = (function () {
5455
function clodule1() {
5556
}

0 commit comments

Comments
 (0)