mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
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:
parent
bffe3540fa
commit
faf128de15
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
10
tests/cases/fourslash/formatTSXWithInlineComment.ts
Normal file
10
tests/cases/fourslash/formatTSXWithInlineComment.ts
Normal 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>`);
|
||||
Loading…
x
Reference in New Issue
Block a user