Skip to content

Commit e73b10a

Browse files
author
Andy
authored
Use isPartOfExpression in extractMethod, not isExpression (#18047)
* Use isPartOfExpression in extractMethod, not isExpression * Add whitespace
1 parent 4d05bfd commit e73b10a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/compiler/utilities.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,7 +4953,11 @@ namespace ts {
49534953

49544954
/* @internal */
49554955
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
4956-
switch (node.kind) {
4956+
return isLeftHandSideExpressionKind(skipPartiallyEmittedExpressions(node).kind);
4957+
}
4958+
4959+
function isLeftHandSideExpressionKind(kind: SyntaxKind): boolean {
4960+
switch (kind) {
49574961
case SyntaxKind.PropertyAccessExpression:
49584962
case SyntaxKind.ElementAccessExpression:
49594963
case SyntaxKind.NewExpression:
@@ -4979,19 +4983,20 @@ namespace ts {
49794983
case SyntaxKind.SuperKeyword:
49804984
case SyntaxKind.NonNullExpression:
49814985
case SyntaxKind.MetaProperty:
4986+
case SyntaxKind.ImportKeyword: // technically this is only an Expression if it's in a CallExpression
49824987
return true;
4983-
case SyntaxKind.PartiallyEmittedExpression:
4984-
return isLeftHandSideExpression((node as PartiallyEmittedExpression).expression);
4985-
case SyntaxKind.ImportKeyword:
4986-
return node.parent.kind === SyntaxKind.CallExpression;
49874988
default:
49884989
return false;
49894990
}
49904991
}
49914992

49924993
/* @internal */
49934994
export function isUnaryExpression(node: Node): node is UnaryExpression {
4994-
switch (node.kind) {
4995+
return isUnaryExpressionKind(skipPartiallyEmittedExpressions(node).kind);
4996+
}
4997+
4998+
function isUnaryExpressionKind(kind: SyntaxKind): boolean {
4999+
switch (kind) {
49955000
case SyntaxKind.PrefixUnaryExpression:
49965001
case SyntaxKind.PostfixUnaryExpression:
49975002
case SyntaxKind.DeleteExpression:
@@ -5000,10 +5005,8 @@ namespace ts {
50005005
case SyntaxKind.AwaitExpression:
50015006
case SyntaxKind.TypeAssertionExpression:
50025007
return true;
5003-
case SyntaxKind.PartiallyEmittedExpression:
5004-
return isUnaryExpression((node as PartiallyEmittedExpression).expression);
50055008
default:
5006-
return isLeftHandSideExpression(node);
5009+
return isLeftHandSideExpressionKind(kind);
50075010
}
50085011
}
50095012

@@ -5021,8 +5024,16 @@ namespace ts {
50215024
}
50225025

50235026
/* @internal */
5027+
/**
5028+
* Determines whether a node is an expression based only on its kind.
5029+
* Use `isPartOfExpression` if not in transforms.
5030+
*/
50245031
export function isExpression(node: Node): node is Expression {
5025-
switch (node.kind) {
5032+
return isExpressionKind(skipPartiallyEmittedExpressions(node).kind);
5033+
}
5034+
5035+
function isExpressionKind(kind: SyntaxKind): boolean {
5036+
switch (kind) {
50265037
case SyntaxKind.ConditionalExpression:
50275038
case SyntaxKind.YieldExpression:
50285039
case SyntaxKind.ArrowFunction:
@@ -5034,7 +5045,7 @@ namespace ts {
50345045
case SyntaxKind.PartiallyEmittedExpression:
50355046
return true;
50365047
default:
5037-
return isUnaryExpression(node);
5048+
return isUnaryExpressionKind(kind);
50385049
}
50395050
}
50405051

src/services/refactors/extractMethod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ namespace ts.refactor.extractMethod {
278278
Continue = 1 << 1,
279279
Return = 1 << 2
280280
}
281-
if (!isStatement(nodeToCheck) && !(isExpression(nodeToCheck) && isExtractableExpression(nodeToCheck))) {
281+
if (!isStatement(nodeToCheck) && !(isPartOfExpression(nodeToCheck) && isExtractableExpression(nodeToCheck))) {
282282
return [createDiagnosticForNode(nodeToCheck, Messages.StatementOrExpressionExpected)];
283283
}
284284

@@ -452,12 +452,12 @@ namespace ts.refactor.extractMethod {
452452
if (isStatement(node)) {
453453
return [node];
454454
}
455-
else if (isExpression(node)) {
455+
else if (isPartOfExpression(node)) {
456456
// If our selection is the expression in an ExpressionStatement, expand
457457
// the selection to include the enclosing Statement (this stops us
458458
// from trying to care about the return value of the extracted function
459459
// and eliminates double semicolon insertion in certain scenarios)
460-
return isExpressionStatement(node.parent) ? [node.parent] : node;
460+
return isExpressionStatement(node.parent) ? [node.parent] : node as Expression;
461461
}
462462
return undefined;
463463
}

0 commit comments

Comments
 (0)