Skip to content

Commit 33fc26c

Browse files
author
Arthur Ozga
committed
Detect this type for codefix
1 parent 7d82e15 commit 33fc26c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/services/codefixes/helpers.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ namespace ts.codefix {
2323
* @returns Empty string iff there we can't figure out a representation for `symbol` in `enclosingDeclaration`.
2424
*/
2525
function getInsertionForMemberSymbol(symbol: Symbol, enclosingDeclaration: ClassLikeDeclaration, checker: TypeChecker, newlineChar: string): string {
26-
// const name = symbol.getName();
27-
const type = checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration);
2826
const declarations = symbol.getDeclarations();
2927
if (!(declarations && declarations.length)) {
3028
return "";
@@ -34,12 +32,22 @@ namespace ts.codefix {
3432
const name = declaration.name ? declaration.name.getText() : undefined;
3533
const visibility = getVisibilityPrefixWithSpace(getModifierFlags(declaration));
3634

35+
const typeAtNewDeclaration = checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration);
36+
3737
switch (declaration.kind) {
3838
case SyntaxKind.GetAccessor:
3939
case SyntaxKind.SetAccessor:
4040
case SyntaxKind.PropertySignature:
4141
case SyntaxKind.PropertyDeclaration:
42-
const typeString = checker.typeToString(type, enclosingDeclaration, TypeFormatFlags.None);
42+
let typeString: string | undefined = undefined;
43+
const typeAtOldDeclaration = checker.getTypeAtLocation(declaration);
44+
if ((typeAtOldDeclaration as TypeParameter).isThisType) {
45+
typeString = "this";
46+
}
47+
else {
48+
typeString = checker.typeToString(typeAtNewDeclaration, enclosingDeclaration, TypeFormatFlags.None);
49+
}
50+
4351
return `${visibility}${name}: ${typeString};${newlineChar}`;
4452

4553
case SyntaxKind.MethodSignature:
@@ -51,7 +59,7 @@ namespace ts.codefix {
5159
// If there is more than one overload but no implementation signature
5260
// (eg: an abstract method or interface declaration), there is a 1-1
5361
// correspondence of declarations and signatures.
54-
const signatures = checker.getSignaturesOfType(type, SignatureKind.Call);
62+
const signatures = checker.getSignaturesOfType(typeAtNewDeclaration, SignatureKind.Call);
5563
if (!(signatures && signatures.length > 0)) {
5664
return "";
5765
}

0 commit comments

Comments
 (0)