From 4d2aa9bf2cef463df1cfea3b206eb3f04e1ca765 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 20 Sep 2017 15:01:04 -0700 Subject: [PATCH] Fix formatting when keyword is parsed as part of a JSX identifier (e.g. `module-layout`) (#18598) --- src/services/formatting/formattingScanner.ts | 5 +++-- .../cases/fourslash/formatJsxWithKeywordInIdentifier.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/formatJsxWithKeywordInIdentifier.ts diff --git a/src/services/formatting/formattingScanner.ts b/src/services/formatting/formattingScanner.ts index 9b4e1be323b..d69fb141ba1 100644 --- a/src/services/formatting/formattingScanner.ts +++ b/src/services/formatting/formattingScanner.ts @@ -125,7 +125,8 @@ namespace ts.formatting { case SyntaxKind.JsxOpeningElement: case SyntaxKind.JsxClosingElement: case SyntaxKind.JsxSelfClosingElement: - return node.kind === SyntaxKind.Identifier; + // May parse an identifier like `module-layout`; that will be scanned as a keyword at first, but we should parse the whole thing to get an identifier. + return isKeyword(node.kind) || node.kind === SyntaxKind.Identifier; } } @@ -209,7 +210,7 @@ namespace ts.formatting { currentToken = scanner.reScanTemplateToken(); lastScanAction = ScanAction.RescanTemplateToken; } - else if (expectedScanAction === ScanAction.RescanJsxIdentifier && currentToken === SyntaxKind.Identifier) { + else if (expectedScanAction === ScanAction.RescanJsxIdentifier) { currentToken = scanner.scanJsxIdentifier(); lastScanAction = ScanAction.RescanJsxIdentifier; } diff --git a/tests/cases/fourslash/formatJsxWithKeywordInIdentifier.ts b/tests/cases/fourslash/formatJsxWithKeywordInIdentifier.ts new file mode 100644 index 00000000000..20a7d746414 --- /dev/null +++ b/tests/cases/fourslash/formatJsxWithKeywordInIdentifier.ts @@ -0,0 +1,9 @@ +/// + +// Test that we don't crash when encountering a keyword in a JSX identifier. + +// @Filename: /a.tsx +////
+ +format.document(); +verify.currentFileContentIs(`
`);