Skip to content

Commit 4f11dd6

Browse files
committed
Handle extracting case clause expression as constant
1 parent a3387cc commit 4f11dd6

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/harness/unittests/extractConstants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ let i: I = [#|{ a: 1 }|];
273273
const myObj: { member(x: number, y: string): void } = {
274274
member: [#|(x, y) => x + y|],
275275
}
276+
`);
277+
278+
testExtractConstant("extractConstant_CaseClauseExpression", `
279+
switch (1) {
280+
case [#|1|]:
281+
break;
282+
}
276283
`);
277284
});
278285

src/services/refactors/extractSymbol.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,13 @@ namespace ts.refactor.extractSymbol {
13211321
}
13221322
prevStatement = statement;
13231323
}
1324+
1325+
if (!prevStatement && isCaseClause(curr)) {
1326+
// We must have been in the expression of the case clause.
1327+
Debug.assert(isSwitchStatement(curr.parent.parent));
1328+
return curr.parent.parent;
1329+
}
1330+
13241331
// There must be at least one statement since we started in one.
13251332
Debug.assert(prevStatement !== undefined);
13261333
return prevStatement;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ==ORIGINAL==
2+
3+
switch (1) {
4+
case /*[#|*/1/*|]*/:
5+
break;
6+
}
7+
8+
// ==SCOPE::Extract to constant in enclosing scope==
9+
const newLocal = 1;
10+
switch (1) {
11+
case /*RENAME*/newLocal:
12+
break;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ==ORIGINAL==
2+
3+
switch (1) {
4+
case /*[#|*/1/*|]*/:
5+
break;
6+
}
7+
8+
// ==SCOPE::Extract to constant in enclosing scope==
9+
const newLocal = 1;
10+
switch (1) {
11+
case /*RENAME*/newLocal:
12+
break;
13+
}

0 commit comments

Comments
 (0)