Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4953,7 +4953,11 @@ namespace ts {

/* @internal */
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
switch (node.kind) {
return isLeftHandSideExpressionKind(skipPartiallyEmittedExpressions(node).kind);
}

function isLeftHandSideExpressionKind(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
case SyntaxKind.NewExpression:
Expand All @@ -4979,19 +4983,20 @@ namespace ts {
case SyntaxKind.SuperKeyword:
case SyntaxKind.NonNullExpression:
case SyntaxKind.MetaProperty:
case SyntaxKind.ImportKeyword: // technically this is only an Expression if it's in a CallExpression
return true;
case SyntaxKind.PartiallyEmittedExpression:
return isLeftHandSideExpression((node as PartiallyEmittedExpression).expression);
case SyntaxKind.ImportKeyword:
return node.parent.kind === SyntaxKind.CallExpression;
default:
return false;
}
}

/* @internal */
export function isUnaryExpression(node: Node): node is UnaryExpression {
switch (node.kind) {
return isUnaryExpressionKind(skipPartiallyEmittedExpressions(node).kind);
}

function isUnaryExpressionKind(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.PrefixUnaryExpression:
case SyntaxKind.PostfixUnaryExpression:
case SyntaxKind.DeleteExpression:
Expand All @@ -5000,10 +5005,8 @@ namespace ts {
case SyntaxKind.AwaitExpression:
case SyntaxKind.TypeAssertionExpression:
return true;
case SyntaxKind.PartiallyEmittedExpression:
return isUnaryExpression((node as PartiallyEmittedExpression).expression);
default:
return isLeftHandSideExpression(node);
return isLeftHandSideExpressionKind(kind);
}
}

Expand All @@ -5021,8 +5024,16 @@ namespace ts {
}

/* @internal */
/**
* Determines whether a node is an expression based only on its kind.
* Use `isPartOfExpression` if not in transforms.
*/
export function isExpression(node: Node): node is Expression {
switch (node.kind) {
return isExpressionKind(skipPartiallyEmittedExpressions(node).kind);
}

function isExpressionKind(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.ConditionalExpression:
case SyntaxKind.YieldExpression:
case SyntaxKind.ArrowFunction:
Expand All @@ -5034,7 +5045,7 @@ namespace ts {
case SyntaxKind.PartiallyEmittedExpression:
return true;
default:
return isUnaryExpression(node);
return isUnaryExpressionKind(kind);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/refactors/extractMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace ts.refactor.extractMethod {
Continue = 1 << 1,
Return = 1 << 2
}
if (!isStatement(nodeToCheck) && !(isExpression(nodeToCheck) && isExtractableExpression(nodeToCheck))) {
if (!isStatement(nodeToCheck) && !(isPartOfExpression(nodeToCheck) && isExtractableExpression(nodeToCheck))) {
return [createDiagnosticForNode(nodeToCheck, Messages.StatementOrExpressionExpected)];
}

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