5
5
namespace ts . refactor . extractSymbol {
6
6
const extractSymbol : Refactor = {
7
7
name : "Extract Symbol" ,
8
- description : Diagnostics . Extract_symbol . message ,
8
+ description : getLocaleSpecificMessage ( Diagnostics . Extract_symbol ) ,
9
9
getAvailableActions,
10
10
getEditsForAction,
11
11
} ;
@@ -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 ( Diagnostics . Extract_to_0_in_1 . message , [ 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 ( Diagnostics . Extract_to_0_in_1 . message , [ 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 ( {
@@ -78,15 +78,15 @@ namespace ts.refactor.extractSymbol {
78
78
if ( functionActions . length ) {
79
79
infos . push ( {
80
80
name : extractSymbol . name ,
81
- description : Diagnostics . Extract_function . message ,
81
+ description : getLocaleSpecificMessage ( Diagnostics . Extract_function ) ,
82
82
actions : functionActions
83
83
} ) ;
84
84
}
85
85
86
86
if ( constantActions . length ) {
87
87
infos . push ( {
88
88
name : extractSymbol . name ,
89
- description : Diagnostics . Extract_constant . message ,
89
+ description : getLocaleSpecificMessage ( Diagnostics . Extract_constant ) ,
90
90
actions : constantActions
91
91
} ) ;
92
92
}
@@ -525,7 +525,6 @@ namespace ts.refactor.extractSymbol {
525
525
526
526
interface Extraction {
527
527
readonly description : string ;
528
- readonly scopeDescription : string ;
529
528
readonly errors : ReadonlyArray < Diagnostic > ;
530
529
}
531
530
@@ -543,23 +542,43 @@ namespace ts.refactor.extractSymbol {
543
542
const { scopes, readsAndWrites : { functionErrorsPerScope, constantErrorsPerScope } } = getPossibleExtractionsWorker ( targetRange , context ) ;
544
543
// Need the inner type annotation to avoid https://github.com/Microsoft/TypeScript/issues/7547
545
544
const extractions = scopes . map ( ( scope , i ) : ScopeExtractions => {
545
+ const functionDescriptionPart = getDescriptionForFunctionInScope ( scope ) ;
546
+ const constantDescriptionPart = getDescriptionForConstantInScope ( scope ) ;
547
+
546
548
const scopeDescription = isFunctionLikeDeclaration ( scope )
547
549
? getDescriptionForFunctionLikeDeclaration ( scope )
548
550
: isClassLike ( scope )
549
551
? getDescriptionForClassLikeDeclaration ( scope )
550
552
: getDescriptionForModuleLikeDeclaration ( scope ) ;
553
+
554
+ let functionDescription : string ;
555
+ let constantDescription : string ;
556
+ if ( scopeDescription === SpecialScope . Global ) {
557
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ functionDescriptionPart , "global" ] ) ;
558
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ constantDescriptionPart , "global" ] ) ;
559
+ }
560
+ else if ( scopeDescription === SpecialScope . Module ) {
561
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ functionDescriptionPart , "module" ] ) ;
562
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1_scope ) , [ constantDescriptionPart , "module" ] ) ;
563
+ }
564
+ else {
565
+ functionDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ functionDescriptionPart , scopeDescription ] ) ;
566
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_1 ) , [ constantDescriptionPart , scopeDescription ] ) ;
567
+ }
568
+
569
+ // Customize the phrasing for the innermost scope to increase clarity.
570
+ if ( i === 0 && ! isClassLike ( scope ) ) {
571
+ constantDescription = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Extract_to_0_in_enclosing_scope ) , [ constantDescriptionPart ] ) ;
572
+ }
573
+
551
574
return {
552
575
functionExtraction : {
553
- description : getDescriptionForFunctionInScope ( scope ) ,
576
+ description : functionDescription ,
554
577
errors : functionErrorsPerScope [ i ] ,
555
- scopeDescription,
556
578
} ,
557
579
constantExtraction : {
558
- description : getDescriptionForConstantInScope ( scope ) ,
580
+ description : constantDescription ,
559
581
errors : constantErrorsPerScope [ i ] ,
560
- scopeDescription : ( i === 0 && ! isClassLike ( scope ) )
561
- ? "enclosing scope" // Like "global scope" and "module scope", this is not localized.
562
- : scopeDescription ,
563
582
} ,
564
583
} ;
565
584
} ) ;
@@ -628,10 +647,15 @@ namespace ts.refactor.extractSymbol {
628
647
? `class '${ scope . name . text } '`
629
648
: scope . name ? `class expression '${ scope . name . text } '` : "anonymous class expression" ;
630
649
}
631
- function getDescriptionForModuleLikeDeclaration ( scope : SourceFile | ModuleBlock ) : string {
650
+ function getDescriptionForModuleLikeDeclaration ( scope : SourceFile | ModuleBlock ) : string | SpecialScope {
632
651
return scope . kind === SyntaxKind . ModuleBlock
633
652
? `namespace '${ scope . parent . name . getText ( ) } '`
634
- : scope . externalModuleIndicator ? "module scope" : "global scope" ;
653
+ : scope . externalModuleIndicator ? SpecialScope . Module : SpecialScope . Global ;
654
+ }
655
+
656
+ const enum SpecialScope {
657
+ Module ,
658
+ Global ,
635
659
}
636
660
637
661
function getUniqueName ( baseName : string , fileText : string ) : string {
0 commit comments