Fix formatting scanner on JSX text that looks like trivia (#39718)

* Fix formatting scanner on JSX text that looks like trivia

* Combine if statements

Co-authored-by: Andrew Branch <andrew@wheream.io>
This commit is contained in:
Orta Therox 2020-07-30 13:56:14 -04:00 committed by GitHub
parent bffe3540fa
commit faf128de15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 6 deletions

View File

@ -483,7 +483,8 @@ namespace ts {
return node.pos;
}
if (isJSDocNode(node)) {
if (isJSDocNode(node) || node.kind === SyntaxKind.JsxText) {
// JsxText cannot actually contain comments, even though the scanner will think it sees comments
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true);
}

View File

@ -720,6 +720,9 @@ namespace ts.formatting {
// proceed any parent tokens that are located prior to child.getStart()
const tokenInfo = formattingScanner.readTokenInfo(node);
if (tokenInfo.token.end > childStartPos) {
if (tokenInfo.token.pos > childStartPos) {
formattingScanner.skipToStartOf(child);
}
// stop when formatting scanner advances past the beginning of the child
break;
}
@ -731,13 +734,15 @@ namespace ts.formatting {
return inheritedIndentation;
}
// JSX text shouldn't affect indenting
if (isToken(child) && child.kind !== SyntaxKind.JsxText) {
if (isToken(child)) {
// if child node is a token, it does not impact indentation, proceed it using parent indentation scope rules
const tokenInfo = formattingScanner.readTokenInfo(child);
Debug.assert(tokenInfo.token.end === child.end, "Token end is child end");
consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child);
return inheritedIndentation;
// JSX text shouldn't affect indenting
if (child.kind !== SyntaxKind.JsxText) {
Debug.assert(tokenInfo.token.end === child.end, "Token end is child end");
consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child);
return inheritedIndentation;
}
}
const effectiveParentStartLine = child.kind === SyntaxKind.Decorator ? childStartLine : undecoratedParentStartLine;

View File

@ -12,6 +12,7 @@ namespace ts.formatting {
getCurrentLeadingTrivia(): TextRangeWithKind[] | undefined;
lastTrailingTriviaWasNewLine(): boolean;
skipToEndOf(node: Node): void;
skipToStartOf(node: Node): void;
}
const enum ScanAction {
@ -47,6 +48,7 @@ namespace ts.formatting {
getCurrentLeadingTrivia: () => leadingTrivia,
lastTrailingTriviaWasNewLine: () => wasNewLine,
skipToEndOf,
skipToStartOf,
});
lastTokenInfo = undefined;
@ -298,5 +300,15 @@ namespace ts.formatting {
leadingTrivia = undefined;
trailingTrivia = undefined;
}
function skipToStartOf(node: Node): void {
scanner.setTextPos(node.pos);
savedPos = scanner.getStartPos();
lastScanAction = undefined;
lastTokenInfo = undefined;
wasNewLine = false;
leadingTrivia = undefined;
trailingTrivia = undefined;
}
}
}

View File

@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>
// @Filename: foo.tsx
////const a = <div>
//// // <a />
////</div>
format.document();
verify.currentFileContentIs(`const a = <div>
// <a />
</div>`);