Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe1242c

Browse filesBrowse files
author
Andy
authored
Don't try to extract import to a method (microsoft#18025)
1 parent 3a0ab74 commit fe1242c
Copy full SHA for fe1242c

3 files changed

+87-67Lines changed: 87 additions & 67 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/utilities.ts‎

Copy file name to clipboardExpand all lines: src/compiler/utilities.ts
+62-55Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,54 +4951,60 @@ namespace ts {
49514951
|| kind === SyntaxKind.NoSubstitutionTemplateLiteral;
49524952
}
49534953

4954-
function isLeftHandSideExpressionKind(kind: SyntaxKind): boolean {
4955-
return kind === SyntaxKind.PropertyAccessExpression
4956-
|| kind === SyntaxKind.ElementAccessExpression
4957-
|| kind === SyntaxKind.NewExpression
4958-
|| kind === SyntaxKind.CallExpression
4959-
|| kind === SyntaxKind.JsxElement
4960-
|| kind === SyntaxKind.JsxSelfClosingElement
4961-
|| kind === SyntaxKind.TaggedTemplateExpression
4962-
|| kind === SyntaxKind.ArrayLiteralExpression
4963-
|| kind === SyntaxKind.ParenthesizedExpression
4964-
|| kind === SyntaxKind.ObjectLiteralExpression
4965-
|| kind === SyntaxKind.ClassExpression
4966-
|| kind === SyntaxKind.FunctionExpression
4967-
|| kind === SyntaxKind.Identifier
4968-
|| kind === SyntaxKind.RegularExpressionLiteral
4969-
|| kind === SyntaxKind.NumericLiteral
4970-
|| kind === SyntaxKind.StringLiteral
4971-
|| kind === SyntaxKind.NoSubstitutionTemplateLiteral
4972-
|| kind === SyntaxKind.TemplateExpression
4973-
|| kind === SyntaxKind.FalseKeyword
4974-
|| kind === SyntaxKind.NullKeyword
4975-
|| kind === SyntaxKind.ThisKeyword
4976-
|| kind === SyntaxKind.TrueKeyword
4977-
|| kind === SyntaxKind.SuperKeyword
4978-
|| kind === SyntaxKind.ImportKeyword
4979-
|| kind === SyntaxKind.NonNullExpression
4980-
|| kind === SyntaxKind.MetaProperty;
4981-
}
4982-
49834954
/* @internal */
49844955
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
4985-
return isLeftHandSideExpressionKind(skipPartiallyEmittedExpressions(node).kind);
4986-
}
4987-
4988-
function isUnaryExpressionKind(kind: SyntaxKind): boolean {
4989-
return kind === SyntaxKind.PrefixUnaryExpression
4990-
|| kind === SyntaxKind.PostfixUnaryExpression
4991-
|| kind === SyntaxKind.DeleteExpression
4992-
|| kind === SyntaxKind.TypeOfExpression
4993-
|| kind === SyntaxKind.VoidExpression
4994-
|| kind === SyntaxKind.AwaitExpression
4995-
|| kind === SyntaxKind.TypeAssertionExpression
4996-
|| isLeftHandSideExpressionKind(kind);
4956+
switch (node.kind) {
4957+
case SyntaxKind.PropertyAccessExpression:
4958+
case SyntaxKind.ElementAccessExpression:
4959+
case SyntaxKind.NewExpression:
4960+
case SyntaxKind.CallExpression:
4961+
case SyntaxKind.JsxElement:
4962+
case SyntaxKind.JsxSelfClosingElement:
4963+
case SyntaxKind.TaggedTemplateExpression:
4964+
case SyntaxKind.ArrayLiteralExpression:
4965+
case SyntaxKind.ParenthesizedExpression:
4966+
case SyntaxKind.ObjectLiteralExpression:
4967+
case SyntaxKind.ClassExpression:
4968+
case SyntaxKind.FunctionExpression:
4969+
case SyntaxKind.Identifier:
4970+
case SyntaxKind.RegularExpressionLiteral:
4971+
case SyntaxKind.NumericLiteral:
4972+
case SyntaxKind.StringLiteral:
4973+
case SyntaxKind.NoSubstitutionTemplateLiteral:
4974+
case SyntaxKind.TemplateExpression:
4975+
case SyntaxKind.FalseKeyword:
4976+
case SyntaxKind.NullKeyword:
4977+
case SyntaxKind.ThisKeyword:
4978+
case SyntaxKind.TrueKeyword:
4979+
case SyntaxKind.SuperKeyword:
4980+
case SyntaxKind.NonNullExpression:
4981+
case SyntaxKind.MetaProperty:
4982+
return true;
4983+
case SyntaxKind.PartiallyEmittedExpression:
4984+
return isLeftHandSideExpression((node as PartiallyEmittedExpression).expression);
4985+
case SyntaxKind.ImportKeyword:
4986+
return node.parent.kind === SyntaxKind.CallExpression;
4987+
default:
4988+
return false;
4989+
}
49974990
}
49984991

