Skip to content

Commit 0a83aed

Browse files
committed
fix(29890): wrap variable/method/property to jsx expression
1 parent 3e0d58f commit 0a83aed

19 files changed

+585
-7
lines changed

src/services/refactors/extractSymbol.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ namespace ts.refactor.extractSymbol {
841841
if (range.facts & RangeFacts.IsAsyncFunction) {
842842
call = createAwait(call);
843843
}
844+
if (isInJSXContent(node)) {
845+
call = createJsxExpression(/*dotDotDotToken*/ undefined, call);
846+
}
844847

845848
if (exposedVariableDeclarations.length && !writes) {
846849
// No need to mix declarations and writes.
@@ -1044,12 +1047,16 @@ namespace ts.refactor.extractSymbol {
10441047
variableType,
10451048
initializer);
10461049

1047-
const localReference = createPropertyAccess(
1050+
let localReference: Expression = createPropertyAccess(
10481051
rangeFacts & RangeFacts.InStaticRegion
10491052
? createIdentifier(scope.name!.getText()) // TODO: GH#18217
10501053
: createThis(),
10511054
createIdentifier(localNameText));
10521055

1056+
if (isInJSXContent(node)) {
1057+
localReference = createJsxExpression(/*dotDotDotToken*/ undefined, localReference);
1058+
}
1059+
10531060
// Declare
10541061
const maxInsertionPos = node.pos;
10551062
const nodeToInsertBefore = getNodeToInsertPropertyBefore(maxInsertionPos, scope);
@@ -1120,12 +1127,6 @@ namespace ts.refactor.extractSymbol {
11201127
const renameLocation = getRenameLocation(edits, renameFilename, localNameText, /*isDeclaredBeforeUse*/ true);
11211128
return { renameFilename, renameLocation, edits };
11221129

1123-
function isInJSXContent(node: Node) {
1124-
if (!isJsxElement(node)) return false;
1125-
if (isJsxElement(node.parent)) return true;
1126-
return false;
1127-
}
1128-
11291130
function transformFunctionInitializerAndType(variableType: TypeNode | undefined, initializer: Expression): { variableType: TypeNode | undefined, initializer: Expression } {
11301131
// If no contextual type exists there is nothing to transfer to the function signature
11311132
if (variableType === undefined) return { variableType, initializer };
@@ -1875,4 +1876,8 @@ namespace ts.refactor.extractSymbol {
18751876
return false;
18761877
}
18771878
}
1879+
1880+
function isInJSXContent(node: Node) {
1881+
return (isJsxElement(node) || isJsxSelfClosingElement(node) || isJsxFragment(node)) && isJsxElement(node.parent);
1882+
}
18781883
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<span></span>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_1",
18+
actionDescription: "Extract to constant in global scope",
19+
newContent:
20+
`const /*RENAME*/newLocal = <span></span>;
21+
function Foo() {
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<span></span>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_0",
18+
actionDescription: "Extract to constant in enclosing scope",
19+
newContent:
20+
`function Foo() {
21+
const /*RENAME*/newLocal = <span></span>;
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////declare var React: any;
6+
////class Foo extends React.Component<{}, {}> {
7+
//// render() {
8+
//// return (
9+
//// <div>
10+
//// /*a*/<span></span>/*b*/
11+
//// </div>
12+
//// );
13+
//// }
14+
////}
15+
16+
goTo.file("a.tsx");
17+
goTo.select("a", "b");
18+
edit.applyRefactor({
19+
refactorName: "Extract Symbol",
20+
actionName: "constant_scope_1",
21+
actionDescription: "Extract to readonly field in class 'Foo'",
22+
newContent:
23+
`declare var React: any;
24+
class Foo extends React.Component<{}, {}> {
25+
private readonly newProperty = <span></span>;
26+
27+
render() {
28+
return (
29+
<div>
30+
{this./*RENAME*/newProperty}
31+
</div>
32+
);
33+
}
34+
}`
35+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<></>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_1",
18+
actionDescription: "Extract to constant in global scope",
19+
newContent:
20+
`const /*RENAME*/newLocal = <></>;
21+
function Foo() {
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<></>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_0",
18+
actionDescription: "Extract to constant in enclosing scope",
19+
newContent:
20+
`function Foo() {
21+
const /*RENAME*/newLocal = <></>;
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////declare var React: any;
6+
////class Foo extends React.Component<{}, {}> {
7+
//// render() {
8+
//// return (
9+
//// <div>
10+
//// /*a*/<></>/*b*/
11+
//// </div>
12+
//// );
13+
//// }
14+
////}
15+
16+
goTo.file("a.tsx");
17+
goTo.select("a", "b");
18+
edit.applyRefactor({
19+
refactorName: "Extract Symbol",
20+
actionName: "constant_scope_1",
21+
actionDescription: "Extract to readonly field in class 'Foo'",
22+
newContent:
23+
`declare var React: any;
24+
class Foo extends React.Component<{}, {}> {
25+
private readonly newProperty = <></>;
26+
27+
render() {
28+
return (
29+
<div>
30+
{this./*RENAME*/newProperty}
31+
</div>
32+
);
33+
}
34+
}`
35+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<br />/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_1",
18+
actionDescription: "Extract to constant in global scope",
19+
newContent:
20+
`const /*RENAME*/newLocal = <br />;
21+
function Foo() {
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<br />/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "constant_scope_0",
18+
actionDescription: "Extract to constant in enclosing scope",
19+
newContent:
20+
`function Foo() {
21+
const /*RENAME*/newLocal = <br />;
22+
return (
23+
<div>
24+
{newLocal}
25+
</div>
26+
);
27+
}`
28+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////declare var React: any;
6+
////class Foo extends React.Component<{}, {}> {
7+
//// render() {
8+
//// return (
9+
//// <div>
10+
//// /*a*/<br />/*b*/
11+
//// </div>
12+
//// );
13+
//// }
14+
////}
15+
16+
goTo.file("a.tsx");
17+
goTo.select("a", "b");
18+
edit.applyRefactor({
19+
refactorName: "Extract Symbol",
20+
actionName: "constant_scope_1",
21+
actionDescription: "Extract to readonly field in class 'Foo'",
22+
newContent:
23+
`declare var React: any;
24+
class Foo extends React.Component<{}, {}> {
25+
private readonly newProperty = <br />;
26+
27+
render() {
28+
return (
29+
<div>
30+
{this./*RENAME*/newProperty}
31+
</div>
32+
);
33+
}
34+
}`
35+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<span></span>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "function_scope_1",
18+
actionDescription: "Extract to function in global scope",
19+
newContent:
20+
`function Foo() {
21+
return (
22+
<div>
23+
{newFunction()}
24+
</div>
25+
);
26+
}
27+
28+
function /*RENAME*/newFunction() {
29+
return <span></span>;
30+
}
31+
`
32+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @jsx: preserve
4+
// @filename: a.tsx
5+
////function Foo() {
6+
//// return (
7+
//// <div>
8+
//// /*a*/<span></span>/*b*/
9+
//// </div>
10+
//// );
11+
////}
12+
13+
goTo.file("a.tsx");
14+
goTo.select("a", "b");
15+
edit.applyRefactor({
16+
refactorName: "Extract Symbol",
17+
actionName: "function_scope_0",
18+
actionDescription: "Extract to inner function in function 'Foo'",
19+
newContent:
20+
`function Foo() {
21+
return (
22+
<div>
23+
{newFunction()}
24+
</div>
25+
);
26+
27+
function /*RENAME*/newFunction() {
28+
return <span></span>;
29+
}
30+
}`
31+
});

0 commit comments

Comments
 (0)