@@ -37,6 +37,12 @@ namespace ts.codefix {
37
37
38
38
type AddNode = PropertyDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction ;
39
39
40
+ export const enum PreserveOptionalFlags {
41
+ Method = 1 << 0 ,
42
+ Property = 1 << 1 ,
43
+ All = Method | Property
44
+ }
45
+
40
46
/**
41
47
* `addClassElement` will not be called if we can't figure out a representation for `symbol` in `enclosingDeclaration`.
42
48
* @param body If defined, this will be the body of the member node passed to `addClassElement`. Otherwise, the body will default to a stub.
@@ -50,6 +56,7 @@ namespace ts.codefix {
50
56
importAdder : ImportAdder | undefined ,
51
57
addClassElement : ( node : AddNode ) => void ,
52
58
body : Block | undefined ,
59
+ preserveOptional = PreserveOptionalFlags . All ,
53
60
isAmbient = false ,
54
61
) : void {
55
62
const declarations = symbol . getDeclarations ( ) ;
@@ -83,7 +90,7 @@ namespace ts.codefix {
83
90
/*decorators*/ undefined ,
84
91
modifiers ,
85
92
name ,
86
- optional ? factory . createToken ( SyntaxKind . QuestionToken ) : undefined ,
93
+ optional && ( preserveOptional & PreserveOptionalFlags . Property ) ? factory . createToken ( SyntaxKind . QuestionToken ) : undefined ,
87
94
typeNode ,
88
95
/*initializer*/ undefined ) ) ;
89
96
break ;
@@ -158,14 +165,14 @@ namespace ts.codefix {
158
165
}
159
166
else {
160
167
Debug . assert ( declarations . length === signatures . length , "Declarations and signatures should match count" ) ;
161
- addClassElement ( createMethodImplementingSignatures ( checker , context , enclosingDeclaration , signatures , name , optional , modifiers , quotePreference , body ) ) ;
168
+ addClassElement ( createMethodImplementingSignatures ( checker , context , enclosingDeclaration , signatures , name , optional && ! ! ( preserveOptional & PreserveOptionalFlags . Method ) , modifiers , quotePreference , body ) ) ;
162
169
}
163
170
}
164
171
break ;
165
172
}
166
173
167
174
function outputMethod ( quotePreference : QuotePreference , signature : Signature , modifiers : NodeArray < Modifier > | undefined , name : PropertyName , body ?: Block ) : void {
168
- const method = createSignatureDeclarationFromSignature ( SyntaxKind . MethodDeclaration , context , quotePreference , signature , body , name , modifiers , optional , enclosingDeclaration , importAdder ) ;
175
+ const method = createSignatureDeclarationFromSignature ( SyntaxKind . MethodDeclaration , context , quotePreference , signature , body , name , modifiers , optional && ! ! ( preserveOptional & PreserveOptionalFlags . Method ) , enclosingDeclaration , importAdder ) ;
169
176
if ( method ) addClassElement ( method ) ;
170
177
}
171
178
}
0 commit comments