49994992
/* @internal */
50004993
export function isUnaryExpression(node: Node): node is UnaryExpression {
5001-
return isUnaryExpressionKind(skipPartiallyEmittedExpressions(node).kind);
4994+
switch (node.kind) {
4995+
case SyntaxKind.PrefixUnaryExpression:
4996+
case SyntaxKind.PostfixUnaryExpression:
4997+
case SyntaxKind.DeleteExpression:
4998+
case SyntaxKind.TypeOfExpression:
4999+
case SyntaxKind.VoidExpression:
5000+
case SyntaxKind.AwaitExpression:
5001+
case SyntaxKind.TypeAssertionExpression:
5002+
return true;
5003+
case SyntaxKind.PartiallyEmittedExpression:
5004+
return isUnaryExpression((node as PartiallyEmittedExpression).expression);
5005+
default:
5006+
return isLeftHandSideExpression(node);
5007+
}
50025008
}
50035009

50045010
/* @internal */
@@ -5014,21 +5020,22 @@ namespace ts {
50145020
}
50155021
}
50165022

5017-
function isExpressionKind(kind: SyntaxKind) {
5018-
return kind === SyntaxKind.ConditionalExpression
5019-
|| kind === SyntaxKind.YieldExpression
5020-
|| kind === SyntaxKind.ArrowFunction
5021-
|| kind === SyntaxKind.BinaryExpression
5022-
|| kind === SyntaxKind.SpreadElement
5023-
|| kind === SyntaxKind.AsExpression
5024-
|| kind === SyntaxKind.OmittedExpression
5025-
|| kind === SyntaxKind.CommaListExpression
5026-
|| isUnaryExpressionKind(kind);
5027-
}
5028-
50295023
/* @internal */
50305024
export function isExpression(node: Node): node is Expression {
5031-
return isExpressionKind(skipPartiallyEmittedExpressions(node).kind);
5025+
switch (node.kind) {
5026+
case SyntaxKind.ConditionalExpression:
5027+
case SyntaxKind.YieldExpression:
5028+
case SyntaxKind.ArrowFunction:
5029+
case SyntaxKind.BinaryExpression:
5030+
case SyntaxKind.SpreadElement:
5031+
case SyntaxKind.AsExpression:
5032+
case SyntaxKind.OmittedExpression:
5033+
case SyntaxKind.CommaListExpression:
5034+
case SyntaxKind.PartiallyEmittedExpression:
5035+
return true;
5036+
default:
5037+
return isUnaryExpression(node);
5038+
}
50325039
}
50335040

50345041
export function isAssertionExpression(node: Node): node is AssertionExpression {
Collapse file

‎src/services/refactors/extractMethod.ts‎

Copy file name to clipboardExpand all lines: src/services/refactors/extractMethod.ts
+15-12Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,7 @@ namespace ts.refactor.extractMethod {
231231
if (errors) {
232232
return { errors };
233233
}
234-
235-
// If our selection is the expression in an ExpressionStatement, expand
236-
// the selection to include the enclosing Statement (this stops us
237-
// from trying to care about the return value of the extracted function
238-
// and eliminates double semicolon insertion in certain scenarios)
239-
const range = isStatement(start)
240-
? [start]
241-
: start.parent && start.parent.kind === SyntaxKind.ExpressionStatement
242-
? [start.parent as Statement]
243-
: start as Expression;
244-
245-
return { targetRange: { range, facts: rangeFacts, declarations } };
234+
return { targetRange: { range: getStatementOrExpressionRange(start), facts: rangeFacts, declarations } };
246235
}
247236

248237
function createErrorResult(sourceFile: SourceFile, start: number, length: number, message: DiagnosticMessage): RangeToExtract {
@@ -459,6 +448,20 @@ namespace ts.refactor.extractMethod {
459448
}
460449
}
461450

451+
function getStatementOrExpressionRange(node: Node): Statement[] | Expression {
452+
if (isStatement(node)) {
453+
return [node];
454+
}
455+
else if (isExpression(node)) {
456+
// If our selection is the expression in an ExpressionStatement, expand
457+
// the selection to include the enclosing Statement (this stops us
458+
// from trying to care about the return value of the extracted function
459+
// and eliminates double semicolon insertion in certain scenarios)
460+
return isExpressionStatement(node.parent) ? [node.parent] : node;
461+
}
462+
return undefined;
463+
}
464+
462465
function isValidExtractionTarget(node: Node): node is Scope {
463466
// Note that we don't use isFunctionLike because we don't want to put the extracted closure *inside* a method
464467
return (node.kind === SyntaxKind.FunctionDeclaration) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
Collapse file
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////i/**/mport _ from "./b";
5+
6+
// @Filename: /b.ts
7+
////export default function f() {}
8+
9+
goTo.marker("");
10+
verify.not.refactorAvailable('Extract Method');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.