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
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3696,7 +3696,7 @@
"code": 95003
},

"Extract function into {0}": {
"Extract to {0}": {
"category": "Message",
"code": 95004
}
Expand Down
27 changes: 20 additions & 7 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4829,26 +4829,39 @@ namespace ts {
}

/* @internal */
export function isFunctionLikeKind(kind: SyntaxKind): boolean {
export function isFunctionLikeDeclaration(node: Node): node is FunctionLikeDeclaration {
return node && isFunctionLikeDeclarationKind(node.kind);
}

function isFunctionLikeDeclarationKind(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.Constructor:
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ArrowFunction:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return true;
default:
return false;
}
}

/* @internal */
export function isFunctionLikeKind(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.MethodSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.FunctionType:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.ConstructorType:
return true;
default:
return isFunctionLikeDeclarationKind(kind);
}

return false;
}

// Classes
Expand Down
75 changes: 38 additions & 37 deletions src/services/refactors/extractMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace ts.refactor.extractMethod {
// Don't issue refactorings with duplicated names.
// Scopes come back in "innermost first" order, so extractions will
// preferentially go into nearer scopes
const description = formatStringFromArgs(Diagnostics.Extract_function_into_0.message, [extr.scopeDescription]);
const description = formatStringFromArgs(Diagnostics.Extract_to_0.message, [extr.scopeDescription]);
if (!usedNames.has(description)) {
usedNames.set(description, true);
actions.push({
Expand Down Expand Up @@ -543,44 +543,45 @@ namespace ts.refactor.extractMethod {
}
}

function getDescriptionForScope(scope: Scope) {
if (isFunctionLike(scope)) {
switch (scope.kind) {
case SyntaxKind.Constructor:
return "constructor";
case SyntaxKind.FunctionExpression:
return scope.name
? `function expression ${scope.name.text}`
: "anonymous function expression";
case SyntaxKind.FunctionDeclaration:
return `function '${scope.name.text}'`;
case SyntaxKind.ArrowFunction:
return "arrow function";
case SyntaxKind.MethodDeclaration:
return `method '${scope.name.getText()}`;
case SyntaxKind.GetAccessor:
return `'get ${scope.name.getText()}'`;
case SyntaxKind.SetAccessor:
return `'set ${scope.name.getText()}'`;
}
}
else if (isModuleBlock(scope)) {
return `namespace '${scope.parent.name.getText()}'`;
}
else if (isClassLike(scope)) {
return scope.kind === SyntaxKind.ClassDeclaration
? `class '${scope.name.text}'`
: scope.name && scope.name.text
? `class expression '${scope.name.text}'`
: "anonymous class expression";
}
else if (isSourceFile(scope)) {
return scope.externalModuleIndicator ? "module scope" : "global scope";
}
else {
return "unknown";
function getDescriptionForScope(scope: Scope): string {
return isFunctionLikeDeclaration(scope)
? `inner function in ${getDescriptionForFunctionLikeDeclaration(scope)}`
: isClassLike(scope)
? `method in ${getDescriptionForClassLikeDeclaration(scope)}`
: `function in ${getDescriptionForModuleLikeDeclaration(scope)}`;
}
function getDescriptionForFunctionLikeDeclaration(scope: FunctionLikeDeclaration): string {
switch (scope.kind) {
case SyntaxKind.Constructor:
return "constructor";
case SyntaxKind.FunctionExpression:
return scope.name
? `function expression '${scope.name.text}'`
: "anonymous function expression";
case SyntaxKind.FunctionDeclaration:
return `function '${scope.name.text}'`;
case SyntaxKind.ArrowFunction:
return "arrow function";
case SyntaxKind.MethodDeclaration:
return `method '${scope.name.getText()}`;
case SyntaxKind.GetAccessor:
return `'get ${scope.name.getText()}'`;
case SyntaxKind.SetAccessor:
return `'set ${scope.name.getText()}'`;
default:
Debug.assertNever(scope);
}
}
function getDescriptionForClassLikeDeclaration(scope: ClassLikeDeclaration): string {
return scope.kind === SyntaxKind.ClassDeclaration
? `class '${scope.name.text}'`
: scope.name ? `class expression '${scope.name.text}'` : "anonymous class expression";
}
function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string {
return scope.kind === SyntaxKind.ModuleBlock
? `namespace '${scope.parent.name.getText()}'`
: scope.externalModuleIndicator ? "module scope" : "global scope";
}

function getUniqueName(isNameOkay: (name: string) => boolean) {
let functionNameText = "newFunction";
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/extractMethod/extractMethod1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace A {
}
}
}
// ==SCOPE::function 'a'==
// ==SCOPE::inner function in function 'a'==
namespace A {
let x = 1;
function foo() {
Expand All @@ -34,7 +34,7 @@ namespace A {
}
}
}
// ==SCOPE::namespace 'B'==
// ==SCOPE::function in namespace 'B'==
namespace A {
let x = 1;
function foo() {
Expand All @@ -55,7 +55,7 @@ namespace A {
}
}
}
// ==SCOPE::namespace 'A'==
// ==SCOPE::function in namespace 'A'==
namespace A {
let x = 1;
function foo() {
Expand All @@ -76,7 +76,7 @@ namespace A {
return a;
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
namespace A {
let x = 1;
function foo() {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extractMethod/extractMethod10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace A {
}
}
}
// ==SCOPE::class 'C'==
// ==SCOPE::method in class 'C'==
namespace A {
export interface I { x: number };
class C {
Expand All @@ -24,7 +24,7 @@ namespace A {
}
}
}
// ==SCOPE::namespace 'A'==
// ==SCOPE::function in namespace 'A'==
namespace A {
export interface I { x: number };
class C {
Expand All @@ -39,7 +39,7 @@ namespace A {
return a1.x + 10;
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
namespace A {
export interface I { x: number };
class C {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extractMethod/extractMethod11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace A {
}
}
}
// ==SCOPE::class 'C'==
// ==SCOPE::method in class 'C'==
namespace A {
let y = 1;
class C {
Expand All @@ -30,7 +30,7 @@ namespace A {
}
}
}
// ==SCOPE::namespace 'A'==
// ==SCOPE::function in namespace 'A'==
namespace A {
let y = 1;
class C {
Expand All @@ -49,7 +49,7 @@ namespace A {
return { __return: a1.x + 10, z };
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
namespace A {
let y = 1;
class C {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/extractMethod/extractMethod12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace A {
}
}
}
// ==SCOPE::class 'C'==
// ==SCOPE::method in class 'C'==
namespace A {
let y = 1;
class C {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extractMethod/extractMethod13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
}
}
// ==SCOPE::function 'F2'==
// ==SCOPE::inner function in function 'F2'==
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
Expand All @@ -34,7 +34,7 @@
}
}
}
// ==SCOPE::function 'F1'==
// ==SCOPE::inner function in function 'F1'==
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
Expand All @@ -54,7 +54,7 @@
}
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extractMethod/extractMethod14.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function F<T>(t1: T) {
t2.toString();
}
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T>(t1: T) {
function F<T>(t2: T) {
newFunction();
Expand All @@ -16,7 +16,7 @@ function F<T>(t1: T) {
}
}
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T>(t1: T) {
function F<T>(t2: T) {
newFunction<T>(t2);
Expand All @@ -27,7 +27,7 @@ function F<T>(t1: T) {
t2.toString();
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
function F<T>(t1: T) {
function F<T>(t2: T) {
newFunction<T, T>(t1, t2);
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extractMethod/extractMethod15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function F<T>(t1: T) {
t2.toString();
}
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T>(t1: T) {
function F<U extends T[]>(t2: U) {
newFunction();
Expand All @@ -14,7 +14,7 @@ function F<T>(t1: T) {
}
}
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T>(t1: T) {
function F<U extends T[]>(t2: U) {
newFunction<U>(t2);
Expand All @@ -24,7 +24,7 @@ function F<T>(t1: T) {
t2.toString();
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
function F<T>(t1: T) {
function F<U extends T[]>(t2: U) {
newFunction<T, U>(t2);
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/extractMethod/extractMethod16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
function F<T>() {
const array: T[] = [];
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T>() {
const array: T[] = newFunction();

function newFunction(): T[] {
return [];
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
function F<T>() {
const array: T[] = newFunction<T>();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/extractMethod/extractMethod17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class C<T1, T2> {
t1.toString();
}
}
// ==SCOPE::class 'C'==
// ==SCOPE::method in class 'C'==
class C<T1, T2> {
M(t1: T1, t2: T2) {
this.newFunction(t1);
Expand All @@ -14,7 +14,7 @@ class C<T1, T2> {
t1.toString();
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
class C<T1, T2> {
M(t1: T1, t2: T2) {
newFunction<T1>(t1);
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/extractMethod/extractMethod18.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class C {
t1.toString();
}
}
// ==SCOPE::class 'C'==
// ==SCOPE::method in class 'C'==
class C {
M<T1, T2>(t1: T1, t2: T2) {
this.newFunction<T1>(t1);
Expand All @@ -14,7 +14,7 @@ class C {
t1.toString();
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
class C {
M<T1, T2>(t1: T1, t2: T2) {
newFunction<T1>(t1);
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/extractMethod/extractMethod19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
function F<T, U extends T[], V extends U[]>(v: V) {
v.toString();
}
// ==SCOPE::function 'F'==
// ==SCOPE::inner function in function 'F'==
function F<T, U extends T[], V extends U[]>(v: V) {
newFunction();

function newFunction() {
v.toString();
}
}
// ==SCOPE::global scope==
// ==SCOPE::function in global scope==
function F<T, U extends T[], V extends U[]>(v: V) {
newFunction<T, U, V>(v);
}
Expand Down
Loading