@@ -23,7 +23,7 @@ namespace ts.codefix {
23
23
const { parentDeclaration, declSourceFile, inJs, makeStatic, token, call } = info ;
24
24
const methodCodeAction = call && getActionForMethodDeclaration ( context , declSourceFile , parentDeclaration , token , call , makeStatic , inJs , context . preferences ) ;
25
25
const addMember = inJs && ! isInterfaceDeclaration ( parentDeclaration ) ?
26
- singleElementArray ( getActionsForAddMissingMemberInJavascriptFile ( context , declSourceFile , parentDeclaration , token . text , makeStatic ) ) :
26
+ singleElementArray ( getActionsForAddMissingMemberInJavascriptFile ( context , declSourceFile , parentDeclaration , token , makeStatic ) ) :
27
27
getActionsForAddMissingMemberInTypeScriptFile ( context , declSourceFile , parentDeclaration , token , makeStatic ) ;
28
28
return concatenate ( singleElementArray ( methodCodeAction ) , addMember ) ;
29
29
} ,
@@ -70,7 +70,7 @@ namespace ts.codefix {
70
70
}
71
71
else {
72
72
if ( inJs && ! isInterfaceDeclaration ( parentDeclaration ) ) {
73
- addMissingMemberInJs ( changes , declSourceFile , parentDeclaration , token . text , makeStatic ) ;
73
+ addMissingMemberInJs ( changes , declSourceFile , parentDeclaration , token , makeStatic ) ;
74
74
}
75
75
else {
76
76
const typeNode = getTypeNode ( program . getTypeChecker ( ) , parentDeclaration , token ) ;
@@ -140,7 +140,7 @@ namespace ts.codefix {
140
140
if ( classOrInterface && ! program . isSourceFileFromExternalLibrary ( classOrInterface . getSourceFile ( ) ) ) {
141
141
const makeStatic = ( ( leftExpressionType as TypeReference ) . target || leftExpressionType ) !== checker . getDeclaredTypeOfSymbol ( symbol ) ;
142
142
// Static private identifier properties are not supported yet.
143
- if ( makeStatic && isPrivateIdentifier ( token ) ) { return undefined ; }
143
+ if ( makeStatic && isPrivateIdentifier ( token ) ) return undefined ;
144
144
const declSourceFile = classOrInterface . getSourceFile ( ) ;
145
145
const inJs = isSourceFileJS ( declSourceFile ) ;
146
146
const call = tryCast ( parent . parent , isCallExpression ) ;
@@ -153,13 +153,20 @@ namespace ts.codefix {
153
153
return undefined ;
154
154
}
155
155
156
- function getActionsForAddMissingMemberInJavascriptFile ( context : CodeFixContext , declSourceFile : SourceFile , classDeclaration : ClassLikeDeclaration , tokenName : string , makeStatic : boolean ) : CodeFixAction | undefined {
157
- const changes = textChanges . ChangeTracker . with ( context , t => addMissingMemberInJs ( t , declSourceFile , classDeclaration , tokenName , makeStatic ) ) ;
158
- return changes . length === 0 ? undefined
159
- : createCodeFixAction ( fixName , changes , [ makeStatic ? Diagnostics . Initialize_static_property_0 : Diagnostics . Initialize_property_0_in_the_constructor , tokenName ] , fixId , Diagnostics . Add_all_missing_members ) ;
156
+ function getActionsForAddMissingMemberInJavascriptFile ( context : CodeFixContext , declSourceFile : SourceFile , classDeclaration : ClassLikeDeclaration , token : Identifier | PrivateIdentifier , makeStatic : boolean ) : CodeFixAction | undefined {
157
+ const changes = textChanges . ChangeTracker . with ( context , t => addMissingMemberInJs ( t , declSourceFile , classDeclaration , token , makeStatic ) ) ;
158
+ if ( changes . length === 0 ) {
159
+ return undefined ;
160
+ }
161
+
162
+ const diagnostic = makeStatic ? Diagnostics . Initialize_static_property_0 :
163
+ isPrivateIdentifier ( token ) ? Diagnostics . Declare_a_private_field_named_0 : Diagnostics . Initialize_property_0_in_the_constructor ;
164
+
165
+ return createCodeFixAction ( fixName , changes , [ diagnostic , token . text ] , fixId , Diagnostics . Add_all_missing_members ) ;
160
166
}
161
167
162
- function addMissingMemberInJs ( changeTracker : textChanges . ChangeTracker , declSourceFile : SourceFile , classDeclaration : ClassLikeDeclaration , tokenName : string , makeStatic : boolean ) : void {
168
+ function addMissingMemberInJs ( changeTracker : textChanges . ChangeTracker , declSourceFile : SourceFile , classDeclaration : ClassLikeDeclaration , token : Identifier | PrivateIdentifier , makeStatic : boolean ) : void {
169
+ const tokenName = token . text ;
163
170
if ( makeStatic ) {
164
171
if ( classDeclaration . kind === SyntaxKind . ClassExpression ) {
165
172
return ;
@@ -168,6 +175,23 @@ namespace ts.codefix {
168
175
const staticInitialization = initializePropertyToUndefined ( createIdentifier ( className ) , tokenName ) ;
169
176
changeTracker . insertNodeAfter ( declSourceFile , classDeclaration , staticInitialization ) ;
170
177
}
178
+ else if ( isPrivateIdentifier ( token ) ) {
179
+ const property = createProperty (
180
+ /*decorators*/ undefined ,
181
+ /*modifiers*/ undefined ,
182
+ tokenName ,
183
+ /*questionToken*/ undefined ,
184
+ /*type*/ undefined ,
185
+ /*initializer*/ undefined ) ;
186
+
187
+ const lastProp = getNodeToInsertPropertyAfter ( classDeclaration ) ;
188
+ if ( lastProp ) {
189
+ changeTracker . insertNodeAfter ( declSourceFile , lastProp , property ) ;
190
+ }
191
+ else {
192
+ changeTracker . insertNodeAtClassStart ( declSourceFile , classDeclaration , property ) ;
193
+ }
194
+ }
171
195
else {
172
196
const classConstructor = getFirstConstructorWithBody ( classDeclaration ) ;
173
197
if ( ! classConstructor ) {
0 commit comments