Handle extracting case clause expression as constant

This commit is contained in:
Andrew Casey 2018-01-23 10:39:26 -08:00
parent a3387cc41f
commit 4f11dd68ab
4 changed files with 40 additions and 0 deletions

View File

@ -273,6 +273,13 @@ let i: I = [#|{ a: 1 }|];
const myObj: { member(x: number, y: string): void } = {
member: [#|(x, y) => x + y|],
}
`);
testExtractConstant("extractConstant_CaseClauseExpression", `
switch (1) {
case [#|1|]:
break;
}
`);
});

View File

@ -1321,6 +1321,13 @@ namespace ts.refactor.extractSymbol {
}
prevStatement = statement;
}
if (!prevStatement && isCaseClause(curr)) {
// We must have been in the expression of the case clause.
Debug.assert(isSwitchStatement(curr.parent.parent));
return curr.parent.parent;
}
// There must be at least one statement since we started in one.
Debug.assert(prevStatement !== undefined);
return prevStatement;

View File

@ -0,0 +1,13 @@
// ==ORIGINAL==
switch (1) {
case /*[#|*/1/*|]*/:
break;
}
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1;
switch (1) {
case /*RENAME*/newLocal:
break;
}

View File

@ -0,0 +1,13 @@
// ==ORIGINAL==
switch (1) {
case /*[#|*/1/*|]*/:
break;
}
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1;
switch (1) {
case /*RENAME*/newLocal:
break;
}