Skip to content

Commit c2d25d3

Browse files
committed
fix(46589): omit ? in method signature completion for optional methods
1 parent 9b1ba8f commit c2d25d3

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/services/codefixes/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace ts.codefix {
5050
importAdder: ImportAdder | undefined,
5151
addClassElement: (node: AddNode) => void,
5252
body: Block | undefined,
53+
preserveOptional = true,
5354
isAmbient = false,
5455
): void {
5556
const declarations = symbol.getDeclarations();
@@ -63,7 +64,7 @@ namespace ts.codefix {
6364
const visibilityModifier = createVisibilityModifier(getEffectiveModifierFlags(declaration));
6465
const modifiers = visibilityModifier ? factory.createNodeArray([visibilityModifier]) : undefined;
6566
const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration));
66-
const optional = !!(symbol.flags & SymbolFlags.Optional);
67+
const optional = preserveOptional && !!(symbol.flags & SymbolFlags.Optional);
6768
const ambient = !!(enclosingDeclaration.flags & NodeFlags.Ambient) || isAmbient;
6869
const quotePreference = getQuotePreference(sourceFile, preferences);
6970

src/services/completions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ namespace ts.Completions {
888888
node => {
889889
let requiredModifiers = ModifierFlags.None;
890890
if (isAbstract) {
891-
requiredModifiers |= ModifierFlags.Abstract;
891+
requiredModifiers |= ModifierFlags.Abstract;
892892
}
893893
if (isClassElement(node)
894894
&& checker.getMemberOverrideModifierStatus(classLikeDeclaration, node) === MemberOverrideStatus.NeedsOverride) {
@@ -912,6 +912,7 @@ namespace ts.Completions {
912912
completionNodes.push(node);
913913
},
914914
body,
915+
/*preserveOptional*/ false,
915916
isAbstract);
916917

917918
if (completionNodes.length) {
@@ -3882,5 +3883,6 @@ namespace ts.Completions {
38823883
}
38833884
return charCode;
38843885
}
3886+
38853887
}
38863888

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: a.ts
4+
// @newline: LF
5+
6+
////interface IFoo {
7+
//// a?: number;
8+
//// b?(x: number): void;
9+
////}
10+
////class Foo implements IFoo {
11+
//// /**/
12+
////}
13+
14+
verify.completions({
15+
marker: "",
16+
isNewIdentifierLocation: true,
17+
preferences: {
18+
includeCompletionsWithInsertText: true,
19+
includeCompletionsWithSnippetText: false,
20+
includeCompletionsWithClassMemberSnippets: true,
21+
},
22+
includes: [
23+
{
24+
name: "a",
25+
sortText: completion.SortText.LocationPriority,
26+
replacementSpan: {
27+
fileName: "",
28+
pos: 0,
29+
end: 0,
30+
},
31+
insertText: "a: number;\n"
32+
},
33+
{
34+
name: "b",
35+
sortText: completion.SortText.LocationPriority,
36+
replacementSpan: {
37+
fileName: "",
38+
pos: 0,
39+
end: 0,
40+
},
41+
insertText: "b(x: number): void {\n}\n"
42+
},
43+
],
44+
});

0 commit comments

Comments
 (0)