@@ -43,7 +43,7 @@ namespace ts.refactor.extractSymbol {
43
43
// Don't issue refactorings with duplicated names.
44
44
// Scopes come back in "innermost first" order, so extractions will
45
45
// preferentially go into nearer scopes
46
- const description = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ functionExtraction . description , functionExtraction . scopeDescription ] ) ;
46
+ const description = functionExtraction . description ;
47
47
if ( ! usedFunctionNames . has ( description ) ) {
48
48
usedFunctionNames . set ( description , true ) ;
49
49
functionActions . push ( {
@@ -58,7 +58,7 @@ namespace ts.refactor.extractSymbol {
58
58
// Don't issue refactorings with duplicated names.
59
59
// Scopes come back in "innermost first" order, so extractions will
60
60
// preferentially go into nearer scopes
61
- const description = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ constantExtraction . description , constantExtraction . scopeDescription ] ) ;
61
+ const description = constantExtraction . description ;
62
62
if ( ! usedConstantNames . has ( description ) ) {
63
63
usedConstantNames . set ( description , true ) ;
64
64
constantActions . push ( {
@@ -524,7 +524,6 @@ namespace ts.refactor.extractSymbol {
524
524
525
525
interface Extraction {
526
526
readonly description : string ;
527
- readonly scopeDescription : string ;
528
527
readonly errors : ReadonlyArray < Diagnostic > ;
529
528
}
530
529
@@ -542,23 +541,43 @@ namespace ts.refactor.extractSymbol {
542
541
const { scopes, readsAndWrites : { functionErrorsPerScope, constantErrorsPerScope } } = getPossibleExtractionsWorker ( targetRange , context ) ;
543
542
// Need the inner type annotation to avoid https://github.com/Microsoft/TypeScript/issues/7547
544
543
const extractions = scopes . map ( ( scope , i ) : ScopeExtractions => {
544
+ const functionDescriptionPart = getDescriptionForFunctionInScope ( scope ) ;
545
+ const constantDescriptionPart = getDescriptionForConstantInScope ( scope ) ;
546
+
545
547
const scopeDescription = isFunctionLikeDeclaration ( scope )
546
548
? getDescriptionForFunctionLikeDeclaration ( scope )
547
549
: isClassLike ( scope )
548
550
? getDescriptionForClassLikeDeclaration ( scope )
549
551
: getDescriptionForModuleLikeDeclaration ( scope ) ;
552
+
553
+ let functionDescription : string ;
554
+ let constantDescription : string ;
555
+ if ( scopeDescription === SpecialScope . Global ) {
556
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ functionDescriptionPart , "global" ] ) ;
557
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ constantDescriptionPart , "global" ] ) ;
558
+ }
559
+ else if ( scopeDescription === SpecialScope . Module ) {
560
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ functionDescriptionPart , "module" ] ) ;
561
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ constantDescriptionPart , "module" ] ) ;
562
+ }
563
+ else {
564
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ functionDescriptionPart , scopeDescription ] ) ;
565
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ constantDescriptionPart , scopeDescription ] ) ;
566
+ }
567
+
568
+ // Customize the phrasing for the innermost scope to increase clarity.
569
+ if ( i === 0 && ! isClassLike ( scope ) ) {
570
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_enclosing_scope ) , [ constantDescriptionPart ] ) ;
571
+ }
572
+
550
573
return {
551
574
functionExtraction : {
552
- description : getDescriptionForFunctionInScope ( scope ) ,
575
+ description : functionDescription ,
553
576
errors : functionErrorsPerScope [ i ] ,
554
- scopeDescription,
555
577
} ,
556
578
constantExtraction : {
557
- description : getDescriptionForConstantInScope ( scope ) ,
579
+ description : constantDescription ,
558
580
errors : constantErrorsPerScope [ i ] ,
559
- scopeDescription : ( i === 0 && ! isClassLike ( scope ) )
560
- ? getLocaleSpecificMessage ( Diagnostics . enclosing_scope )
561
- : scopeDescription ,
562
581
} ,
563
582
} ;
564
583
} ) ;
@@ -627,10 +646,15 @@ namespace ts.refactor.extractSymbol {
627
646
? `class '${ scope . name . text } '`
628
647
: scope . name ? `class expression '${ scope . name . text } '` : "anonymous class expression" ;
629
648
}
630
- function getDescriptionForModuleLikeDeclaration ( scope : SourceFile | ModuleBlock ) : string {
649
+ function getDescriptionForModuleLikeDeclaration ( scope : SourceFile | ModuleBlock ) : string | SpecialScope {
631
650
return scope . kind === SyntaxKind . ModuleBlock
632
651
? `namespace '${ scope . parent . name . getText ( ) } '`
633
- : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . _0_scope ) , [ scope . externalModuleIndicator ? "module" : "global" ] ) ;
652
+ : scope . externalModuleIndicator ? SpecialScope . Module : SpecialScope . Global ;
653
+ }
654
+
655
+ const enum SpecialScope {
656
+ Module ,
657
+ Global ,
634
658
}
635
659
636
660
function getUniqueName ( baseName : string , fileText : string ) : string {
0 commit comments