indentMultilineCommentOrJsxText: Fix bug when 'parts' is empty (#25645)

This commit is contained in:
Andy 2018-07-13 09:56:21 -07:00 committed by GitHub
parent 1fb050bcca
commit 88c6ced3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 12 deletions

View file

@ -975,7 +975,6 @@ namespace ts.formatting {
// split comment in lines
let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line;
const endLine = sourceFile.getLineAndCharacterOfPosition(commentRange.end).line;
let parts: TextRange[];
if (startLine === endLine) {
if (!firstLineIsIndented) {
// treat as single line comment
@ -983,20 +982,21 @@ namespace ts.formatting {
}
return;
}
else {
parts = [];
let startPos = commentRange.pos;
for (let line = startLine; line < endLine; line++) {
const endOfLine = getEndLinePosition(line, sourceFile);
parts.push({ pos: startPos, end: endOfLine });
startPos = getStartPositionOfLine(line + 1, sourceFile);
}
if (indentFinalLine) {
parts.push({ pos: startPos, end: commentRange.end });
}
const parts: TextRange[] = [];
let startPos = commentRange.pos;
for (let line = startLine; line < endLine; line++) {
const endOfLine = getEndLinePosition(line, sourceFile);
parts.push({ pos: startPos, end: endOfLine });
startPos = getStartPositionOfLine(line + 1, sourceFile);
}
if (indentFinalLine) {
parts.push({ pos: startPos, end: commentRange.end });
}
if (parts.length === 0) return;
const startLinePos = getStartPositionOfLine(startLine, sourceFile);
const nonWhitespaceColumnInFirstPart =

View file

@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.tsx
////const x = () /*a*/=>/*b*/
//// <div>:</div>
// This code with this exact indent level used to crash in `indentMultilineCommentOrJsxText`.
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent:
`const x = () =>
{
return <div>:</div>;
}`,
});