Skip to content

Commit da5f0fe

Browse files
authored
Add missing members to array of objects in quickfix (#57143)
1 parent 67542c4 commit da5f0fe

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ registerCodeFix({
187187
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
188188
eachDiagnostic(context, errorCodes, diag => {
189189
const info = getInfo(diag.file, diag.start, diag.code, checker, context.program);
190-
if (!info || !addToSeen(seen, getNodeId(info.parentDeclaration) + "#" + info.token.text)) {
190+
if (!info || !addToSeen(seen, getNodeId(info.parentDeclaration) + "#" + (info.kind === InfoKind.ObjectLiteral ? info.identifier : info.token.text))) {
191191
return;
192192
}
193193
if (fixId === fixMissingFunctionDeclaration && (info.kind === InfoKind.Function || info.kind === InfoKind.Signature)) {
@@ -273,7 +273,8 @@ interface FunctionInfo {
273273

274274
interface ObjectLiteralInfo {
275275
readonly kind: InfoKind.ObjectLiteral;
276-
readonly token: Identifier;
276+
readonly token: Node;
277+
readonly identifier: string;
277278
readonly properties: Symbol[];
278279
readonly parentDeclaration: ObjectLiteralExpression;
279280
readonly indentation?: number;
@@ -315,7 +316,18 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
315316

316317
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
317318
if (!length(properties)) return undefined;
318-
return { kind: InfoKind.ObjectLiteral, token: param.name, properties, parentDeclaration: parent };
319+
return { kind: InfoKind.ObjectLiteral, token: param.name, identifier: param.name.text, properties, parentDeclaration: parent };
320+
}
321+
322+
if (token.kind === SyntaxKind.OpenBraceToken && isObjectLiteralExpression(parent)) {
323+
const targetType = checker.getContextualType(parent) || checker.getTypeAtLocation(parent);
324+
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
325+
if (!length(properties)) return undefined;
326+
327+
// no identifier needed because the whole parentDeclaration has the error
328+
const identifier = "";
329+
330+
return { kind: InfoKind.ObjectLiteral, token: parent, identifier, properties, parentDeclaration: parent };
319331
}
320332

321333
if (!isMemberName(token)) return undefined;
@@ -325,7 +337,7 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
325337
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
326338
if (!length(properties)) return undefined;
327339

328-
return { kind: InfoKind.ObjectLiteral, token, properties, parentDeclaration: parent.initializer };
340+
return { kind: InfoKind.ObjectLiteral, token, identifier: token.text, properties, parentDeclaration: parent.initializer };
329341
}
330342

331343
if (isIdentifier(token) && isJsxOpeningLikeElement(token.parent)) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////interface A {
4+
//// a: number;
5+
//// b: string;
6+
////}
7+
////function f(_obj: A[]): string {
8+
//// return "";
9+
////}
10+
////[|f([{}])|]
11+
12+
debugger;
13+
verify.codeFix({
14+
index: 0,
15+
description: ts.Diagnostics.Add_missing_properties.message,
16+
newRangeContent:
17+
`f([{
18+
a: 0,
19+
b: ""
20+
}])`,
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////interface A {
4+
//// a: number;
5+
//// b: string;
6+
////}
7+
////interface B {
8+
//// c: A[];
9+
////}
10+
////[|const b: B[] = [{c: [{}]}]|]
11+
12+
debugger;
13+
verify.codeFix({
14+
index: 0,
15+
description: ts.Diagnostics.Add_missing_properties.message,
16+
newRangeContent:
17+
`const b: B[] = [{c: [{
18+
a: 0,
19+
b: ""
20+
}]}]`,
21+
});

0 commit comments

Comments
 (0)