Fixed "add missing properties" codefix for positions with nullable contextual types (#60328)

This commit is contained in:
Mateusz Burzyński 2024-10-23 20:31:37 +02:00 коммит произвёл GitHub
Родитель 437d7f7d9c
Коммит 6a90111d05
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
7 изменённых файлов: 78 добавлений и 5 удалений

Просмотреть файл

@ -315,13 +315,13 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
const param = signature.parameters[argIndex].valueDeclaration;
if (!(param && isParameter(param) && isIdentifier(param.name))) return undefined;
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex).getNonNullableType(), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
if (!length(properties)) return undefined;
return { kind: InfoKind.ObjectLiteral, token: param.name, identifier: param.name.text, properties, parentDeclaration: parent };
}
if (token.kind === SyntaxKind.OpenBraceToken && isObjectLiteralExpression(parent)) {
const targetType = checker.getContextualType(parent) || checker.getTypeAtLocation(parent);
const targetType = (checker.getContextualType(parent) || checker.getTypeAtLocation(parent))?.getNonNullableType();
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
if (!length(properties)) return undefined;
@ -334,7 +334,7 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
if (!isMemberName(token)) return undefined;
if (isIdentifier(token) && hasInitializer(parent) && parent.initializer && isObjectLiteralExpression(parent.initializer)) {
const targetType = checker.getContextualType(token) || checker.getTypeAtLocation(token);
const targetType = (checker.getContextualType(token) || checker.getTypeAtLocation(token))?.getNonNullableType();
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
if (!length(properties)) return undefined;

Просмотреть файл

@ -9,7 +9,6 @@
////}
////[|f([{}])|]
debugger;
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,

Просмотреть файл

@ -9,7 +9,6 @@
////}
////[|const b: B[] = [{c: [{}]}]|]
debugger;
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,

Просмотреть файл

@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// type U = { u?: { v: string } };
//// const u: U = { [|u: {}|] };
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
newRangeContent:
`u: {
v: ""
}`,
});

Просмотреть файл

@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// type T = { t: string };
//// declare function f(arg?: T): void;
//// f([|{}|]);
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
newRangeContent:
`{
t: ""
}`,
});

Просмотреть файл

@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// interface A {
//// a: number;
//// b: string;
//// }
//// function f(_obj: (A | undefined)[]): string {
//// return "";
//// }
//// [|f([{}]);|]
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
newRangeContent:
`f([{
a: 0,
b: ""
}]);`,
});

Просмотреть файл

@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />
// @strict: true
//// interface A {
//// a: number;
//// b: string;
//// }
//// interface B {
//// c: (A | undefined)[];
//// }
//// [|const b: B[] = [{ c: [{}] }];|]
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
newRangeContent:
`const b: B[] = [{ c: [{
a: 0,
b: ""
}] }];`,
});