diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 4b921c1a81c..d0a005f8c8a 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -114,18 +114,29 @@ namespace ts.codefix { newSignatureDeclaration.parameters = createNodeArray(); for (let i = 0; i < maxArgs - 1; i++) { - const newParameter = createParameterDeclaration(i, minArgumentCount, newSignatureDeclaration); + const newParameter = createParameterDeclaration(i, minArgumentCount, anyTypeNode, newSignatureDeclaration); newSignatureDeclaration.parameters.push(newParameter); } - const lastParameter = createParameterDeclaration(maxArgs - 1, minArgumentCount, newSignatureDeclaration); + let lastParameter: ParameterDeclaration; if (hasRestParameter) { - lastParameter.dotDotDotToken = createToken(SyntaxKind.DotDotDotToken); + + const anyArrayTypeNode = createNode(SyntaxKind.ArrayType) as ArrayTypeNode; + anyArrayTypeNode.elementType = anyTypeNode; if (!allMaxArgsAreRest) { - const newParameter = createParameterDeclaration(maxArgs - 1, minArgumentCount, newSignatureDeclaration); + const newParameter = createParameterDeclaration(maxArgs - 1, minArgumentCount, anyTypeNode, newSignatureDeclaration); newSignatureDeclaration.parameters.push(newParameter); + lastParameter = createParameterDeclaration(maxArgs, minArgumentCount, anyArrayTypeNode, newSignatureDeclaration); } + else { + lastParameter = createParameterDeclaration(maxArgs - 1, minArgumentCount, anyArrayTypeNode, newSignatureDeclaration); + } + + lastParameter.dotDotDotToken = createToken(SyntaxKind.DotDotDotToken); + } + else { + lastParameter = createParameterDeclaration(maxArgs - 1, minArgumentCount, anyTypeNode, newSignatureDeclaration); } newSignatureDeclaration.parameters.push(lastParameter); @@ -135,12 +146,12 @@ namespace ts.codefix { return checker.getSignatureFromDeclaration(newSignatureDeclaration); - function createParameterDeclaration(index: number, minArgCount: number, enclosingSignatureDeclaration: SignatureDeclaration): ParameterDeclaration { + function createParameterDeclaration(index: number, minArgCount: number, typeNode: TypeNode, enclosingSignatureDeclaration: SignatureDeclaration): ParameterDeclaration { const newParameter = createNode(SyntaxKind.Parameter) as ParameterDeclaration; newParameter.symbol = checker.createSymbol(SymbolFlags.FunctionScopedVariable, "arg" + index); newParameter.symbol.valueDeclaration = newParameter; newParameter.symbol.declarations = [newParameter]; - newParameter.type = anyTypeNode; + newParameter.type = typeNode; newParameter.parent = enclosingSignatureDeclaration; if (index >= minArgCount) { newParameter.questionToken = optionalToken; diff --git a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethod.ts b/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethod.ts deleted file mode 100644 index b5ba2e20697..00000000000 --- a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethod.ts +++ /dev/null @@ -1,13 +0,0 @@ -/// - -//// interface I { -//// f1(); -//// } -//// -//// class C implements I {[| -//// |]} - -verify.rangeAfterCodeFix(`f1(){ - throw new Error('Method not implemented.'); -} -`); diff --git a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithParams.ts b/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithParams.ts index f0b1e669b46..0ee842975e0 100644 --- a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithParams.ts +++ b/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithParams.ts @@ -1,14 +1,14 @@ /// //// interface I { -//// f(x: number, y: string) +//// f(x: number, y: string): I //// } //// //// class C implements I {[| //// |]} verify.rangeAfterCodeFix(` -f(x: number,y: string){ +f(x: number,y: string): I { throw new Error('Method not implemented.'); } `); diff --git a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithReturnType.ts b/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithReturnType.ts deleted file mode 100644 index c85f1d0043a..00000000000 --- a/tests/cases/fourslash/codeFixUnImplementedInterfaceMissingMethodWithReturnType.ts +++ /dev/null @@ -1,13 +0,0 @@ -/// - -//// interface I { -//// f1(): string; -//// } -//// -//// class C implements I {[| -//// |]} - -verify.rangeAfterCodeFix(`f1(): string { - throw new Error('Method not implemented.'); -} -`); diff --git a/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest1.ts b/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest1.ts new file mode 100644 index 00000000000..a473b9dccbe --- /dev/null +++ b/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest1.ts @@ -0,0 +1,18 @@ +/// + +//// interface I { +//// method(a: number, ...b: string[]): boolean; +//// method(a: string, ...b: number[]): Function; +//// method(a: string): Function; +//// } +//// +//// class C implements I {[| |]} + +verify.rangeAfterCodeFix(` + method(a: number, ...b: string[]): boolean; + method(a: string, ...b: number[]): Function; + method(a: string): Function; + method(arg0: any, ...arg1?: any[]) { + throw new Error('Method not implemented.'); + } +`); diff --git a/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest2.ts b/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest2.ts new file mode 100644 index 00000000000..7cacbda171b --- /dev/null +++ b/tests/cases/fourslash/codeFixUnImplementedInterfaceMultipleSignaturesRest2.ts @@ -0,0 +1,18 @@ +/// + +//// interface I { +//// method(a: number, ...b: string[]): boolean; +//// method(a: string, b: number): Function; +//// method(a: string): Function; +//// } +//// +//// class C implements I {[| |]} + +verify.rangeAfterCodeFix(` + method(a: number, ...b: string[]): boolean; + method(a: string, b: number): Function; + method(a: string): Function; + method(arg0: any, arg1?: any, ...arg2?: any[]) { + throw new Error('Method not implemented.'); + } +`);