Skip to content

Commit d1015bf

Browse files
authored
Merge pull request microsoft#18926 from amcasey/ExtractConstantArrow
Disallow constant extraction into expression-bodied arrow functions
2 parents 46e135b + 2cb965c commit d1015bf

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

src/harness/unittests/extractConstants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ const x = [#|2 + 1|];
214214
/* About x */
215215
const x = [#|2 + 1|];
216216
`);
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|];`);
217225
});
218226

219227
function testExtractConstant(caption: string, text: string) {

src/services/refactors/extractSymbol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ namespace ts.refactor.extractSymbol {
142142
export const CannotAccessVariablesFromNestedScopes = createMessage("Cannot access variables from nested scopes");
143143
export const CannotExtractToOtherFunctionLike = createMessage("Cannot extract method to a function-like scope that is not a function");
144144
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");
145146
}
146147

147148
enum RangeFacts {
@@ -1299,6 +1300,10 @@ namespace ts.refactor.extractSymbol {
12991300
if (isClassLike(scope) && isInJavaScriptFile(scope)) {
13001301
constantErrors.push(createDiagnosticForNode(scope, Messages.CannotExtractToJSClass));
13011302
}
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+
}
13021307
constantErrorsPerScope.push(constantErrors);
13031308
}
13041309

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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;

0 commit comments

Comments
 (0)