Skip to content

Commit 724f71c

Browse files
committed
Describe the innermost scope as "enclosing scope" for constants
Using the same description as for functions was confusing because the extracted constant was inserted below the top-level of that scope.
1 parent ba5f09b commit 724f71c

File tree

53 files changed

+70
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+70
-63
lines changed

src/services/refactors/extractSymbol.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace ts.refactor.extractSymbol {
4343
// Don't issue refactorings with duplicated names.
4444
// Scopes come back in "innermost first" order, so extractions will
4545
// preferentially go into nearer scopes
46-
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [extraction.functionDescription, extraction.scopeDescription]);
46+
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [extraction.functionDescription, extraction.functionScopeDescription]);
4747
if (!usedFunctionNames.has(description)) {
4848
usedFunctionNames.set(description, true);
4949
functionActions.push({
@@ -58,7 +58,7 @@ namespace ts.refactor.extractSymbol {
5858
// Don't issue refactorings with duplicated names.
5959
// Scopes come back in "innermost first" order, so extractions will
6060
// preferentially go into nearer scopes
61-
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [extraction.constantDescription, extraction.scopeDescription]);
61+
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [extraction.constantDescription, extraction.constantScopeDescription]);
6262
if (!usedConstantNames.has(description)) {
6363
usedConstantNames.set(description, true);
6464
constantActions.push({
@@ -544,10 +544,11 @@ namespace ts.refactor.extractSymbol {
544544

545545
interface PossibleExtraction {
546546
readonly functionDescription: string;
547+
readonly functionScopeDescription: string;
547548
readonly functionErrors: ReadonlyArray<Diagnostic>;
548549
readonly constantDescription: string;
550+
readonly constantScopeDescription: string;
549551
readonly constantErrors: ReadonlyArray<Diagnostic>;
550-
readonly scopeDescription: string;
551552
}
552553
/**
553554
* Given a piece of text to extract ('targetRange'), computes a list of possible extractions.
@@ -557,17 +558,23 @@ namespace ts.refactor.extractSymbol {
557558
function getPossibleExtractions(targetRange: TargetRange, context: RefactorContext): ReadonlyArray<PossibleExtraction> | undefined {
558559
const { scopes, readsAndWrites: { functionErrorsPerScope, constantErrorsPerScope } } = getPossibleExtractionsWorker(targetRange, context);
559560
// Need the inner type annotation to avoid https://github.com/Microsoft/TypeScript/issues/7547
560-
const extractions = scopes.map((scope, i): PossibleExtraction => ({
561-
functionDescription: getDescriptionForFunctionInScope(scope),
562-
functionErrors: functionErrorsPerScope[i],
563-
constantDescription: getDescriptionForConstantInScope(scope),
564-
constantErrors: constantErrorsPerScope[i],
565-
scopeDescription: isFunctionLikeDeclaration(scope)
561+
const extractions = scopes.map((scope, i): PossibleExtraction => {
562+
const scopeDescription = isFunctionLikeDeclaration(scope)
566563
? getDescriptionForFunctionLikeDeclaration(scope)
567564
: isClassLike(scope)
568565
? getDescriptionForClassLikeDeclaration(scope)
569-
: getDescriptionForModuleLikeDeclaration(scope)
570-
}));
566+
: getDescriptionForModuleLikeDeclaration(scope);
567+
return {
568+
functionDescription: getDescriptionForFunctionInScope(scope),
569+
functionErrors: functionErrorsPerScope[i],
570+
functionScopeDescription: scopeDescription,
571+
constantDescription: getDescriptionForConstantInScope(scope),
572+
constantErrors: constantErrorsPerScope[i],
573+
constantScopeDescription: (i === 0 && !isClassLike(scope))
574+
? "enclosing scope" // Like "global scope" and "module scope", this is not localized.
575+
: scopeDescription,
576+
};
577+
});
571578
return extractions;
572579
}
573580

tests/baselines/reference/extractConstant/extractConstant_BlockScopeMismatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ for (let i = 0; i < 10; i++) {
66
}
77
}
88

9-
// ==SCOPE::Extract to constant in global scope==
9+
// ==SCOPE::Extract to constant in enclosing scope==
1010

1111
for (let i = 0; i < 10; i++) {
1212
for (let j = 0; j < 10; j++) {

tests/baselines/reference/extractConstant/extractConstant_BlockScopeMismatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ for (let i = 0; i < 10; i++) {
66
}
77
}
88

9-
// ==SCOPE::Extract to constant in global scope==
9+
// ==SCOPE::Extract to constant in enclosing scope==
1010

1111
for (let i = 0; i < 10; i++) {
1212
for (let j = 0; j < 10; j++) {

tests/baselines/reference/extractConstant/extractConstant_BlockScopes_NoDependencies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ for (let i = 0; i < 10; i++) {
44
let x = 1;
55
}
66
}
7-
// ==SCOPE::Extract to constant in global scope==
7+
// ==SCOPE::Extract to constant in enclosing scope==
88
for (let i = 0; i < 10; i++) {
99
for (let j = 0; j < 10; j++) {
1010
const newLocal = 1;

tests/baselines/reference/extractConstant/extractConstant_BlockScopes_NoDependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ for (let i = 0; i < 10; i++) {
44
let x = 1;
55
}
66
}
7-
// ==SCOPE::Extract to constant in global scope==
7+
// ==SCOPE::Extract to constant in enclosing scope==
88
for (let i = 0; i < 10; i++) {
99
for (let j = 0; j < 10; j++) {
1010
const newLocal = 1;

tests/baselines/reference/extractConstant/extractConstant_ClassInsertionPosition1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class C {
88
let x = 1;
99
}
1010
}
11-
// ==SCOPE::Extract to constant in method 'M3==
11+
// ==SCOPE::Extract to constant in enclosing scope==
1212
class C {
1313
a = 1;
1414
b = 2;

tests/baselines/reference/extractConstant/extractConstant_ClassInsertionPosition1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class C {
88
let x = 1;
99
}
1010
}
11-
// ==SCOPE::Extract to constant in method 'M3==
11+
// ==SCOPE::Extract to constant in enclosing scope==
1212
class C {
1313
a = 1;
1414
b = 2;

tests/baselines/reference/extractConstant/extractConstant_ClassInsertionPosition2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class C {
88
let x = 1;
99
}
1010
}
11-
// ==SCOPE::Extract to constant in method 'M3==
11+
// ==SCOPE::Extract to constant in enclosing scope==
1212
class C {
1313
a = 1;
1414
M1() { }

tests/baselines/reference/extractConstant/extractConstant_ClassInsertionPosition2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class C {
88
let x = 1;
99
}
1010
}
11-
// ==SCOPE::Extract to constant in method 'M3==
11+
// ==SCOPE::Extract to constant in enclosing scope==
1212
class C {
1313
a = 1;
1414
M1() { }

tests/baselines/reference/extractConstant/extractConstant_ClassInsertionPosition3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class C {
88
let x = 1;
99
}
1010
}
11-
// ==SCOPE::Extract to constant in method 'M3==
11+
// ==SCOPE::Extract to constant in enclosing scope==
1212
class C {
1313
M1() { }
1414
a = 1;

0 commit comments

Comments
 (0)