From 07437a6e7c5db627d4e73f622e4a1d9126a39bfc Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 20 Jun 2016 10:55:17 -0700 Subject: [PATCH] Make goto-definition work for `this` parameter --- src/compiler/checker.ts | 37 +++++++++++++++---- src/compiler/types.ts | 2 + src/services/services.ts | 21 +---------- ...ParameterWithMethodCallInitializer.symbols | 4 +- .../reference/thisTypeInAccessors.symbols | 20 +++++----- .../reference/thisTypeInFunctions.symbols | 36 +++++++++--------- tests/cases/fourslash/goToDefinitionThis.ts | 20 ++++++++++ tests/cases/fourslash/quickInfoOnThis.ts | 2 +- tests/cases/fourslash/quickInfoOnThis3.ts | 2 +- tests/cases/fourslash/renameThis.ts | 2 +- 10 files changed, 86 insertions(+), 60 deletions(-) create mode 100644 tests/cases/fourslash/goToDefinitionThis.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ac8a47a9913..0fb345cf610 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -134,8 +134,8 @@ namespace ts { const noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - const anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); - const unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); + const anySignature = createSignature(undefined, undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); + const unknownSignature = createSignature(undefined, undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); const enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); @@ -3198,6 +3198,11 @@ namespace ts { return undefined; } + function getAnnotatedAccessorThisParameter(accessor: AccessorDeclaration): Symbol { + const parameter = getAccessorThisParameter(accessor); + return parameter && parameter.symbol; + } + function getAnnotatedAccessorThisType(accessor: AccessorDeclaration): Type { if (accessor) { const parameter = getAccessorThisParameter(accessor); @@ -3888,12 +3893,13 @@ namespace ts { resolveObjectTypeMembers(type, source, typeParameters, typeArguments); } - function createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], thisType: Type, parameters: Symbol[], + function createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], thisParameter: Symbol | undefined, thisType: Type | undefined, parameters: Symbol[], resolvedReturnType: Type, typePredicate: TypePredicate, minArgumentCount: number, hasRestParameter: boolean, hasStringLiterals: boolean): Signature { const sig = new Signature(checker); sig.declaration = declaration; sig.typeParameters = typeParameters; sig.parameters = parameters; + sig.thisParameter = thisParameter; sig.thisType = thisType; sig.resolvedReturnType = resolvedReturnType; sig.typePredicate = typePredicate; @@ -3904,7 +3910,7 @@ namespace ts { } function cloneSignature(sig: Signature): Signature { - return createSignature(sig.declaration, sig.typeParameters, sig.thisType, sig.parameters, sig.resolvedReturnType, + return createSignature(sig.declaration, sig.typeParameters, sig.thisParameter, sig.thisType, sig.parameters, sig.resolvedReturnType, sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasStringLiterals); } @@ -3912,7 +3918,7 @@ namespace ts { const baseConstructorType = getBaseConstructorTypeOfClass(classType); const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct); if (baseSignatures.length === 0) { - return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false)]; + return [createSignature(undefined, classType.localTypeParameters, undefined, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false)]; } const baseTypeNode = getBaseTypeNodeOfClass(classType); const typeArguments = map(baseTypeNode.typeArguments, getTypeFromTypeNode); @@ -4454,6 +4460,7 @@ namespace ts { const parameters: Symbol[] = []; let hasStringLiterals = false; let minArgumentCount = -1; + let thisParameter: Symbol = undefined; let thisType: Type = undefined; let hasThisParameter: boolean; const isJSConstructSignature = isJSDocConstructSignature(declaration); @@ -4472,6 +4479,7 @@ namespace ts { } if (i === 0 && paramSymbol.name === "this") { hasThisParameter = true; + thisParameter = param.symbol; thisType = param.type ? getTypeFromTypeNode(param.type) : unknownType; } else { @@ -4498,8 +4506,11 @@ namespace ts { !hasDynamicName(declaration) && (!hasThisParameter || thisType === unknownType)) { const otherKind = declaration.kind === SyntaxKind.GetAccessor ? SyntaxKind.SetAccessor : SyntaxKind.GetAccessor; - const setter = getDeclarationOfKind(declaration.symbol, otherKind); - thisType = getAnnotatedAccessorThisType(setter); + const other = getDeclarationOfKind(declaration.symbol, otherKind); + if (other) { + thisParameter = getAnnotatedAccessorThisParameter(other); + thisType = getAnnotatedAccessorThisType(other); + } } if (minArgumentCount < 0) { @@ -4520,7 +4531,7 @@ namespace ts { createTypePredicateFromTypePredicateNode(declaration.type as TypePredicateNode) : undefined; - links.resolvedSignature = createSignature(declaration, typeParameters, thisType, parameters, returnType, typePredicate, minArgumentCount, hasRestParameter(declaration), hasStringLiterals); + links.resolvedSignature = createSignature(declaration, typeParameters, thisParameter, thisType, parameters, returnType, typePredicate, minArgumentCount, hasRestParameter(declaration), hasStringLiterals); } return links.resolvedSignature; } @@ -5453,6 +5464,7 @@ namespace ts { freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); } const result = createSignature(signature.declaration, freshTypeParameters, + signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), signature.thisType && instantiateType(signature.thisType, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), @@ -17156,6 +17168,15 @@ namespace ts { return getSymbolOfEntityNameOrPropertyAccessExpression(node); case SyntaxKind.ThisKeyword: + const container = getThisContainer(node, /*includeArrowFunctions*/ false); + if (isFunctionLike(container)) { + const sig = getSignatureFromDeclaration(container); + if (sig.thisParameter) { + return sig.thisParameter; + } + } + // fallthrough + case SyntaxKind.SuperKeyword: const type = isExpression(node) ? checkExpression(node) : getTypeFromTypeNode(node); return type.symbol; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 22861f04c5e..fbda9df5e03 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2388,6 +2388,8 @@ namespace ts { parameters: Symbol[]; // Parameters thisType?: Type; // type of this-type /* @internal */ + thisParameter?: Symbol; // symbol of this-type parameter + /* @internal */ resolvedReturnType: Type; // Resolved return type /* @internal */ minArgumentCount: number; // Number of non-optional parameters diff --git a/src/services/services.ts b/src/services/services.ts index 853817b05e8..a2b2bc44097 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -779,6 +779,7 @@ namespace ts { declaration: SignatureDeclaration; typeParameters: TypeParameter[]; parameters: Symbol[]; + thisParameter: Symbol; thisType: Type; resolvedReturnType: Type; minArgumentCount: number; @@ -8069,26 +8070,6 @@ namespace ts { } } } - else if (node.kind === SyntaxKind.ThisKeyword) { - const container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - // Only allow rename to change a function with a 'this' type to one with a regular parameter, - // e.g. `function(this: number) { return this; }` to `function(x: number) { return x; }` - if (isFunctionLike(container)) { - const sig = typeChecker.getSignatureFromDeclaration(container); - if (sig.thisType) { - return { - canRename: true, - kind: ScriptElementKind.parameterElement, - displayName: "this", - localizedErrorMessage: undefined, - fullDisplayName: "this", - kindModifiers: "", - triggerSpan: createTriggerSpanForNode(node, sourceFile) - }; - } - } - // fallthrough to error - } } } diff --git a/tests/baselines/reference/inferParameterWithMethodCallInitializer.symbols b/tests/baselines/reference/inferParameterWithMethodCallInitializer.symbols index e0734a4b39b..ce098f6e243 100644 --- a/tests/baselines/reference/inferParameterWithMethodCallInitializer.symbols +++ b/tests/baselines/reference/inferParameterWithMethodCallInitializer.symbols @@ -30,7 +30,7 @@ function weird(this: Example, a = this.getNumber()) { >Example : Symbol(Example, Decl(inferParameterWithMethodCallInitializer.ts, 2, 1)) >a : Symbol(a, Decl(inferParameterWithMethodCallInitializer.ts, 11, 29)) >this.getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15)) ->this : Symbol(Example, Decl(inferParameterWithMethodCallInitializer.ts, 2, 1)) +>this : Symbol(this, Decl(inferParameterWithMethodCallInitializer.ts, 11, 15)) >getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15)) return a; @@ -45,7 +45,7 @@ class Weird { >Example : Symbol(Example, Decl(inferParameterWithMethodCallInitializer.ts, 2, 1)) >a : Symbol(a, Decl(inferParameterWithMethodCallInitializer.ts, 15, 30)) >this.getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15)) ->this : Symbol(Example, Decl(inferParameterWithMethodCallInitializer.ts, 2, 1)) +>this : Symbol(this, Decl(inferParameterWithMethodCallInitializer.ts, 15, 16)) >getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15)) return a; diff --git a/tests/baselines/reference/thisTypeInAccessors.symbols b/tests/baselines/reference/thisTypeInAccessors.symbols index ef2201d3808..43f3f2fb0a1 100644 --- a/tests/baselines/reference/thisTypeInAccessors.symbols +++ b/tests/baselines/reference/thisTypeInAccessors.symbols @@ -20,7 +20,7 @@ const explicit = { >this : Symbol(this, Decl(thisTypeInAccessors.ts, 7, 10)) >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 7, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) set x(this: Foo, n: number) { this.n = n; } @@ -29,7 +29,7 @@ const explicit = { >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 8, 20)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 8, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 8, 20)) } @@ -44,14 +44,14 @@ const copiedFromGetter = { >this : Symbol(this, Decl(thisTypeInAccessors.ts, 12, 10)) >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 12, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) set x(n) { this.n = n; } >x : Symbol(x, Decl(thisTypeInAccessors.ts, 11, 10), Decl(thisTypeInAccessors.ts, 12, 48)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 13, 10)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 12, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 13, 10)) } @@ -64,7 +64,7 @@ const copiedFromSetter = { get x() { return this.n }, >x : Symbol(x, Decl(thisTypeInAccessors.ts, 16, 10), Decl(thisTypeInAccessors.ts, 17, 30)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 18, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) set x(this: Foo, n: number) { this.n = n; } @@ -73,7 +73,7 @@ const copiedFromSetter = { >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 18, 20)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 18, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 18, 20)) } @@ -88,7 +88,7 @@ const copiedFromGetterUnannotated = { >this : Symbol(this, Decl(thisTypeInAccessors.ts, 22, 10)) >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 22, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) set x(this, n) { this.n = n; } @@ -96,7 +96,7 @@ const copiedFromGetterUnannotated = { >this : Symbol(this, Decl(thisTypeInAccessors.ts, 23, 10)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 23, 15)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 22, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 23, 15)) } @@ -112,7 +112,7 @@ class Explicit { >this : Symbol(this, Decl(thisTypeInAccessors.ts, 28, 10)) >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 28, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) set x(this: Foo, n: number) { this.n = n; } @@ -121,7 +121,7 @@ class Explicit { >Foo : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 29, 20)) >this.n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) ->this : Symbol(Foo, Decl(thisTypeInAccessors.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInAccessors.ts, 29, 10)) >n : Symbol(Foo.n, Decl(thisTypeInAccessors.ts, 0, 15)) >n : Symbol(n, Decl(thisTypeInAccessors.ts, 29, 20)) } diff --git a/tests/baselines/reference/thisTypeInFunctions.symbols b/tests/baselines/reference/thisTypeInFunctions.symbols index 3d4c3f6ecc7..8a0be7427ed 100644 --- a/tests/baselines/reference/thisTypeInFunctions.symbols +++ b/tests/baselines/reference/thisTypeInFunctions.symbols @@ -19,7 +19,7 @@ class C { return this.n + m; >this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) ->this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 6, 17)) >n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 6, 28)) } @@ -31,7 +31,7 @@ class C { return this.n + m; >this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) ->this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 9, 14)) >n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 9, 22)) } @@ -43,7 +43,7 @@ class C { return this.n + m; >this.n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28)) ->this : Symbol(, Decl(thisTypeInFunctions.ts, 12, 26)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 12, 21)) >n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 12, 39)) } @@ -97,7 +97,7 @@ function explicitStructural(this: { y: number }, x: number): number { return x + this.y; >x : Symbol(x, Decl(thisTypeInFunctions.ts, 28, 48)) >this.y : Symbol(y, Decl(thisTypeInFunctions.ts, 28, 35)) ->this : Symbol(, Decl(thisTypeInFunctions.ts, 28, 33)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 28, 28)) >y : Symbol(y, Decl(thisTypeInFunctions.ts, 28, 35)) } function justThis(this: { y: number }): number { @@ -107,7 +107,7 @@ function justThis(this: { y: number }): number { return this.y; >this.y : Symbol(y, Decl(thisTypeInFunctions.ts, 31, 25)) ->this : Symbol(, Decl(thisTypeInFunctions.ts, 31, 23)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 31, 18)) >y : Symbol(y, Decl(thisTypeInFunctions.ts, 31, 25)) } function implicitThis(n: number): number { @@ -435,7 +435,7 @@ c.explicitC = function(this: C, m: number) { return this.n + m }; >C : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 105, 31)) >this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) ->this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 105, 23)) >n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 105, 31)) @@ -453,7 +453,7 @@ c.explicitProperty = function(this: {n: number}, m: number) { return this.n + m >n : Symbol(n, Decl(thisTypeInFunctions.ts, 107, 37)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 107, 48)) >this.n : Symbol(n, Decl(thisTypeInFunctions.ts, 107, 37)) ->this : Symbol(, Decl(thisTypeInFunctions.ts, 107, 35)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 107, 30)) >n : Symbol(n, Decl(thisTypeInFunctions.ts, 107, 37)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 107, 48)) @@ -525,7 +525,7 @@ c.explicitThis = function(this: C, m: number) { return this.n + m }; >C : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 123, 34)) >this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) ->this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 123, 26)) >n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 123, 34)) @@ -568,7 +568,7 @@ c.explicitThis = function(this, m) { return this.n + m }; >this : Symbol(this, Decl(thisTypeInFunctions.ts, 131, 26)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 131, 31)) >this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) ->this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 131, 26)) >n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 131, 31)) @@ -581,7 +581,7 @@ c.explicitC = function(this: B, m: number) { return this.n + m }; >B : Symbol(B, Decl(thisTypeInFunctions.ts, 0, 0)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 134, 31)) >this.n : Symbol(B.n, Decl(thisTypeInFunctions.ts, 1, 9)) ->this : Symbol(B, Decl(thisTypeInFunctions.ts, 0, 0)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 134, 23)) >n : Symbol(B.n, Decl(thisTypeInFunctions.ts, 1, 9)) >m : Symbol(m, Decl(thisTypeInFunctions.ts, 134, 31)) @@ -604,7 +604,7 @@ class Base1 { >polymorphic : Symbol(Base1.polymorphic, Decl(thisTypeInFunctions.ts, 141, 14)) >this : Symbol(this, Decl(thisTypeInFunctions.ts, 142, 23)) >this.x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) ->this : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 142, 23)) >x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) explicit(this: Base1): number { return this.x; } @@ -612,7 +612,7 @@ class Base1 { >this : Symbol(this, Decl(thisTypeInFunctions.ts, 143, 13)) >Base1 : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) >this.x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) ->this : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 143, 13)) >x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) static explicitStatic(this: typeof Base1): number { return this.y; } @@ -620,7 +620,7 @@ class Base1 { >this : Symbol(this, Decl(thisTypeInFunctions.ts, 144, 26)) >Base1 : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) >this.y : Symbol(Base1.y, Decl(thisTypeInFunctions.ts, 144, 72)) ->this : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 144, 26)) >y : Symbol(Base1.y, Decl(thisTypeInFunctions.ts, 144, 72)) static y: number; @@ -643,7 +643,7 @@ class Base2 { >polymorphic : Symbol(Base2.polymorphic, Decl(thisTypeInFunctions.ts, 151, 13)) >this : Symbol(this, Decl(thisTypeInFunctions.ts, 152, 16)) >this.y : Symbol(Base2.y, Decl(thisTypeInFunctions.ts, 150, 13)) ->this : Symbol(Base2, Decl(thisTypeInFunctions.ts, 149, 1)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 152, 16)) >y : Symbol(Base2.y, Decl(thisTypeInFunctions.ts, 150, 13)) explicit(this: Base1): number { return this.x; } @@ -651,7 +651,7 @@ class Base2 { >this : Symbol(this, Decl(thisTypeInFunctions.ts, 153, 13)) >Base1 : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) >this.x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) ->this : Symbol(Base1, Decl(thisTypeInFunctions.ts, 137, 24)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 153, 13)) >x : Symbol(Base1.x, Decl(thisTypeInFunctions.ts, 140, 13)) } class Derived2 extends Base2 { @@ -734,7 +734,7 @@ function InterfaceThis(this: I) { this.a = 12; >this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13)) ->this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 172, 23)) >a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13)) } function LiteralTypeThis(this: {x: string}) { @@ -744,7 +744,7 @@ function LiteralTypeThis(this: {x: string}) { this.x = "ok"; >this.x : Symbol(x, Decl(thisTypeInFunctions.ts, 175, 32)) ->this : Symbol(, Decl(thisTypeInFunctions.ts, 175, 30)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 175, 25)) >x : Symbol(x, Decl(thisTypeInFunctions.ts, 175, 32)) } function AnyThis(this: any) { @@ -752,6 +752,7 @@ function AnyThis(this: any) { >this : Symbol(this, Decl(thisTypeInFunctions.ts, 178, 17)) this.x = "ok"; +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 178, 17)) } let interfaceThis = new InterfaceThis(); >interfaceThis : Symbol(interfaceThis, Decl(thisTypeInFunctions.ts, 181, 3)) @@ -793,5 +794,6 @@ function missingTypeIsImplicitAny(this, a: number) { return this.anything + a; } >missingTypeIsImplicitAny : Symbol(missingTypeIsImplicitAny, Decl(thisTypeInFunctions.ts, 190, 27)) >this : Symbol(this, Decl(thisTypeInFunctions.ts, 192, 34)) >a : Symbol(a, Decl(thisTypeInFunctions.ts, 192, 39)) +>this : Symbol(this, Decl(thisTypeInFunctions.ts, 192, 34)) >a : Symbol(a, Decl(thisTypeInFunctions.ts, 192, 39)) diff --git a/tests/cases/fourslash/goToDefinitionThis.ts b/tests/cases/fourslash/goToDefinitionThis.ts new file mode 100644 index 00000000000..300e3423d81 --- /dev/null +++ b/tests/cases/fourslash/goToDefinitionThis.ts @@ -0,0 +1,20 @@ +/// + +// @noLib: true +////function f(/*fnDecl*/this: number) { +//// return /*fnUse*/this; +////} +/////*cls*/class C { +//// constructor() { return /*clsUse*/this; } +//// get self(/*getterDecl*/this: number) { return /*getterUse*/this; } +////} + +function verifyDefinition(a, b) { + goTo.marker(a); + goTo.definition(); + verify.caretAtMarker(b); +} + +verifyDefinition("fnUse", "fnDecl"); +verifyDefinition("clsUse", "cls"); +verifyDefinition("getterUse", "getterDecl"); diff --git a/tests/cases/fourslash/quickInfoOnThis.ts b/tests/cases/fourslash/quickInfoOnThis.ts index 4191c0846ec..14737486b87 100644 --- a/tests/cases/fourslash/quickInfoOnThis.ts +++ b/tests/cases/fourslash/quickInfoOnThis.ts @@ -25,7 +25,7 @@ goTo.marker('0'); verify.quickInfoIs('this: this'); goTo.marker('1'); -verify.quickInfoIs('void'); +verify.quickInfoIs('this: void'); goTo.marker('2'); verify.quickInfoIs('this: this'); goTo.marker('3'); diff --git a/tests/cases/fourslash/quickInfoOnThis3.ts b/tests/cases/fourslash/quickInfoOnThis3.ts index 6988ac14860..239e49d5dc0 100644 --- a/tests/cases/fourslash/quickInfoOnThis3.ts +++ b/tests/cases/fourslash/quickInfoOnThis3.ts @@ -20,7 +20,7 @@ verify.quickInfoIs('any'); goTo.marker('2'); verify.quickInfoIs('(parameter) this: void'); goTo.marker('3'); -verify.quickInfoIs('void'); +verify.quickInfoIs('this: void'); goTo.marker('4'); verify.quickInfoIs('(parameter) this: Restricted'); goTo.marker('5'); diff --git a/tests/cases/fourslash/renameThis.ts b/tests/cases/fourslash/renameThis.ts index 011110ef72e..f567cace70d 100644 --- a/tests/cases/fourslash/renameThis.ts +++ b/tests/cases/fourslash/renameThis.ts @@ -12,7 +12,7 @@ for (let range of [r0, r1]) { verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false, [r0, r1]); } -// Trying to rename a legitimate 'this' should fail +// Trying to rename a non-parameter 'this' should fail goTo.marker(); verify.renameInfoFailed("You cannot rename this element.");