Skip to content

Commit

Permalink
fix(46589): omit ? in method signature completion for optional methods
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Nov 3, 2021
1 parent 9b1ba8f commit ddd9d3d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ namespace ts.codefix {

type AddNode = PropertyDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction;

export const enum PreserveOptionalFlags {
Method = 1 << 0,
Propery = 1 << 1,
All = Method | Propery
}

/**
* `addClassElement` will not be called if we can't figure out a representation for `symbol` in `enclosingDeclaration`.
* @param body If defined, this will be the body of the member node passed to `addClassElement`. Otherwise, the body will default to a stub.
Expand All @@ -50,6 +56,7 @@ namespace ts.codefix {
importAdder: ImportAdder | undefined,
addClassElement: (node: AddNode) => void,
body: Block | undefined,
preserveOptional = PreserveOptionalFlags.All,
isAmbient = false,
): void {
const declarations = symbol.getDeclarations();
Expand Down Expand Up @@ -83,7 +90,7 @@ namespace ts.codefix {
/*decorators*/ undefined,
modifiers,
name,
optional ? factory.createToken(SyntaxKind.QuestionToken) : undefined,
optional && (preserveOptional & PreserveOptionalFlags.Propery) ? factory.createToken(SyntaxKind.QuestionToken) : undefined,
typeNode,
/*initializer*/ undefined));
break;
Expand Down Expand Up @@ -158,14 +165,14 @@ namespace ts.codefix {
}
else {
Debug.assert(declarations.length === signatures.length, "Declarations and signatures should match count");
addClassElement(createMethodImplementingSignatures(checker, context, enclosingDeclaration, signatures, name, optional, modifiers, quotePreference, body));
addClassElement(createMethodImplementingSignatures(checker, context, enclosingDeclaration, signatures, name, optional && !!(preserveOptional & PreserveOptionalFlags.Method), modifiers, quotePreference, body));
}
}
break;
}

function outputMethod(quotePreference: QuotePreference, signature: Signature, modifiers: NodeArray<Modifier> | undefined, name: PropertyName, body?: Block): void {
const method = createSignatureDeclarationFromSignature(SyntaxKind.MethodDeclaration, context, quotePreference, signature, body, name, modifiers, optional, enclosingDeclaration, importAdder);
const method = createSignatureDeclarationFromSignature(SyntaxKind.MethodDeclaration, context, quotePreference, signature, body, name, modifiers, optional && !!(preserveOptional & PreserveOptionalFlags.Method), enclosingDeclaration, importAdder);
if (method) addClassElement(method);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ namespace ts.Completions {
node => {
let requiredModifiers = ModifierFlags.None;
if (isAbstract) {
requiredModifiers |= ModifierFlags.Abstract;
requiredModifiers |= ModifierFlags.Abstract;
}
if (isClassElement(node)
&& checker.getMemberOverrideModifierStatus(classLikeDeclaration, node) === MemberOverrideStatus.NeedsOverride) {
Expand All @@ -912,6 +912,7 @@ namespace ts.Completions {
completionNodes.push(node);
},
body,
codefix.PreserveOptionalFlags.Propery,
isAbstract);

if (completionNodes.length) {
Expand Down Expand Up @@ -3882,5 +3883,6 @@ namespace ts.Completions {
}
return charCode;
}

}

44 changes: 44 additions & 0 deletions tests/cases/fourslash/completionsOverridingMethod9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// <reference path="fourslash.ts" />

// @Filename: a.ts
// @newline: LF

////interface IFoo {
//// a?: number;
//// b?(x: number): void;
////}
////class Foo implements IFoo {
//// /**/
////}

verify.completions({
marker: "",
isNewIdentifierLocation: true,
preferences: {
includeCompletionsWithInsertText: true,
includeCompletionsWithSnippetText: false,
includeCompletionsWithClassMemberSnippets: true,
},
includes: [
{
name: "a",
sortText: completion.SortText.LocationPriority,
replacementSpan: {
fileName: "",
pos: 0,
end: 0,
},
insertText: "a?: number;\n"
},
{
name: "b",
sortText: completion.SortText.LocationPriority,
replacementSpan: {
fileName: "",
pos: 0,
end: 0,
},
insertText: "b(x: number): void {\n}\n"
},
],
});

0 comments on commit ddd9d3d

Please sign in to comment.