Merge pull request #19619 from uniqueiniquity/indentJsxText

Properly indent JSXText on format document
This commit is contained in:
Benjamin Lichtman 2017-11-01 09:03:52 -07:00 committed by GitHub
commit dcc1f1497f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View file

@ -714,6 +714,11 @@ namespace ts.formatting {
processNode(child, childContextNode, childStartLine, undecoratedChildStartLine, childIndentation.indentation, childIndentation.delta);
if (child.kind === SyntaxKind.JsxText) {
const range: TextRange = { pos: child.getStart(), end: child.getEnd() };
indentMultilineCommentOrJsxText(range, childIndentation.indentation, /*firstLineIsIndented*/ true, /*indentFinalLine*/ false);
}
childContextNode = node;
if (isFirstListItem && parent.kind === SyntaxKind.ArrayLiteralExpression && inheritedIndentation === Constants.Unknown) {
@ -833,7 +838,7 @@ namespace ts.formatting {
switch (triviaItem.kind) {
case SyntaxKind.MultiLineCommentTrivia:
if (triviaInRange) {
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
indentMultilineCommentOrJsxText(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
}
indentNextTokenOrTrivia = false;
break;
@ -985,7 +990,7 @@ namespace ts.formatting {
return indentationString !== sourceFile.text.substr(startLinePosition, indentationString.length);
}
function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) {
function indentMultilineCommentOrJsxText(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean, indentFinalLine = true) {
// split comment in lines
let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line;
const endLine = sourceFile.getLineAndCharacterOfPosition(commentRange.end).line;
@ -1006,7 +1011,9 @@ namespace ts.formatting {
startPos = getStartPositionOfLine(line + 1, sourceFile);
}
parts.push({ pos: startPos, end: commentRange.end });
if (indentFinalLine) {
parts.push({ pos: startPos, end: commentRange.end });
}
}
const startLinePos = getStartPositionOfLine(startLine, sourceFile);

View file

@ -0,0 +1,35 @@
/// <reference path='fourslash.ts' />
//@Filename: file.tsx
////function foo() {
//// return (
//// <div>
////hello
////goodbye
//// </div>
//// )
////}
verify.currentFileContentIs(
`function foo() {
return (
<div>
hello
goodbye
</div>
)
}`
);
format.document();
verify.currentFileContentIs(
`function foo() {
return (
<div>
hello
goodbye
</div>
)
}`
);