Skip to content

Commit 21c4300

Browse files
committed
Enable new behavior only in --noImplicitThis mode
1 parent ee7b93c commit 21c4300

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/compiler/checker.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11570,36 +11570,38 @@ namespace ts {
1157011570
}
1157111571
}
1157211572
}
11573-
const containingLiteral = getContainingObjectLiteral(func);
11574-
if (containingLiteral) {
11575-
// We have an object literal method. Check if the containing object literal has a contextual type
11576-
// that includes a ThisType<T>. If so, T is the contextual type for 'this'. We continue looking in
11577-
// any directly enclosing object literals.
11578-
const contextualType = getApparentTypeOfContextualType(containingLiteral);
11579-
let literal = containingLiteral;
11580-
let type = contextualType;
11581-
while (type) {
11582-
const thisType = getThisTypeFromContextualType(type);
11583-
if (thisType) {
11584-
return instantiateType(thisType, getContextualMapper(containingLiteral));
11585-
}
11586-
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
11587-
break;
11573+
if (compilerOptions.noImplicitThis) {
11574+
const containingLiteral = getContainingObjectLiteral(func);
11575+
if (containingLiteral) {
11576+
// We have an object literal method. Check if the containing object literal has a contextual type
11577+
// that includes a ThisType<T>. If so, T is the contextual type for 'this'. We continue looking in
11578+
// any directly enclosing object literals.
11579+
const contextualType = getApparentTypeOfContextualType(containingLiteral);
11580+
let literal = containingLiteral;
11581+
let type = contextualType;
11582+
while (type) {
11583+
const thisType = getThisTypeFromContextualType(type);
11584+
if (thisType) {
11585+
return instantiateType(thisType, getContextualMapper(containingLiteral));
11586+
}
11587+
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
11588+
break;
11589+
}
11590+
literal = <ObjectLiteralExpression>literal.parent.parent;
11591+
type = getApparentTypeOfContextualType(literal);
1158811592
}
11589-
literal = <ObjectLiteralExpression>literal.parent.parent;
11590-
type = getApparentTypeOfContextualType(literal);
11593+
// There was no contextual ThisType<T> for the containing object literal, so the contextual type
11594+
// for 'this' is the contextual type for the containing object literal or the type of the object
11595+
// literal itself.
11596+
return contextualType || checkExpressionCached(containingLiteral);
1159111597
}
11592-
// There was no contextual ThisType<T> for the containing object literal, so the contextual type
11593-
// for 'this' is the contextual type for the containing object literal or the type of the object
11594-
// literal itself.
11595-
return contextualType || checkExpressionCached(containingLiteral);
11596-
}
11597-
// In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
11598-
// contextual type for 'this' is 'obj'.
11599-
if (func.parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>func.parent).operatorToken.kind === SyntaxKind.EqualsToken) {
11600-
const target = (<BinaryExpression>func.parent).left;
11601-
if (target.kind === SyntaxKind.PropertyAccessExpression || target.kind === SyntaxKind.ElementAccessExpression) {
11602-
return checkExpressionCached((<PropertyAccessExpression | ElementAccessExpression>target).expression);
11598+
// In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
11599+
// contextual type for 'this' is 'obj'.
11600+
if (func.parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>func.parent).operatorToken.kind === SyntaxKind.EqualsToken) {
11601+
const target = (<BinaryExpression>func.parent).left;
11602+
if (target.kind === SyntaxKind.PropertyAccessExpression || target.kind === SyntaxKind.ElementAccessExpression) {
11603+
return checkExpressionCached((<PropertyAccessExpression | ElementAccessExpression>target).expression);
11604+
}
1160311605
}
1160411606
}
1160511607
return undefined;

0 commit comments

Comments
 (0)