diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 12b55aa818..9698f0cf4f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21106,7 +21106,15 @@ namespace ts { } if (isPartOfTypeNode(node)) { - return getTypeFromTypeNode(node); + let typeFromTypeNode = getTypeFromTypeNode(node); + + if (typeFromTypeNode && isExpressionWithTypeArgumentsInClassImplementsClause(node)) { + const containingClass = getContainingClass(node); + const classType = getTypeOfNode(containingClass) as InterfaceType; + typeFromTypeNode = getTypeWithThisArgument(typeFromTypeNode, classType.thisType); + } + + return typeFromTypeNode; } if (isPartOfExpression(node)) { @@ -21116,7 +21124,10 @@ namespace ts { if (isExpressionWithTypeArgumentsInClassExtendsClause(node)) { // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the // extends clause of a class. We handle that case here. - return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; + const classNode = getContainingClass(node); + const classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classNode)) as InterfaceType; classType; + const baseType = getBaseTypes(classType)[0]; baseType; + return baseType && getTypeWithThisArgument(baseType, classType.thisType); } if (isTypeDeclaration(node)) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 66bf693d83..9eb7640503 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3126,6 +3126,14 @@ namespace ts { return tryGetClassExtendingExpressionWithTypeArguments(node) !== undefined; } + export function isExpressionWithTypeArgumentsInClassImplementsClause(node: Node): boolean { + return node.kind === SyntaxKind.ExpressionWithTypeArguments + && node.parent + && (node.parent).token === SyntaxKind.ImplementsKeyword + && node.parent.parent + && isClassLike(node.parent.parent); + } + export function isEntityNameExpression(node: Expression): node is EntityNameExpression { return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isEntityNameExpression((node).expression); diff --git a/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts b/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts index 22546e55ec..16edce0a51 100644 --- a/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts +++ b/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts @@ -22,8 +22,8 @@ namespace ts.codefix { const classDecl = token.parent as ClassLikeDeclaration; const startPos = classDecl.members.pos; - const classType = checker.getTypeAtLocation(classDecl) as InterfaceType; - const instantiatedExtendsType = checker.getBaseTypes(classType)[0]; + const extendsNode = getClassExtendsHeritageClauseElement(classDecl); + const instantiatedExtendsType = checker.getTypeAtLocation(extendsNode); // Note that this is ultimately derived from a map indexed by symbol names, // so duplicates cannot occur. diff --git a/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts b/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts index 42488dc2ae..856a57c46b 100644 --- a/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts +++ b/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts @@ -17,7 +17,7 @@ namespace ts.codefix { } const startPos: number = classDecl.members.pos; - const classType = checker.getTypeAtLocation(classDecl); + const classType = checker.getTypeAtLocation(classDecl) as InterfaceType; const implementedTypeNodes = getClassImplementsHeritageClauseElements(classDecl); const hasNumericIndexSignature = !!checker.getIndexTypeOfType(classType, IndexKind.Number); @@ -25,7 +25,7 @@ namespace ts.codefix { const result: CodeAction[] = []; for (const implementedTypeNode of implementedTypeNodes) { - const implementedType = checker.getTypeFromTypeNode(implementedTypeNode) as InterfaceType; + const implementedType = checker.getTypeAtLocation(implementedTypeNode) as InterfaceType; // Note that this is ultimately derived from a map indexed by symbol names, // so duplicates cannot occur. const implementedTypeSymbols = checker.getPropertiesOfType(implementedType); diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 45479e4947..d20fc0129c 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -32,22 +32,14 @@ namespace ts.codefix { const name = declaration.name ? declaration.name.getText() : undefined; const visibility = getVisibilityPrefixWithSpace(getModifierFlags(declaration)); - const typeAtNewDeclaration = checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration); + const type = checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration); switch (declaration.kind) { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: case SyntaxKind.PropertySignature: case SyntaxKind.PropertyDeclaration: - let typeString: string | undefined = undefined; - const typeAtOldDeclaration = checker.getTypeAtLocation(declaration); - if ((typeAtOldDeclaration as TypeParameter).isThisType) { - typeString = "this"; - } - else { - typeString = checker.typeToString(typeAtNewDeclaration, enclosingDeclaration, TypeFormatFlags.None); - } - + const typeString = checker.typeToString(type, enclosingDeclaration, TypeFormatFlags.None); return `${visibility}${name}: ${typeString};${newlineChar}`; case SyntaxKind.MethodSignature: @@ -59,7 +51,7 @@ namespace ts.codefix { // If there is more than one overload but no implementation signature // (eg: an abstract method or interface declaration), there is a 1-1 // correspondence of declarations and signatures. - const signatures = checker.getSignaturesOfType(typeAtNewDeclaration, SignatureKind.Call); + const signatures = checker.getSignaturesOfType(type, SignatureKind.Call); if (!(signatures && signatures.length > 0)) { return ""; } diff --git a/tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts b/tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts index a657dfeb71..344a7ee3f2 100644 --- a/tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts +++ b/tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts @@ -2,6 +2,7 @@ //// abstract class A { //// abstract f(a: number, b: string): boolean; +//// abstract f(a: number, b: string): this; //// abstract f(a: string, b: number): Function; //// abstract f(a: string): Function; //// } @@ -10,6 +11,7 @@ verify.rangeAfterCodeFix(` f(a: number, b: string): boolean; + f(a: number, b: string): this; f(a: string, b: number): Function; f(a: string): Function; f(a: any, b?: any) { diff --git a/tests/cases/fourslash/codeFixClassExtendAbstractMethodThis.ts b/tests/cases/fourslash/codeFixClassExtendAbstractMethodThis.ts new file mode 100644 index 0000000000..55b3ad4b77 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassExtendAbstractMethodThis.ts @@ -0,0 +1,13 @@ +/// + +//// abstract class A { +//// abstract f(): this; +//// } +//// +//// class C extends A {[| |]} + +verify.rangeAfterCodeFix(` + f(): this { + throw new Error('Method not implemented.'); + } +`); diff --git a/tests/cases/fourslash/codeFixClassExtendAbstractPropertyThis.ts b/tests/cases/fourslash/codeFixClassExtendAbstractPropertyThis.ts new file mode 100644 index 0000000000..de128ca1b7 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassExtendAbstractPropertyThis.ts @@ -0,0 +1,11 @@ +/// + +//// abstract class A { +//// abstract x: this; +//// } +//// +//// class C extends A {[| |]} + +verify.rangeAfterCodeFix(` + x: this; +`); diff --git a/tests/cases/fourslash/codeFixClassImplementInterfaceMethodWithParams.ts b/tests/cases/fourslash/codeFixClassImplementInterfaceMethodThisAndSelfReference.ts similarity index 71% rename from tests/cases/fourslash/codeFixClassImplementInterfaceMethodWithParams.ts rename to tests/cases/fourslash/codeFixClassImplementInterfaceMethodThisAndSelfReference.ts index 0ee842975e..95cc3476bf 100644 --- a/tests/cases/fourslash/codeFixClassImplementInterfaceMethodWithParams.ts +++ b/tests/cases/fourslash/codeFixClassImplementInterfaceMethodThisAndSelfReference.ts @@ -1,14 +1,14 @@ /// //// interface I { -//// f(x: number, y: string): I +//// f(x: number, y: this): I //// } //// //// class C implements I {[| //// |]} verify.rangeAfterCodeFix(` -f(x: number,y: string): I { +f(x: number,y: this): I { throw new Error('Method not implemented.'); } `);