Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(46589): Extra ? added to method in method signature completion for optional method #46594

Merged
merged 1 commit into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/services/codefixes/helpers.ts
Expand Up @@ -37,6 +37,12 @@ namespace ts.codefix {

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

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

/**
* `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.Property) ? 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
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.Property,
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
@@ -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"
},
],
});