1
1
/* @internal */
2
- namespace ts . refactor . GenerateGetterAndSetter {
2
+ namespace ts . refactor . generateGetAccessorAndSetAccessor {
3
3
const actionName = "Generate 'get' and 'set' accessors" ;
4
4
const actionDescription = Diagnostics . Generate_get_and_set_accessors . message ;
5
-
6
5
registerRefactor ( actionName , { getEditsForAction, getAvailableActions } ) ;
7
6
7
+ interface Info {
8
+ originalName : string ;
9
+ fieldName : string ;
10
+ accessorName : string ;
11
+ accessorType : TypeNode ;
12
+ propertyDeclaration : PropertyDeclaration ;
13
+ needUpdateName : boolean ;
14
+ hasModifiers : boolean ;
15
+ needUpdateModifiers : boolean ;
16
+ }
17
+
8
18
function getAvailableActions ( context : RefactorContext ) : ApplicableRefactorInfo [ ] | undefined {
9
19
const { file, startPosition } = context ;
10
20
@@ -34,15 +44,17 @@ namespace ts.refactor.GenerateGetterAndSetter {
34
44
const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
35
45
const newLineCharacter = getNewLineOrDefaultFromHost ( context . host , context . formatContext . options ) ;
36
46
37
- const { fieldName, accessorName, propertyDeclaration, needUpdateName, hasModifiers, needUpdateModifiers } = fieldInfo ;
38
- const accessorModifiers = hasModifiers ? createNodeArray ( [ createToken ( SyntaxKind . PublicKeyword ) ] ) : undefined ;
47
+ const { fieldName, accessorName, accessorType, propertyDeclaration, needUpdateName, hasModifiers, needUpdateModifiers } = fieldInfo ;
48
+ const accessorModifiers = hasModifiers ? (
49
+ ( getModifierFlags ( propertyDeclaration ) & ModifierFlags . Private || ! propertyDeclaration . modifiers ) ? createNodeArray ( [ createToken ( SyntaxKind . PublicKeyword ) ] ) : propertyDeclaration . modifiers
50
+ ) : undefined ;
39
51
40
- const getAccessor = generateGetAccessor ( propertyDeclaration , fieldName , accessorName , accessorModifiers ) ;
41
- const setAccessor = generateSetAccessor ( propertyDeclaration , fieldName , accessorName , accessorModifiers ) ;
52
+ const getAccessor = generateGetAccessor ( fieldName , accessorName , accessorType , accessorModifiers ) ;
53
+ const setAccessor = generateSetAccessor ( fieldName , accessorName , accessorType , accessorModifiers ) ;
42
54
43
- const modifiers = needUpdateModifiers ? createNodeArray ( [ createToken ( SyntaxKind . PrivateKeyword ) ] ) : propertyDeclaration . modifiers ;
55
+ const modifiers = hasModifiers ? createNodeArray ( [ createToken ( SyntaxKind . PrivateKeyword ) ] ) : undefined ;
44
56
if ( needUpdateName || needUpdateModifiers ) {
45
- changeTracker . replaceNode ( file , propertyDeclaration , updateOriginPropertyDeclaration ( propertyDeclaration , fieldName , modifiers ) , {
57
+ changeTracker . replaceNode ( file , propertyDeclaration , updateoriginalPropertyDeclaration ( propertyDeclaration , fieldName , modifiers ) , {
46
58
suffix : newLineCharacter
47
59
} ) ;
48
60
}
@@ -57,13 +69,13 @@ namespace ts.refactor.GenerateGetterAndSetter {
57
69
} ;
58
70
}
59
71
60
- interface Info { originName : string ; fieldName : string ; accessorName : string ; propertyDeclaration : PropertyDeclaration ; needUpdateName : boolean ; hasModifiers : boolean ; needUpdateModifiers : boolean ; }
61
72
function getConvertibleFieldAtPosition ( file : SourceFile , startPosition : number ) : Info | undefined {
62
73
const node = getTokenAtPosition ( file , startPosition , /*includeJsDocComment*/ false ) ;
63
74
const propertyDeclaration = findAncestor ( node . parent , isPropertyDeclaration ) ;
64
75
65
- if ( ! ( propertyDeclaration && propertyDeclaration . name . kind === SyntaxKind . Identifier &&
66
- ( getModifierFlags ( propertyDeclaration ) | ModifierFlags . AccessibilityModifier ) === ModifierFlags . AccessibilityModifier ) ) return undefined ;
76
+ if ( ! propertyDeclaration || propertyDeclaration . name . kind !== SyntaxKind . Identifier ) return undefined ;
77
+ // make sure propertyDeclaration have only AccessibilityModifier
78
+ if ( ( getModifierFlags ( propertyDeclaration ) | ModifierFlags . AccessibilityModifier ) !== ModifierFlags . AccessibilityModifier ) return undefined ;
67
79
68
80
const containerClass = getContainingClass ( propertyDeclaration ) ;
69
81
if ( ! containerClass ) return undefined ;
@@ -79,26 +91,28 @@ namespace ts.refactor.GenerateGetterAndSetter {
79
91
if ( find ( members , member => needUpdateName ? member . name . getText ( ) === fieldName : member . name . getText ( ) === accessorName ) ) return undefined ;
80
92
81
93
const hasModifiers = ! ! find ( members , member => ! ! member . modifiers ) ;
82
- const needUpdateModifiers = hasModifiers && ( ! propertyDeclaration . modifiers || hasModifier ( propertyDeclaration , ModifierFlags . Public ) ) ;
94
+ const needUpdateModifiers = hasModifiers && ( ! propertyDeclaration . modifiers || ! hasModifier ( propertyDeclaration , ModifierFlags . Private ) ) ;
95
+ const accessorType = propertyDeclaration . questionToken ? unionTypeNode ( propertyDeclaration . type , createKeywordTypeNode ( SyntaxKind . UndefinedKeyword ) ) : propertyDeclaration . type ;
83
96
84
97
return {
85
- originName : propertyDeclaration . name . text ,
98
+ originalName : propertyDeclaration . name . text ,
86
99
fieldName,
87
100
accessorName,
101
+ accessorType,
88
102
propertyDeclaration,
89
103
needUpdateName,
90
104
hasModifiers,
91
105
needUpdateModifiers
92
106
} ;
93
107
}
94
108
95
- function generateGetAccessor ( propertyDeclaration : PropertyDeclaration , fieldName : string , name : string , modifiers : ModifiersArray ) {
109
+ function generateGetAccessor ( fieldName : string , name : string , type : TypeNode , modifiers : ModifiersArray ) {
96
110
return createGetAccessor (
97
111
/*decorators*/ undefined ,
98
112
modifiers ,
99
113
name ,
100
114
/*parameters*/ undefined ,
101
- propertyDeclaration . type ,
115
+ type ,
102
116
createBlock ( [
103
117
createReturn (
104
118
createPropertyAccess (
@@ -110,7 +124,7 @@ namespace ts.refactor.GenerateGetterAndSetter {
110
124
) ;
111
125
}
112
126
113
- function generateSetAccessor ( propertyDeclaration : PropertyDeclaration , fieldName : string , name : string , modifiers : ModifiersArray ) {
127
+ function generateSetAccessor ( fieldName : string , name : string , type : TypeNode , modifiers : ModifiersArray ) {
114
128
return createSetAccessor (
115
129
/*decorators*/ undefined ,
116
130
modifiers ,
@@ -121,7 +135,7 @@ namespace ts.refactor.GenerateGetterAndSetter {
121
135
/*dotDotDotToken*/ undefined ,
122
136
createIdentifier ( "value" ) ,
123
137
/*questionToken*/ undefined ,
124
- propertyDeclaration . type
138
+ type
125
139
) ] ,
126
140
createBlock ( [
127
141
createStatement (
@@ -137,13 +151,13 @@ namespace ts.refactor.GenerateGetterAndSetter {
137
151
) ;
138
152
}
139
153
140
- function updateOriginPropertyDeclaration ( propertyDeclaration : PropertyDeclaration , fieldName : string , modifiers : ModifiersArray ) {
154
+ function updateoriginalPropertyDeclaration ( propertyDeclaration : PropertyDeclaration , fieldName : string , modifiers : ModifiersArray ) {
141
155
return updateProperty (
142
156
propertyDeclaration ,
143
- /* decorators*/ undefined ,
157
+ propertyDeclaration . decorators ,
144
158
modifiers ,
145
159
fieldName ,
146
- /*questionOrExclamationToken*/ undefined ,
160
+ propertyDeclaration . questionToken || propertyDeclaration . exclamationToken ,
147
161
propertyDeclaration . type ,
148
162
propertyDeclaration . initializer ,
149
163
) ;
0 commit comments