mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Resolve function-this in parameter initialisers when explicitly provided
This commit is contained in:
@@ -8392,7 +8392,10 @@ namespace ts {
|
||||
if (needToCaptureLexicalThis) {
|
||||
captureLexicalThis(node, container);
|
||||
}
|
||||
if (isFunctionLike(container) && !isInParameterInitializerBeforeContainingFunction(node)) {
|
||||
if (isFunctionLike(container) &&
|
||||
(!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) {
|
||||
// Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
|
||||
|
||||
// If this is a function in a JS file, it might be a class method. Check if it's the RHS
|
||||
// of a x.prototype.y = function [name]() { .... }
|
||||
if (container.kind === SyntaxKind.FunctionExpression &&
|
||||
@@ -18473,6 +18476,14 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function getFunctionLikeThisParameter(func: FunctionLikeDeclaration) {
|
||||
if (func.parameters.length &&
|
||||
func.parameters[0].name.kind === SyntaxKind.Identifier &&
|
||||
(<Identifier>func.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
|
||||
return func.parameters[0];
|
||||
}
|
||||
}
|
||||
|
||||
function checkGrammarForNonSymbolComputedProperty(node: DeclarationName, message: DiagnosticMessage) {
|
||||
if (isDynamicName(node)) {
|
||||
return grammarErrorOnNode(node, message);
|
||||
|
||||
@@ -10,6 +10,14 @@ class Example {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
function weird(this: Example, a = this.getNumber()) {
|
||||
return a;
|
||||
}
|
||||
class Weird {
|
||||
doSomething(this: Example, a = this.getNumber()) {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [inferParameterWithMethodCallInitializer.js]
|
||||
@@ -28,3 +36,16 @@ var Example = (function () {
|
||||
};
|
||||
return Example;
|
||||
}());
|
||||
function weird(a) {
|
||||
if (a === void 0) { a = this.getNumber(); }
|
||||
return a;
|
||||
}
|
||||
var Weird = (function () {
|
||||
function Weird() {
|
||||
}
|
||||
Weird.prototype.doSomething = function (a) {
|
||||
if (a === void 0) { a = this.getNumber(); }
|
||||
return a;
|
||||
};
|
||||
return Weird;
|
||||
}());
|
||||
|
||||
@@ -24,4 +24,32 @@ class Example {
|
||||
>a : Symbol(a, Decl(inferParameterWithMethodCallInitializer.ts, 7, 16))
|
||||
}
|
||||
}
|
||||
function weird(this: Example, a = this.getNumber()) {
|
||||
>weird : Symbol(weird, Decl(inferParameterWithMethodCallInitializer.ts, 10, 1))
|
||||
>this : Symbol(this, Decl(inferParameterWithMethodCallInitializer.ts, 11, 15))
|
||||
>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))
|
||||
>getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15))
|
||||
|
||||
return a;
|
||||
>a : Symbol(a, Decl(inferParameterWithMethodCallInitializer.ts, 11, 29))
|
||||
}
|
||||
class Weird {
|
||||
>Weird : Symbol(Weird, Decl(inferParameterWithMethodCallInitializer.ts, 13, 1))
|
||||
|
||||
doSomething(this: Example, a = this.getNumber()) {
|
||||
>doSomething : Symbol(Weird.doSomething, Decl(inferParameterWithMethodCallInitializer.ts, 14, 13))
|
||||
>this : Symbol(this, Decl(inferParameterWithMethodCallInitializer.ts, 15, 16))
|
||||
>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))
|
||||
>getNumber : Symbol(Example.getNumber, Decl(inferParameterWithMethodCallInitializer.ts, 3, 15))
|
||||
|
||||
return a;
|
||||
>a : Symbol(a, Decl(inferParameterWithMethodCallInitializer.ts, 15, 30))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,4 +27,34 @@ class Example {
|
||||
>a : number
|
||||
}
|
||||
}
|
||||
function weird(this: Example, a = this.getNumber()) {
|
||||
>weird : (this: Example, a?: number) => number
|
||||
>this : Example
|
||||
>Example : Example
|
||||
>a : number
|
||||
>this.getNumber() : number
|
||||
>this.getNumber : () => number
|
||||
>this : Example
|
||||
>getNumber : () => number
|
||||
|
||||
return a;
|
||||
>a : number
|
||||
}
|
||||
class Weird {
|
||||
>Weird : Weird
|
||||
|
||||
doSomething(this: Example, a = this.getNumber()) {
|
||||
>doSomething : (this: Example, a?: number) => number
|
||||
>this : Example
|
||||
>Example : Example
|
||||
>a : number
|
||||
>this.getNumber() : number
|
||||
>this.getNumber : () => number
|
||||
>this : Example
|
||||
>getNumber : () => number
|
||||
|
||||
return a;
|
||||
>a : number
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,3 +10,11 @@ class Example {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
function weird(this: Example, a = this.getNumber()) {
|
||||
return a;
|
||||
}
|
||||
class Weird {
|
||||
doSomething(this: Example, a = this.getNumber()) {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user