File tree Expand file tree Collapse file tree 6 files changed +61
-0
lines changed Expand file tree Collapse file tree 6 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,14 @@ const x = [#|2 + 1|];
214
214
/* About x */
215
215
const x = [#|2 + 1|];
216
216
` ) ;
217
+
218
+ testExtractConstant ( "extractConstant_ArrowFunction_Block" , `
219
+ const f = () => {
220
+ return [#|2 + 1|];
221
+ };` ) ;
222
+
223
+ testExtractConstant ( "extractConstant_ArrowFunction_Expression" ,
224
+ `const f = () => [#|2 + 1|];` ) ;
217
225
} ) ;
218
226
219
227
function testExtractConstant ( caption : string , text : string ) {
Original file line number Diff line number Diff line change @@ -142,6 +142,7 @@ namespace ts.refactor.extractSymbol {
142
142
export const CannotAccessVariablesFromNestedScopes = createMessage ( "Cannot access variables from nested scopes" ) ;
143
143
export const CannotExtractToOtherFunctionLike = createMessage ( "Cannot extract method to a function-like scope that is not a function" ) ;
144
144
export const CannotExtractToJSClass = createMessage ( "Cannot extract constant to a class scope in JS" ) ;
145
+ export const CannotExtractToExpressionArrowFunction = createMessage ( "Cannot extract constant to an arrow function without a block" ) ;
145
146
}
146
147
147
148
enum RangeFacts {
@@ -1299,6 +1300,10 @@ namespace ts.refactor.extractSymbol {
1299
1300
if ( isClassLike ( scope ) && isInJavaScriptFile ( scope ) ) {
1300
1301
constantErrors . push ( createDiagnosticForNode ( scope , Messages . CannotExtractToJSClass ) ) ;
1301
1302
}
1303
+ if ( isArrowFunction ( scope ) && ! isBlock ( scope . body ) ) {
1304
+ // TODO (https://github.com/Microsoft/TypeScript/issues/18924): allow this
1305
+ constantErrors . push ( createDiagnosticForNode ( scope , Messages . CannotExtractToExpressionArrowFunction ) ) ;
1306
+ }
1302
1307
constantErrorsPerScope . push ( constantErrors ) ;
1303
1308
}
1304
1309
Original file line number Diff line number Diff line change
1
+ // ==ORIGINAL==
2
+
3
+ const f = ( ) => {
4
+ return 2 + 1 ;
5
+ } ;
6
+ // ==SCOPE::Extract to constant in enclosing scope==
7
+
8
+ const f = ( ) => {
9
+ const newLocal = 2 + 1 ;
10
+
11
+ return /*RENAME*/ newLocal ;
12
+ } ;
13
+ // ==SCOPE::Extract to constant in global scope==
14
+ const newLocal = 2 + 1 ;
15
+
16
+ const f = ( ) => {
17
+ return /*RENAME*/ newLocal ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ // ==ORIGINAL==
2
+
3
+ const f = ( ) => {
4
+ return 2 + 1 ;
5
+ } ;
6
+ // ==SCOPE::Extract to constant in enclosing scope==
7
+
8
+ const f = ( ) => {
9
+ const newLocal = 2 + 1 ;
10
+
11
+ return /*RENAME*/ newLocal ;
12
+ } ;
13
+ // ==SCOPE::Extract to constant in global scope==
14
+ const newLocal = 2 + 1 ;
15
+
16
+ const f = ( ) => {
17
+ return /*RENAME*/ newLocal ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ // ==ORIGINAL==
2
+ const f = ( ) => 2 + 1 ;
3
+ // ==SCOPE::Extract to constant in global scope==
4
+ const newLocal = 2 + 1 ;
5
+
6
+ const f = ( ) => /*RENAME*/ newLocal ;
Original file line number Diff line number Diff line change
1
+ // ==ORIGINAL==
2
+ const f = ( ) => 2 + 1 ;
3
+ // ==SCOPE::Extract to constant in global scope==
4
+ const newLocal = 2 + 1 ;
5
+
6
+ const f = ( ) => /*RENAME*/ newLocal ;
You can’t perform that action at this time.
0 commit comments