Skip to content

Commit e51e91d

Browse files
author
Andy
authored
Change wording of scope description (#18342)
1 parent 018c645 commit e51e91d

39 files changed

+140
-126
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3696,7 +3696,7 @@
36963696
"code": 95003
36973697
},
36983698

3699-
"Extract function into {0}": {
3699+
"Extract to {0}": {
37003700
"category": "Message",
37013701
"code": 95004
37023702
}

src/compiler/utilities.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,26 +4829,39 @@ namespace ts {
48294829
}
48304830

48314831
/* @internal */
4832-
export function isFunctionLikeKind(kind: SyntaxKind): boolean {
4832+
export function isFunctionLikeDeclaration(node: Node): node is FunctionLikeDeclaration {
4833+
return node && isFunctionLikeDeclarationKind(node.kind);
4834+
}
4835+
4836+
function isFunctionLikeDeclarationKind(kind: SyntaxKind): boolean {
48334837
switch (kind) {
4834-
case SyntaxKind.Constructor:
4835-
case SyntaxKind.FunctionExpression:
48364838
case SyntaxKind.FunctionDeclaration:
4837-
case SyntaxKind.ArrowFunction:
48384839
case SyntaxKind.MethodDeclaration:
4839-
case SyntaxKind.MethodSignature:
4840+
case SyntaxKind.Constructor:
48404841
case SyntaxKind.GetAccessor:
48414842
case SyntaxKind.SetAccessor:
4843+
case SyntaxKind.FunctionExpression:
4844+
case SyntaxKind.ArrowFunction:
4845+
return true;
4846+
default:
4847+
return false;
4848+
}
4849+
}
4850+
4851+
/* @internal */
4852+
export function isFunctionLikeKind(kind: SyntaxKind): boolean {
4853+
switch (kind) {
4854+
case SyntaxKind.MethodSignature:
48424855
case SyntaxKind.CallSignature:
48434856
case SyntaxKind.ConstructSignature:
48444857
case SyntaxKind.IndexSignature:
48454858
case SyntaxKind.FunctionType:
48464859
case SyntaxKind.JSDocFunctionType:
48474860
case SyntaxKind.ConstructorType:
48484861
return true;
4862+
default:
4863+
return isFunctionLikeDeclarationKind(kind);
48494864
}
4850-
4851-
return false;
48524865
}
48534866

48544867
// Classes

src/services/refactors/extractMethod.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace ts.refactor.extractMethod {
4040
// Don't issue refactorings with duplicated names.
4141
// Scopes come back in "innermost first" order, so extractions will
4242
// preferentially go into nearer scopes
43-
const description = formatStringFromArgs(Diagnostics.Extract_function_into_0.message, [extr.scopeDescription]);
43+
const description = formatStringFromArgs(Diagnostics.Extract_to_0.message, [extr.scopeDescription]);
4444
if (!usedNames.has(description)) {
4545
usedNames.set(description, true);
4646
actions.push({
@@ -543,44 +543,45 @@ namespace ts.refactor.extractMethod {
543543
}
544544
}
545545

546-
function getDescriptionForScope(scope: Scope) {
547-
if (isFunctionLike(scope)) {
548-
switch (scope.kind) {
549-
case SyntaxKind.Constructor:
550-
return "constructor";
551-
case SyntaxKind.FunctionExpression:
552-
return scope.name
553-
? `function expression ${scope.name.text}`
554-
: "anonymous function expression";
555-
case SyntaxKind.FunctionDeclaration:
556-
return `function '${scope.name.text}'`;
557-
case SyntaxKind.ArrowFunction:
558-
return "arrow function";
559-
case SyntaxKind.MethodDeclaration:
560-
return `method '${scope.name.getText()}`;
561-
case SyntaxKind.GetAccessor:
562-
return `'get ${scope.name.getText()}'`;
563-
case SyntaxKind.SetAccessor:
564-
return `'set ${scope.name.getText()}'`;
565-
}
566-
}
567-
else if (isModuleBlock(scope)) {
568-
return `namespace '${scope.parent.name.getText()}'`;
569-
}
570-
else if (isClassLike(scope)) {
571-
return scope.kind === SyntaxKind.ClassDeclaration
572-
? `class '${scope.name.text}'`
573-
: scope.name && scope.name.text
574-
? `class expression '${scope.name.text}'`
575-
: "anonymous class expression";
576-
}
577-
else if (isSourceFile(scope)) {
578-
return scope.externalModuleIndicator ? "module scope" : "global scope";
579-
}
580-
else {
581-
return "unknown";
546+
function getDescriptionForScope(scope: Scope): string {
547+
return isFunctionLikeDeclaration(scope)
548+
? `inner function in ${getDescriptionForFunctionLikeDeclaration(scope)}`
549+
: isClassLike(scope)
550+
? `method in ${getDescriptionForClassLikeDeclaration(scope)}`
551+
: `function in ${getDescriptionForModuleLikeDeclaration(scope)}`;
552+
}
553+
function getDescriptionForFunctionLikeDeclaration(scope: FunctionLikeDeclaration): string {
554+
switch (scope.kind) {
555+
case SyntaxKind.Constructor:
556+
return "constructor";
557+
case SyntaxKind.FunctionExpression:
558+
return scope.name
559+
? `function expression '${scope.name.text}'`
560+
: "anonymous function expression";
561+
case SyntaxKind.FunctionDeclaration:
562+
return `function '${scope.name.text}'`;
563+
case SyntaxKind.ArrowFunction:
564+
return "arrow function";
565+
case SyntaxKind.MethodDeclaration:
566+
return `method '${scope.name.getText()}`;
567+
case SyntaxKind.GetAccessor:
568+
return `'get ${scope.name.getText()}'`;
569+
case SyntaxKind.SetAccessor:
570+
return `'set ${scope.name.getText()}'`;
571+
default:
572+
Debug.assertNever(scope);
582573
}
583574
}
575+
function getDescriptionForClassLikeDeclaration(scope: ClassLikeDeclaration): string {
576+
return scope.kind === SyntaxKind.ClassDeclaration
577+
? `class '${scope.name.text}'`
578+
: scope.name ? `class expression '${scope.name.text}'` : "anonymous class expression";
579+
}
580+
function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string {
581+
return scope.kind === SyntaxKind.ModuleBlock
582+
? `namespace '${scope.parent.name.getText()}'`
583+
: scope.externalModuleIndicator ? "module scope" : "global scope";
584+
}
584585

585586
function getUniqueName(isNameOkay: (name: string) => boolean) {
586587
let functionNameText = "newFunction";

tests/baselines/reference/extractMethod/extractMethod1.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace A {
1414
}
1515
}
1616
}
17-
// ==SCOPE::function 'a'==
17+
// ==SCOPE::inner function in function 'a'==
1818
namespace A {
1919
let x = 1;
2020
function foo() {
@@ -34,7 +34,7 @@ namespace A {
3434
}
3535
}
3636
}
37-
// ==SCOPE::namespace 'B'==
37+
// ==SCOPE::function in namespace 'B'==
3838
namespace A {
3939
let x = 1;
4040
function foo() {
@@ -55,7 +55,7 @@ namespace A {
5555
}
5656
}
5757
}
58-
// ==SCOPE::namespace 'A'==
58+
// ==SCOPE::function in namespace 'A'==
5959
namespace A {
6060
let x = 1;
6161
function foo() {
@@ -76,7 +76,7 @@ namespace A {
7676
return a;
7777
}
7878
}
79-
// ==SCOPE::global scope==
79+
// ==SCOPE::function in global scope==
8080
namespace A {
8181
let x = 1;
8282
function foo() {

tests/baselines/reference/extractMethod/extractMethod10.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace A {
99
}
1010
}
1111
}
12-
// ==SCOPE::class 'C'==
12+
// ==SCOPE::method in class 'C'==
1313
namespace A {
1414
export interface I { x: number };
1515
class C {
@@ -24,7 +24,7 @@ namespace A {
2424
}
2525
}
2626
}
27-
// ==SCOPE::namespace 'A'==
27+
// ==SCOPE::function in namespace 'A'==
2828
namespace A {
2929
export interface I { x: number };
3030
class C {
@@ -39,7 +39,7 @@ namespace A {
3939
return a1.x + 10;
4040
}
4141
}
42-
// ==SCOPE::global scope==
42+
// ==SCOPE::function in global scope==
4343
namespace A {
4444
export interface I { x: number };
4545
class C {

tests/baselines/reference/extractMethod/extractMethod11.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace A {
1111
}
1212
}
1313
}
14-
// ==SCOPE::class 'C'==
14+
// ==SCOPE::method in class 'C'==
1515
namespace A {
1616
let y = 1;
1717
class C {
@@ -30,7 +30,7 @@ namespace A {
3030
}
3131
}
3232
}
33-
// ==SCOPE::namespace 'A'==
33+
// ==SCOPE::function in namespace 'A'==
3434
namespace A {
3535
let y = 1;
3636
class C {
@@ -49,7 +49,7 @@ namespace A {
4949
return { __return: a1.x + 10, z };
5050
}
5151
}
52-
// ==SCOPE::global scope==
52+
// ==SCOPE::function in global scope==
5353
namespace A {
5454
let y = 1;
5555
class C {

tests/baselines/reference/extractMethod/extractMethod12.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace A {
1313
}
1414
}
1515
}
16-
// ==SCOPE::class 'C'==
16+
// ==SCOPE::method in class 'C'==
1717
namespace A {
1818
let y = 1;
1919
class C {

tests/baselines/reference/extractMethod/extractMethod13.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
}
1616
}
17-
// ==SCOPE::function 'F2'==
17+
// ==SCOPE::inner function in function 'F2'==
1818
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
1919
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
2020
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
@@ -34,7 +34,7 @@
3434
}
3535
}
3636
}
37-
// ==SCOPE::function 'F1'==
37+
// ==SCOPE::inner function in function 'F1'==
3838
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
3939
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
4040
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
@@ -54,7 +54,7 @@
5454
}
5555
}
5656
}
57-
// ==SCOPE::global scope==
57+
// ==SCOPE::function in global scope==
5858
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
5959
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
6060
<U2a, U2b>(u2a: U2a, u2b: U2b) => {

tests/baselines/reference/extractMethod/extractMethod14.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function F<T>(t1: T) {
55
t2.toString();
66
}
77
}
8-
// ==SCOPE::function 'F'==
8+
// ==SCOPE::inner function in function 'F'==
99
function F<T>(t1: T) {
1010
function F<T>(t2: T) {
1111
newFunction();
@@ -16,7 +16,7 @@ function F<T>(t1: T) {
1616
}
1717
}
1818
}
19-
// ==SCOPE::function 'F'==
19+
// ==SCOPE::inner function in function 'F'==
2020
function F<T>(t1: T) {
2121
function F<T>(t2: T) {
2222
newFunction<T>(t2);
@@ -27,7 +27,7 @@ function F<T>(t1: T) {
2727
t2.toString();
2828
}
2929
}
30-
// ==SCOPE::global scope==
30+
// ==SCOPE::function in global scope==
3131
function F<T>(t1: T) {
3232
function F<T>(t2: T) {
3333
newFunction<T, T>(t1, t2);

tests/baselines/reference/extractMethod/extractMethod15.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function F<T>(t1: T) {
44
t2.toString();
55
}
66
}
7-
// ==SCOPE::function 'F'==
7+
// ==SCOPE::inner function in function 'F'==
88
function F<T>(t1: T) {
99
function F<U extends T[]>(t2: U) {
1010
newFunction();
@@ -14,7 +14,7 @@ function F<T>(t1: T) {
1414
}
1515
}
1616
}
17-
// ==SCOPE::function 'F'==
17+
// ==SCOPE::inner function in function 'F'==
1818
function F<T>(t1: T) {
1919
function F<U extends T[]>(t2: U) {
2020
newFunction<U>(t2);
@@ -24,7 +24,7 @@ function F<T>(t1: T) {
2424
t2.toString();
2525
}
2626
}
27-
// ==SCOPE::global scope==
27+
// ==SCOPE::function in global scope==
2828
function F<T>(t1: T) {
2929
function F<U extends T[]>(t2: U) {
3030
newFunction<T, U>(t2);

tests/baselines/reference/extractMethod/extractMethod16.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
function F<T>() {
33
const array: T[] = [];
44
}
5-
// ==SCOPE::function 'F'==
5+
// ==SCOPE::inner function in function 'F'==
66
function F<T>() {
77
const array: T[] = newFunction();
88

99
function newFunction(): T[] {
1010
return [];
1111
}
1212
}
13-
// ==SCOPE::global scope==
13+
// ==SCOPE::function in global scope==
1414
function F<T>() {
1515
const array: T[] = newFunction<T>();
1616
}

tests/baselines/reference/extractMethod/extractMethod17.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class C<T1, T2> {
44
t1.toString();
55
}
66
}
7-
// ==SCOPE::class 'C'==
7+
// ==SCOPE::method in class 'C'==
88
class C<T1, T2> {
99
M(t1: T1, t2: T2) {
1010
this.newFunction(t1);
@@ -14,7 +14,7 @@ class C<T1, T2> {
1414
t1.toString();
1515
}
1616
}
17-
// ==SCOPE::global scope==
17+
// ==SCOPE::function in global scope==
1818
class C<T1, T2> {
1919
M(t1: T1, t2: T2) {
2020
newFunction<T1>(t1);

tests/baselines/reference/extractMethod/extractMethod18.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class C {
44
t1.toString();
55
}
66
}
7-
// ==SCOPE::class 'C'==
7+
// ==SCOPE::method in class 'C'==
88
class C {
99
M<T1, T2>(t1: T1, t2: T2) {
1010
this.newFunction<T1>(t1);
@@ -14,7 +14,7 @@ class C {
1414
t1.toString();
1515
}
1616
}
17-
// ==SCOPE::global scope==
17+
// ==SCOPE::function in global scope==
1818
class C {
1919
M<T1, T2>(t1: T1, t2: T2) {
2020
newFunction<T1>(t1);

tests/baselines/reference/extractMethod/extractMethod19.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
function F<T, U extends T[], V extends U[]>(v: V) {
33
v.toString();
44
}
5-
// ==SCOPE::function 'F'==
5+
// ==SCOPE::inner function in function 'F'==
66
function F<T, U extends T[], V extends U[]>(v: V) {
77
newFunction();
88

99
function newFunction() {
1010
v.toString();
1111
}
1212
}
13-
// ==SCOPE::global scope==
13+
// ==SCOPE::function in global scope==
1414
function F<T, U extends T[], V extends U[]>(v: V) {
1515
newFunction<T, U, V>(v);
1616
}

0 commit comments

Comments
 (0)