Skip to content

Commit 88c6ced

Browse files
author
Andy
authored
indentMultilineCommentOrJsxText: Fix bug when 'parts' is empty (microsoft#25645)
1 parent 1fb050b commit 88c6ced

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/services/formatting/formatting.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -975,28 +975,28 @@ namespace ts.formatting {
975975
// split comment in lines
976976
let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line;
977977
const endLine = sourceFile.getLineAndCharacterOfPosition(commentRange.end).line;
978-
let parts: TextRange[];
979978
if (startLine === endLine) {
980979
if (!firstLineIsIndented) {
981980
// treat as single line comment
982981
insertIndentation(commentRange.pos, indentation, /*lineAdded*/ false);
983982
}
984983
return;
985984
}
986-
else {
987-
parts = [];
988-
let startPos = commentRange.pos;
989-
for (let line = startLine; line < endLine; line++) {
990-
const endOfLine = getEndLinePosition(line, sourceFile);
991-
parts.push({ pos: startPos, end: endOfLine });
992-
startPos = getStartPositionOfLine(line + 1, sourceFile);
993-
}
994985

995-
if (indentFinalLine) {
996-
parts.push({ pos: startPos, end: commentRange.end });
997-
}
986+
const parts: TextRange[] = [];
987+
let startPos = commentRange.pos;
988+
for (let line = startLine; line < endLine; line++) {
989+
const endOfLine = getEndLinePosition(line, sourceFile);
990+
parts.push({ pos: startPos, end: endOfLine });
991+
startPos = getStartPositionOfLine(line + 1, sourceFile);
998992
}
999993

994+
if (indentFinalLine) {
995+
parts.push({ pos: startPos, end: commentRange.end });
996+
}
997+
998+
if (parts.length === 0) return;
999+
10001000
const startLinePos = getStartPositionOfLine(startLine, sourceFile);
10011001

10021002
const nonWhitespaceColumnInFirstPart =
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.tsx
4+
////const x = () /*a*/=>/*b*/
5+
//// <div>:</div>
6+
7+
// This code with this exact indent level used to crash in `indentMultilineCommentOrJsxText`.
8+
9+
goTo.select("a", "b");
10+
11+
edit.applyRefactor({
12+
refactorName: "Add or remove braces in an arrow function",
13+
actionName: "Add braces to arrow function",
14+
actionDescription: "Add braces to arrow function",
15+
newContent:
16+
`const x = () =>
17+
{
18+
return <div>:</div>;
19+
}`,
20+
});

0 commit comments

Comments
 (0)