3
3
namespace ts . refactor {
4
4
const convertFunctionToES6Class : Refactor = {
5
5
name : "Convert to ES2015 class" ,
6
- description : "Convert a pre-ES2015 function into an ES2015 class" ,
6
+ description : Diagnostics . Convert_function_to_an_ES2015_class . message ,
7
7
getCodeActions,
8
- isApplicableForPositionOrRange
8
+ isApplicable
9
9
} ;
10
10
11
11
registerRefactor ( convertFunctionToES6Class ) ;
12
12
13
- function isApplicableForPositionOrRange ( context : QueryRefactorContext , positionOrRange : number | TextRange ) : boolean {
14
- const start = typeof positionOrRange === "number" ? positionOrRange : positionOrRange . pos ;
13
+ function isApplicable ( context : RefactorContext ) : boolean {
14
+ const start = context . startPosition ;
15
15
const node = getTokenAtPosition ( context . file , start ) ;
16
16
const checker = context . program . getTypeChecker ( ) ;
17
- const symbol = checker . getSymbolAtLocation ( node ) ;
18
- if ( isClassLikeSymbol ( symbol ) ) {
19
- return true ;
20
- }
21
-
22
- function isClassLikeSymbol ( symbol : Symbol ) {
23
- if ( ! symbol || ! symbol . valueDeclaration ) {
24
- return false ;
25
- }
17
+ let symbol = checker . getSymbolAtLocation ( node ) ;
26
18
27
- let targetSymbol : Symbol ;
28
- if ( symbol . valueDeclaration . kind === SyntaxKind . FunctionDeclaration ) {
29
- targetSymbol = symbol ;
30
- }
31
- else if ( isDeclarationOfFunctionOrClassExpression ( symbol ) ) {
32
- targetSymbol = ( symbol . valueDeclaration as VariableDeclaration ) . initializer . symbol ;
33
- }
34
-
35
- // if there is a prototype property assignment like:
36
- // foo.prototype.method = function () { }
37
- // then the symbol for "foo" will have a member
38
- return targetSymbol && targetSymbol . members && targetSymbol . members . size > 0 ;
19
+ if ( symbol && isDeclarationOfFunctionOrClassExpression ( symbol ) ) {
20
+ symbol = ( symbol . valueDeclaration as VariableDeclaration ) . initializer . symbol ;
39
21
}
22
+
23
+ return symbol && symbol . flags & SymbolFlags . Function && symbol . members && symbol . members . size > 0 ;
40
24
}
41
25
42
- function getCodeActions ( context : RefactorContext , positionOrRange : number | TextRange ) : CodeAction [ ] | undefined {
43
- const start = typeof positionOrRange === "number" ? positionOrRange : positionOrRange . pos ;
26
+ function getCodeActions ( context : RefactorContext ) : CodeAction [ ] | undefined {
27
+ const start = context . startPosition ;
44
28
const sourceFile = context . file ;
45
29
const checker = context . program . getTypeChecker ( ) ;
46
30
const token = getTokenAtPosition ( sourceFile , start ) ;
47
31
const ctorSymbol = checker . getSymbolAtLocation ( token ) ;
32
+ const newLine = context . rulesProvider . getFormatOptions ( ) . newLineCharacter ;
48
33
49
34
const deletedNodes : Node [ ] = [ ] ;
50
35
const deletes : ( ( ) => any ) [ ] = [ ] ;
@@ -54,7 +39,7 @@ namespace ts.refactor {
54
39
}
55
40
56
41
const ctorDeclaration = ctorSymbol . valueDeclaration ;
57
- const changeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context ) ;
42
+ const changeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context as { newLineCharacter : string , rulesProvider : formatting . RulesProvider } ) ;
58
43
59
44
let precedingNode : Node ;
60
45
let newClassDeclaration : ClassDeclaration ;
@@ -82,13 +67,13 @@ namespace ts.refactor {
82
67
}
83
68
84
69
// Because the preceding node could be touched, we need to insert nodes before delete nodes.
85
- changeTracker . insertNodeAfter ( sourceFile , precedingNode , newClassDeclaration , { suffix : "\n" } ) ;
70
+ changeTracker . insertNodeAfter ( sourceFile , precedingNode , newClassDeclaration , { suffix : newLine } ) ;
86
71
for ( const deleteCallback of deletes ) {
87
72
deleteCallback ( ) ;
88
73
}
89
74
90
75
return [ {
91
- description : `Convert function ${ ctorSymbol . name } to ES6 class` ,
76
+ description : formatStringFromArgs ( Diagnostics . Convert_function_0_to_class . message , [ ctorSymbol . name ] ) ,
92
77
changes : changeTracker . getChanges ( )
93
78
} ] ;
94
79
0 commit comments