File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -275,7 +275,7 @@ namespace ts.refactor.extractSymbol {
275
275
}
276
276
const cursorRequest = length === 0 && invoked ;
277
277
278
- const startToken = getTokenAtPosition ( sourceFile , span . start ) ;
278
+ const startToken = findFirstNonJsxWhitespaceToken ( sourceFile , span . start ) ;
279
279
const endToken = findTokenOnLeftOfPosition ( sourceFile , textSpanEnd ( span ) ) ;
280
280
/* If the refactoring command is invoked through a keyboard action it's safe to assume that the user is actively looking for
281
281
refactoring actions at the span location. As they may not know the exact range that will trigger a refactoring, we expand the
Original file line number Diff line number Diff line change @@ -1141,6 +1141,20 @@ namespace ts {
1141
1141
}
1142
1142
}
1143
1143
1144
+ /**
1145
+ * Returns the first token where position is in [start, end),
1146
+ * excluding `JsxText` tokens containing only whitespace.
1147
+ */
1148
+ export function findFirstNonJsxWhitespaceToken ( sourceFile : SourceFile , position : number ) : Node | undefined {
1149
+ let tokenAtPosition = getTokenAtPosition ( sourceFile , position ) ;
1150
+ while ( isWhiteSpaceOnlyJsxText ( tokenAtPosition ) ) {
1151
+ const nextToken = findNextToken ( tokenAtPosition , tokenAtPosition . parent , sourceFile ) ;
1152
+ if ( ! nextToken ) return ;
1153
+ tokenAtPosition = nextToken ;
1154
+ }
1155
+ return tokenAtPosition ;
1156
+ }
1157
+
1144
1158
/**
1145
1159
* The token on the left of the position is the token that strictly includes the position
1146
1160
* or sits to the left of the cursor if it is on a boundary. For example
Original file line number Diff line number Diff line change
1
+ /// <reference path='fourslash.ts' />
2
+
3
+ // Repro https://github.com/Microsoft/TypeScript/issues/42829
4
+
5
+ // @jsx : preserve
6
+ // @filename : a.tsx
7
+ ////export default function ComponentThatExhibitsIssue() {
8
+ //// return <div>
9
+ //// /*a*/ <div className="some-nested-data">
10
+ //// hello from my nested component
11
+ //// </div>
12
+ ////
13
+ //// /*b*/
14
+ //// </div>
15
+
16
+ goTo . file ( "a.tsx" ) ;
17
+ goTo . select ( "a" , "b" ) ;
18
+ edit . applyRefactor ( {
19
+ refactorName : "Extract Symbol" ,
20
+ actionName : "function_scope_1" ,
21
+ actionDescription : "Extract to function in module scope" ,
22
+ newContent :
23
+ `export default function ComponentThatExhibitsIssue() {
24
+ return <div>
25
+ {newFunction()}
26
+
27
+
28
+ </div>
29
+
30
+ function /*RENAME*/newFunction() {
31
+ return <div className="some-nested-data">
32
+ hello from my nested component
33
+ </div>;
34
+ }
35
+ `
36
+ } ) ;
You can’t perform that action at this time.
0 commit comments