Fix missing reference in js (#50509) (#53000)

This commit is contained in:
gu 2023-03-18 06:51:11 +08:00 committed by GitHub
parent b7b0b52d68
commit bf369f1b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 246 additions and 1 deletions

View File

@ -45149,6 +45149,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}
function isThisPropertyAndThisTyped(node: PropertyAccessExpression) {
if (node.expression.kind === SyntaxKind.ThisKeyword) {
const container = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
if (isFunctionLike(container)) {
const containingLiteral = getContainingObjectLiteral(container);
if (containingLiteral) {
const contextualType = getApparentTypeOfContextualType(containingLiteral, /*contextFlags*/ undefined);
const type = contextualType && getThisTypeFromContextualType(contextualType);
return type && !isTypeAny(type);
}
}
}
}
function getSymbolOfNameOrPropertyAccessExpression(name: EntityName | PrivateIdentifier | PropertyAccessExpression | JSDocMemberName): Symbol | undefined {
if (isDeclarationName(name)) {
return getSymbolOfNode(name.parent);
@ -45158,7 +45172,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
name.parent.kind === SyntaxKind.PropertyAccessExpression &&
name.parent === (name.parent.parent as BinaryExpression).left) {
// Check if this is a special property assignment
if (!isPrivateIdentifier(name) && !isJSDocMemberName(name)) {
if (!isPrivateIdentifier(name) && !isJSDocMemberName(name) && !isThisPropertyAndThisTyped(name.parent as PropertyAccessExpression)) {
const specialPropertyAssignmentSymbol = getSpecialPropertyAssignmentSymbolFromEntityName(name);
if (specialPropertyAssignmentSymbol) {
return specialPropertyAssignmentSymbol;

View File

@ -0,0 +1,200 @@
// === /tests/cases/fourslash/infer.d.ts ===
// export declare function infer(o: { m(): void } & ThisType<{ [|x|]: number }>): void;
// === /tests/cases/fourslash/a.js ===
// import { infer } from "./infer";
// infer({
// m() {
// this.[|x|] = 1;
// this./*FIND ALL REFS*/[|x|];
// },
// });
[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/infer.d.ts",
"kind": "property",
"name": "(property) x: number",
"textSpan": {
"start": 60,
"length": 1
},
"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"
}
],
"contextSpan": {
"start": 60,
"length": 9
}
},
"references": [
{
"textSpan": {
"start": 60,
"length": 1
},
"fileName": "/tests/cases/fourslash/infer.d.ts",
"contextSpan": {
"start": 60,
"length": 9
},
"isWriteAccess": true
},
{
"textSpan": {
"start": 64,
"length": 1
},
"fileName": "/tests/cases/fourslash/a.js",
"contextSpan": {
"start": 59,
"length": 11
},
"isWriteAccess": true
},
{
"textSpan": {
"start": 84,
"length": 1
},
"fileName": "/tests/cases/fourslash/a.js",
"isWriteAccess": false
}
]
}
]
// === /tests/cases/fourslash/b.js ===
// /**
// * @template T
// * @param {{m(): void} & ThisType<{[|x|]: number}>} o
// */
// function infer(o) {}
// infer({
// m() {
// this.[|x|] = 2;
// this./*FIND ALL REFS*/[|x|];
// },
// });
[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/b.js",
"kind": "property",
"name": "(property) x: number",
"textSpan": {
"start": 54,
"length": 1
},
"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"
}
],
"contextSpan": {
"start": 54,
"length": 9
}
},
"references": [
{
"textSpan": {
"start": 54,
"length": 1
},
"fileName": "/tests/cases/fourslash/b.js",
"contextSpan": {
"start": 54,
"length": 9
},
"isWriteAccess": false
},
{
"textSpan": {
"start": 125,
"length": 1
},
"fileName": "/tests/cases/fourslash/b.js",
"contextSpan": {
"start": 120,
"length": 11
},
"isWriteAccess": true
},
{
"textSpan": {
"start": 145,
"length": 1
},
"fileName": "/tests/cases/fourslash/b.js",
"isWriteAccess": false
}
]
}
]

View File

@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @noImplicitThis: true
// @Filename: infer.d.ts
//// export declare function infer(o: { m(): void } & ThisType<{ x: number }>): void;
// @Filename: a.js
//// import { infer } from "./infer";
//// infer({
//// m() {
//// this.x = 1;
//// this./*1*/x;
//// },
//// });
// @Filename: b.js
//// /**
//// * @template T
//// * @param {{m(): void} & ThisType<{x: number}>} o
//// */
//// function infer(o) {}
//// infer({
//// m() {
//// this.x = 2;
//// this./*2*/x;
//// },
//// });
verify.baselineFindAllReferences("1", "2");