From 3e29397d74c0dc2ad4c9df2fec6c1a41eb404c2a Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Wed, 26 May 2021 21:24:02 +0100 Subject: [PATCH] fix(44249): JSX: "extract to constant" generates invalid code when using fragment syntax (#44252) Fixes https://github.com/microsoft/TypeScript/issues/44249 --- src/services/refactors/extractSymbol.ts | 2 +- .../extract-const_insideJsxFragment1.ts | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/extract-const_insideJsxFragment1.ts diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 832725a9cda..c85a825a958 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -2002,6 +2002,6 @@ namespace ts.refactor.extractSymbol { } function isInJSXContent(node: Node) { - return (isJsxElement(node) || isJsxSelfClosingElement(node) || isJsxFragment(node)) && isJsxElement(node.parent); + return (isJsxElement(node) || isJsxSelfClosingElement(node) || isJsxFragment(node)) && (isJsxElement(node.parent) || isJsxFragment(node.parent)); } } diff --git a/tests/cases/fourslash/extract-const_insideJsxFragment1.ts b/tests/cases/fourslash/extract-const_insideJsxFragment1.ts new file mode 100644 index 00000000000..bbde285183b --- /dev/null +++ b/tests/cases/fourslash/extract-const_insideJsxFragment1.ts @@ -0,0 +1,28 @@ +/// + +// @jsx: preserve +// @filename: a.tsx +////function Foo() { +//// return ( +//// <> +//// /*a*//*b*/ +//// +//// ); +////} + +goTo.file("a.tsx"); +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "constant_scope_1", + actionDescription: "Extract to constant in global scope", + newContent: +`const /*RENAME*/newLocal = ; +function Foo() { + return ( + <> + {newLocal} + + ); +}` +});