Skip to content

Commit

Permalink
Fix formatting scanner on multiline JSX attribute strings (#35705)
Browse files Browse the repository at this point in the history
* Fix formatting scanner on multiline JSX attribute strings

* Update API
  • Loading branch information
andrewbranch authored Dec 16, 2019
1 parent 16aae96 commit 53c8b95
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace ts {
reScanTemplateToken(): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
reScanJsxToken(): JsxTokenSyntaxKind;
reScanLessThanToken(): SyntaxKind;
reScanQuestionToken(): SyntaxKind;
Expand Down Expand Up @@ -902,6 +903,7 @@ namespace ts {
reScanTemplateToken,
scanJsxIdentifier,
scanJsxAttributeValue,
reScanJsxAttributeValue,
reScanJsxToken,
reScanLessThanToken,
reScanQuestionToken,
Expand Down Expand Up @@ -2133,6 +2135,11 @@ namespace ts {
}
}

function reScanJsxAttributeValue(): SyntaxKind {
pos = tokenPos = startPos;
return scanJsxAttributeValue();
}

function scanJsDocToken(): JSDocSyntaxKind {
startPos = tokenPos = pos;
tokenFlags = TokenFlags.None;
Expand Down
9 changes: 9 additions & 0 deletions src/services/formatting/formattingScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace ts.formatting {
RescanTemplateToken,
RescanJsxIdentifier,
RescanJsxText,
RescanJsxAttributeValue,
}

export function getFormattingScanner<T>(text: string, languageVariant: LanguageVariant, startPos: number, endPos: number, cb: (scanner: FormattingScanner) => T): T {
Expand Down Expand Up @@ -133,6 +134,10 @@ namespace ts.formatting {
container.kind === SyntaxKind.TemplateTail;
}

function shouldRescanJsxAttributeValue(node: Node): boolean {
return node.parent && isJsxAttribute(node.parent) && node.parent.initializer === node;
}

function startsWithSlashToken(t: SyntaxKind): boolean {
return t === SyntaxKind.SlashToken || t === SyntaxKind.SlashEqualsToken;
}
Expand All @@ -147,6 +152,7 @@ namespace ts.formatting {
shouldRescanTemplateToken(n) ? ScanAction.RescanTemplateToken :
shouldRescanJsxIdentifier(n) ? ScanAction.RescanJsxIdentifier :
shouldRescanJsxText(n) ? ScanAction.RescanJsxText :
shouldRescanJsxAttributeValue(n) ? ScanAction.RescanJsxAttributeValue :
ScanAction.Scan;

if (lastTokenInfo && expectedScanAction === lastScanAction) {
Expand Down Expand Up @@ -239,6 +245,9 @@ namespace ts.formatting {
case ScanAction.RescanJsxText:
lastScanAction = ScanAction.RescanJsxText;
return scanner.reScanJsxToken();
case ScanAction.RescanJsxAttributeValue:
lastScanAction = ScanAction.RescanJsxAttributeValue;
return scanner.reScanJsxAttributeValue();
case ScanAction.Scan:
break;
default:
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,7 @@ declare namespace ts {
reScanTemplateToken(): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
reScanJsxToken(): JsxTokenSyntaxKind;
reScanLessThanToken(): SyntaxKind;
reScanQuestionToken(): SyntaxKind;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,7 @@ declare namespace ts {
reScanTemplateToken(): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
reScanJsxToken(): JsxTokenSyntaxKind;
reScanLessThanToken(): SyntaxKind;
reScanQuestionToken(): SyntaxKind;
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/formatTsxMultilineAttributeString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts"/>

// @Filename: foo.tsx
////(
//// <input
//// value="x
//// x"
//// />
////);

format.document();
verify.currentFileContentIs(`(
<input
value="x
x"
/>
);`);

0 comments on commit 53c8b95

Please sign in to comment.