Skip to content

Additional fix to issue #50509 #54423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28672,6 +28672,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
});
}

function getThisTypeOfObjectLiteralFromContextualType(containingLiteral: ObjectLiteralExpression, contextualType: Type | undefined) {
let literal = containingLiteral;
let type = contextualType;
while (type) {
const thisType = getThisTypeFromContextualType(type);
if (thisType) {
return thisType;
}
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
break;
}
literal = literal.parent.parent as ObjectLiteralExpression;
type = getApparentTypeOfContextualType(literal, /*contextFlags*/ undefined);
}
}

function getContextualThisParameterType(func: SignatureDeclaration): Type | undefined {
if (func.kind === SyntaxKind.ArrowFunction) {
return undefined;
Expand All @@ -28693,18 +28709,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// that includes a ThisType<T>. If so, T is the contextual type for 'this'. We continue looking in
// any directly enclosing object literals.
const contextualType = getApparentTypeOfContextualType(containingLiteral, /*contextFlags*/ undefined);
let literal = containingLiteral;
let type = contextualType;
while (type) {
const thisType = getThisTypeFromContextualType(type);
if (thisType) {
return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
}
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
break;
}
literal = literal.parent.parent as ObjectLiteralExpression;
type = getApparentTypeOfContextualType(literal, /*contextFlags*/ undefined);
const thisType = getThisTypeOfObjectLiteralFromContextualType(containingLiteral, contextualType);
if (thisType) {
return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
}
// There was no contextual ThisType<T> for the containing object literal, so the contextual type
// for 'this' is the non-null form of the contextual type for the containing object literal or
Expand Down Expand Up @@ -45424,7 +45431,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const containingLiteral = getContainingObjectLiteral(container);
if (containingLiteral) {
const contextualType = getApparentTypeOfContextualType(containingLiteral, /*contextFlags*/ undefined);
const type = contextualType && getThisTypeFromContextualType(contextualType);
const type = getThisTypeOfObjectLiteralFromContextualType(containingLiteral, contextualType);
return type && !isTypeAny(type);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// === findAllReferences ===
// === /tests/cases/fourslash/infer.d.ts ===
// export declare function infer(o: { m: Record<string, Function> } & ThisType<{ <|[|{| isWriteAccess: true |}x|]: number|> }>): void;

// === /tests/cases/fourslash/a.js ===
// import { infer } from "./infer";
// infer({
// m: {
// initData() {
// <|this.[|{| isWriteAccess: true |}x|] = 1;|>
// this./*FIND ALL REFS*/[|x|];
// },
// }
// });

// === /tests/cases/fourslash/b.ts ===
// import { infer } from "./infer";
// infer({
// m: {
// initData() {
// this.[|{| isWriteAccess: true |}x|] = 1;
// this.[|x|];
// },
// }
// });

// === Definitions ===
// === /tests/cases/fourslash/infer.d.ts ===
// export declare function infer(o: { m: Record<string, Function> } & ThisType<{ <|[|x|]: number|> }>): void;

// === Details ===
[
{
"containerKind": "",
"containerName": "",
"kind": "property",
"name": "(property) x: number",
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "x",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
]
}
]



// === findAllReferences ===
// === /tests/cases/fourslash/infer.d.ts ===
// export declare function infer(o: { m: Record<string, Function> } & ThisType<{ <|[|{| isWriteAccess: true |}x|]: number|> }>): void;

// === /tests/cases/fourslash/a.js ===
// import { infer } from "./infer";
// infer({
// m: {
// initData() {
// <|this.[|{| isWriteAccess: true |}x|] = 1;|>
// this.[|x|];
// },
// }
// });

// === /tests/cases/fourslash/b.ts ===
// import { infer } from "./infer";
// infer({
// m: {
// initData() {
// this.[|{| isWriteAccess: true |}x|] = 1;
// this./*FIND ALL REFS*/[|x|];
// },
// }
// });

// === Definitions ===
// === /tests/cases/fourslash/infer.d.ts ===
// export declare function infer(o: { m: Record<string, Function> } & ThisType<{ <|[|x|]: number|> }>): void;

// === Details ===
[
{
"containerKind": "",
"containerName": "",
"kind": "property",
"name": "(property) x: number",
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "x",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
]
}
]
31 changes: 31 additions & 0 deletions tests/cases/fourslash/findAllRefsJsThisPropertyAssignment2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />

// @allowJs: true
// @noImplicitThis: true

// @Filename: infer.d.ts
//// export declare function infer(o: { m: Record<string, Function> } & ThisType<{ x: number }>): void;

// @Filename: a.js
//// import { infer } from "./infer";
//// infer({
//// m: {
//// initData() {
//// this.x = 1;
//// this./*1*/x;
//// },
//// }
//// });

// @Filename: b.ts
//// import { infer } from "./infer";
//// infer({
//// m: {
//// initData() {
//// this.x = 1;
//// this./*2*/x;
//// },
//// }
//// });

verify.baselineFindAllReferences("1", "2");