Skip to content

Commit b21d8a4

Browse files
authored
Merge pull request microsoft#18900 from amcasey/ReduceDup
Refactor to reduce property duplication
2 parents 4d8663c + e1380a8 commit b21d8a4

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/services/refactors/extractSymbol.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ namespace ts.refactor.extractSymbol {
3737
const usedConstantNames: Map<boolean> = createMap();
3838

3939
let i = 0;
40-
for (const extraction of extractions) {
40+
for (const {functionExtraction, constantExtraction} of extractions) {
4141
// Skip these since we don't have a way to report errors yet
42-
if (extraction.functionErrors.length === 0) {
42+
if (functionExtraction.errors.length === 0) {
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.functionScopeDescription]);
46+
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [functionExtraction.description, functionExtraction.scopeDescription]);
4747
if (!usedFunctionNames.has(description)) {
4848
usedFunctionNames.set(description, true);
4949
functionActions.push({
@@ -54,11 +54,11 @@ namespace ts.refactor.extractSymbol {
5454
}
5555

5656
// Skip these since we don't have a way to report errors yet
57-
if (extraction.constantErrors.length === 0) {
57+
if (constantExtraction.errors.length === 0) {
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.constantScopeDescription]);
61+
const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [constantExtraction.description, constantExtraction.scopeDescription]);
6262
if (!usedConstantNames.has(description)) {
6363
usedConstantNames.set(description, true);
6464
constantActions.push({
@@ -521,37 +521,44 @@ namespace ts.refactor.extractSymbol {
521521
return extractConstantInScope(expression, scopes[requestedChangesIndex], usagesPerScope[requestedChangesIndex], targetRange.facts, context);
522522
}
523523

524-
interface PossibleExtraction {
525-
readonly functionDescription: string;
526-
readonly functionScopeDescription: string;
527-
readonly functionErrors: ReadonlyArray<Diagnostic>;
528-
readonly constantDescription: string;
529-
readonly constantScopeDescription: string;
530-
readonly constantErrors: ReadonlyArray<Diagnostic>;
524+
interface Extraction {
525+
readonly description: string;
526+
readonly scopeDescription: string;
527+
readonly errors: ReadonlyArray<Diagnostic>;
528+
}
529+
530+
interface ScopeExtractions {
531+
readonly functionExtraction: Extraction;
532+
readonly constantExtraction: Extraction;
531533
}
534+
532535
/**
533536
* Given a piece of text to extract ('targetRange'), computes a list of possible extractions.
534537
* Each returned ExtractResultForScope corresponds to a possible target scope and is either a set of changes
535538
* or an error explaining why we can't extract into that scope.
536539
*/
537-
function getPossibleExtractions(targetRange: TargetRange, context: RefactorContext): ReadonlyArray<PossibleExtraction> | undefined {
540+
function getPossibleExtractions(targetRange: TargetRange, context: RefactorContext): ReadonlyArray<ScopeExtractions> | undefined {
538541
const { scopes, readsAndWrites: { functionErrorsPerScope, constantErrorsPerScope } } = getPossibleExtractionsWorker(targetRange, context);
539542
// Need the inner type annotation to avoid https://github.com/Microsoft/TypeScript/issues/7547
540-
const extractions = scopes.map((scope, i): PossibleExtraction => {
543+
const extractions = scopes.map((scope, i): ScopeExtractions => {
541544
const scopeDescription = isFunctionLikeDeclaration(scope)
542545
? getDescriptionForFunctionLikeDeclaration(scope)
543546
: isClassLike(scope)
544547
? getDescriptionForClassLikeDeclaration(scope)
545548
: getDescriptionForModuleLikeDeclaration(scope);
546549
return {
547-
functionDescription: getDescriptionForFunctionInScope(scope),
548-
functionErrors: functionErrorsPerScope[i],
549-
functionScopeDescription: scopeDescription,
550-
constantDescription: getDescriptionForConstantInScope(scope),
551-
constantErrors: constantErrorsPerScope[i],
552-
constantScopeDescription: (i === 0 && !isClassLike(scope))
553-
? "enclosing scope" // Like "global scope" and "module scope", this is not localized.
554-
: scopeDescription,
550+
functionExtraction: {
551+
description: getDescriptionForFunctionInScope(scope),
552+
errors: functionErrorsPerScope[i],
553+
scopeDescription,
554+
},
555+
constantExtraction: {
556+
description: getDescriptionForConstantInScope(scope),
557+
errors: constantErrorsPerScope[i],
558+
scopeDescription: (i === 0 && !isClassLike(scope))
559+
? "enclosing scope" // Like "global scope" and "module scope", this is not localized.
560+
: scopeDescription,
561+
},
555562
};
556563
});
557564
return extractions;

0 commit comments

Comments
 (0)