@@ -4,17 +4,14 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
4
4
const actionDescription = Diagnostics . Generate_get_and_set_accessors . message ;
5
5
registerRefactor ( actionName , { getEditsForAction, getAvailableActions } ) ;
6
6
7
- type AcceptedDeclaration = ParameterDeclaration | PropertyDeclaration | PropertyAssignment ;
7
+ type AcceptedDeclaration = ParameterPropertyDeclaration | PropertyDeclaration | PropertyAssignment ;
8
8
type AcceptedNameType = Identifier | StringLiteral ;
9
9
type ContainerDeclaration = ClassLikeDeclaration | ObjectLiteralExpression ;
10
10
11
- interface DeclarationInfo {
11
+ interface Info {
12
12
container : ContainerDeclaration ;
13
13
isStatic : boolean ;
14
14
type : TypeNode | undefined ;
15
- }
16
-
17
- interface Info extends DeclarationInfo {
18
15
declaration : AcceptedDeclaration ;
19
16
fieldName : AcceptedNameType ;
20
17
accessorName : AcceptedNameType ;
@@ -92,62 +89,24 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
92
89
return modifiers && createNodeArray ( modifiers ) ;
93
90
}
94
91
95
- function getPropertyDeclarationInfo ( propertyDeclaration : PropertyDeclaration ) : DeclarationInfo | undefined {
96
- if ( ! isClassLike ( propertyDeclaration . parent ) || ! propertyDeclaration . parent . members ) return undefined ;
97
-
98
- return {
99
- isStatic : hasStaticModifier ( propertyDeclaration ) ,
100
- type : propertyDeclaration . type ,
101
- container : propertyDeclaration . parent
102
- } ;
103
- }
104
-
105
- function getParameterPropertyDeclarationInfo ( parameterDeclaration : ParameterDeclaration ) : DeclarationInfo | undefined {
106
- if ( ! isClassLike ( parameterDeclaration . parent . parent ) || ! parameterDeclaration . parent . parent . members ) return undefined ;
107
-
108
- return {
109
- isStatic : false ,
110
- type : parameterDeclaration . type ,
111
- container : parameterDeclaration . parent . parent
112
- } ;
113
- }
114
-
115
- function getPropertyAssignmentDeclarationInfo ( propertyAssignment : PropertyAssignment ) : DeclarationInfo | undefined {
116
- return {
117
- isStatic : false ,
118
- type : undefined ,
119
- container : propertyAssignment . parent
120
- } ;
121
- }
122
-
123
- function getDeclarationInfo ( declaration : AcceptedDeclaration ) {
124
- if ( isPropertyDeclaration ( declaration ) ) {
125
- return getPropertyDeclarationInfo ( declaration ) ;
126
- }
127
- else if ( isPropertyAssignment ( declaration ) ) {
128
- return getPropertyAssignmentDeclarationInfo ( declaration ) ;
129
- }
130
- else {
131
- return getParameterPropertyDeclarationInfo ( declaration ) ;
132
- }
133
- }
134
-
135
92
function getConvertibleFieldAtPosition ( file : SourceFile , startPosition : number ) : Info | undefined {
136
93
const node = getTokenAtPosition ( file , startPosition , /*includeJsDocComment*/ false ) ;
137
94
const declaration = findAncestor ( node . parent , isAcceptedDeclaration ) ;
138
95
// make sure propertyDeclaration have AccessibilityModifier or Static Modifier
139
96
const meaning = ModifierFlags . AccessibilityModifier | ModifierFlags . Static ;
140
97
if ( ! declaration || ! isConvertableName ( declaration . name ) || ( getModifierFlags ( declaration ) | meaning ) !== meaning ) return undefined ;
141
98
142
- const info = getDeclarationInfo ( declaration ) ;
143
99
const fieldName = createPropertyName ( getUniqueName ( `_${ declaration . name . text } ` , file . text ) , declaration . name ) ;
100
+ const accessorName = createPropertyName ( declaration . name . text , declaration . name ) ;
144
101
suppressLeadingAndTrailingTrivia ( fieldName ) ;
145
102
suppressLeadingAndTrailingTrivia ( declaration ) ;
146
103
return {
147
- ...info ,
104
+ isStatic : hasStaticModifier ( declaration ) ,
105
+ type : getTypeAnnotationNode ( declaration ) ,
106
+ container : declaration . kind === SyntaxKind . Parameter ? declaration . parent . parent : declaration . parent ,
148
107
declaration,
149
108
fieldName,
150
- accessorName : createPropertyName ( declaration . name . text , declaration . name )
109
+ accessorName,
151
110
} ;
152
111
}
153
112
0 commit comments