Skip to content

Commit 2f7429e

Browse files
committed
Merge pull request #572 from Microsoft/commentFormatting
Fix the tab and indent calculation when formatting comments in the emitted output
2 parents 655039c + 7181184 commit 2f7429e

File tree

6 files changed

+94
-7
lines changed

6 files changed

+94
-7
lines changed

src/compiler/emitter.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,10 @@ module ts {
296296

297297
function calculateIndent(pos: number, end: number) {
298298
var currentLineIndent = 0;
299-
while (pos < end && isWhiteSpace(currentSourceFile.text.charCodeAt(pos))) {
300-
pos++;
299+
for (; pos < end && isWhiteSpace(currentSourceFile.text.charCodeAt(pos)); pos++) {
301300
if (currentSourceFile.text.charCodeAt(pos) === CharacterCodes.tab) {
302-
// Tabs = size of the indent
303-
currentLineIndent += getIndentSize();
301+
// Tabs = TabSize = indent size and go to next tabStop
302+
currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize());
304303
}
305304
else {
306305
// Single space

tests/baselines/reference/commentsFormatting.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ this is 4 spaces left aligned but above line is empty
6767
above 3 lines are empty*/
6868
export class c3 {
6969
}
70+
71+
/** this is first line - aligned to class declaration
72+
* this is 0 space + tab
73+
* this is 1 space + tab
74+
* this is 2 spaces + tab
75+
* this is 3 spaces + tab
76+
* this is 4 spaces + tab
77+
* this is 5 spaces + tab
78+
* this is 6 spaces + tab
79+
* this is 7 spaces + tab
80+
* this is 8 spaces + tab
81+
* this is 9 spaces + tab
82+
* this is 10 spaces + tab
83+
* this is 11 spaces + tab
84+
* this is 12 spaces + tab */
85+
export class c4 {
86+
}
7087
}
7188

7289
//// [commentsFormatting.js]
@@ -148,6 +165,26 @@ this is 4 spaces left aligned but above line is empty
148165
return c3;
149166
})();
150167
m.c3 = c3;
168+
/** this is first line - aligned to class declaration
169+
* this is 0 space + tab
170+
* this is 1 space + tab
171+
* this is 2 spaces + tab
172+
* this is 3 spaces + tab
173+
* this is 4 spaces + tab
174+
* this is 5 spaces + tab
175+
* this is 6 spaces + tab
176+
* this is 7 spaces + tab
177+
* this is 8 spaces + tab
178+
* this is 9 spaces + tab
179+
* this is 10 spaces + tab
180+
* this is 11 spaces + tab
181+
* this is 12 spaces + tab */
182+
var c4 = (function () {
183+
function c4() {
184+
}
185+
return c4;
186+
})();
187+
m.c4 = c4;
151188
})(m || (m = {}));
152189

153190

@@ -217,4 +254,20 @@ this is 4 spaces left aligned but above line is empty
217254
above 3 lines are empty*/
218255
class c3 {
219256
}
257+
/** this is first line - aligned to class declaration
258+
* this is 0 space + tab
259+
* this is 1 space + tab
260+
* this is 2 spaces + tab
261+
* this is 3 spaces + tab
262+
* this is 4 spaces + tab
263+
* this is 5 spaces + tab
264+
* this is 6 spaces + tab
265+
* this is 7 spaces + tab
266+
* this is 8 spaces + tab
267+
* this is 9 spaces + tab
268+
* this is 10 spaces + tab
269+
* this is 11 spaces + tab
270+
* this is 12 spaces + tab */
271+
class c4 {
272+
}
220273
}

tests/baselines/reference/commentsFormatting.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,22 @@ this is 4 spaces left aligned but above line is empty
7272
export class c3 {
7373
>c3 : c3
7474
}
75+
76+
/** this is first line - aligned to class declaration
77+
* this is 0 space + tab
78+
* this is 1 space + tab
79+
* this is 2 spaces + tab
80+
* this is 3 spaces + tab
81+
* this is 4 spaces + tab
82+
* this is 5 spaces + tab
83+
* this is 6 spaces + tab
84+
* this is 7 spaces + tab
85+
* this is 8 spaces + tab
86+
* this is 9 spaces + tab
87+
* this is 10 spaces + tab
88+
* this is 11 spaces + tab
89+
* this is 12 spaces + tab */
90+
export class c4 {
91+
>c4 : c4
92+
}
7593
}

tests/baselines/reference/enumBasics1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ foo(a); // shouldn't error
5555
5656
5757
class C {
58-
public e: E;
58+
public e: E;
5959
60-
public m(): E { return this.e; } // shouldn't error
60+
public m(): E { return this.e; } // shouldn't error
6161
}
6262
6363

tests/baselines/reference/genericArray1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U
2323
}
2424
2525
interface String{
26-
length: number;
26+
length: number;
2727
}
2828
*/
2929
var lengths = ["a", "b", "c"].map(function (x) { return x.length; });

tests/cases/compiler/commentsFormatting.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,21 @@ this is 4 spaces left aligned but above line is empty
6969
above 3 lines are empty*/
7070
export class c3 {
7171
}
72+
73+
/** this is first line - aligned to class declaration
74+
* this is 0 space + tab
75+
* this is 1 space + tab
76+
* this is 2 spaces + tab
77+
* this is 3 spaces + tab
78+
* this is 4 spaces + tab
79+
* this is 5 spaces + tab
80+
* this is 6 spaces + tab
81+
* this is 7 spaces + tab
82+
* this is 8 spaces + tab
83+
* this is 9 spaces + tab
84+
* this is 10 spaces + tab
85+
* this is 11 spaces + tab
86+
* this is 12 spaces + tab */
87+
export class c4 {
88+
}
7289
}

0 commit comments

Comments
 (0)