try get add missing member return type from context (#26250)
* try get add missing member return type from context * support contextual type
This commit is contained in:
parent
65da7e91e1
commit
8ba501926a
3 changed files with 43 additions and 13 deletions
|
@ -112,27 +112,23 @@ namespace ts.codefix {
|
||||||
|
|
||||||
export function createMethodFromCallExpression(
|
export function createMethodFromCallExpression(
|
||||||
context: CodeFixContextBase,
|
context: CodeFixContextBase,
|
||||||
{ typeArguments, arguments: args, parent: parent }: CallExpression,
|
call: CallExpression,
|
||||||
methodName: string,
|
methodName: string,
|
||||||
inJs: boolean,
|
inJs: boolean,
|
||||||
makeStatic: boolean,
|
makeStatic: boolean,
|
||||||
preferences: UserPreferences,
|
preferences: UserPreferences,
|
||||||
body: boolean,
|
body: boolean,
|
||||||
): MethodDeclaration {
|
): MethodDeclaration {
|
||||||
|
const { typeArguments, arguments: args, parent } = call;
|
||||||
const checker = context.program.getTypeChecker();
|
const checker = context.program.getTypeChecker();
|
||||||
const types = map(args,
|
const types = map(args, arg =>
|
||||||
arg => {
|
|
||||||
let type = checker.getTypeAtLocation(arg);
|
|
||||||
if (type === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
// Widen the type so we don't emit nonsense annotations like "function fn(x: 3) {"
|
// Widen the type so we don't emit nonsense annotations like "function fn(x: 3) {"
|
||||||
type = checker.getBaseTypeOfLiteralType(type);
|
checker.typeToTypeNode(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(arg))));
|
||||||
return checker.typeToTypeNode(type);
|
|
||||||
});
|
|
||||||
const names = map(args, arg =>
|
const names = map(args, arg =>
|
||||||
isIdentifier(arg) ? arg.text :
|
isIdentifier(arg) ? arg.text :
|
||||||
isPropertyAccessExpression(arg) ? arg.name.text : undefined);
|
isPropertyAccessExpression(arg) ? arg.name.text : undefined);
|
||||||
|
const contextualType = checker.getContextualType(call);
|
||||||
|
const returnType = inJs ? undefined : contextualType && checker.typeToTypeNode(contextualType, call) || createKeywordTypeNode(SyntaxKind.AnyKeyword);
|
||||||
return createMethod(
|
return createMethod(
|
||||||
/*decorators*/ undefined,
|
/*decorators*/ undefined,
|
||||||
/*modifiers*/ makeStatic ? [createToken(SyntaxKind.StaticKeyword)] : undefined,
|
/*modifiers*/ makeStatic ? [createToken(SyntaxKind.StaticKeyword)] : undefined,
|
||||||
|
@ -142,7 +138,7 @@ namespace ts.codefix {
|
||||||
/*typeParameters*/ inJs ? undefined : map(typeArguments, (_, i) =>
|
/*typeParameters*/ inJs ? undefined : map(typeArguments, (_, i) =>
|
||||||
createTypeParameterDeclaration(CharacterCodes.T + typeArguments!.length - 1 <= CharacterCodes.Z ? String.fromCharCode(CharacterCodes.T + i) : `T${i}`)),
|
createTypeParameterDeclaration(CharacterCodes.T + typeArguments!.length - 1 <= CharacterCodes.Z ? String.fromCharCode(CharacterCodes.T + i) : `T${i}`)),
|
||||||
/*parameters*/ createDummyParameters(args.length, names, types, /*minArgumentCount*/ undefined, inJs),
|
/*parameters*/ createDummyParameters(args.length, names, types, /*minArgumentCount*/ undefined, inJs),
|
||||||
/*type*/ inJs ? undefined : createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
/*type*/ returnType,
|
||||||
body ? createStubbedMethodBody(preferences) : undefined);
|
body ? createStubbedMethodBody(preferences) : undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
tests/cases/fourslash/codeFixAddMissingMember10.ts
Normal file
16
tests/cases/fourslash/codeFixAddMissingMember10.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
/// <reference path='fourslash.ts' />
|
||||||
|
|
||||||
|
//// class C {}
|
||||||
|
//// const n: number = new C().add(1, 2);
|
||||||
|
|
||||||
|
verify.codeFixAll({
|
||||||
|
fixId: "addMissingMember",
|
||||||
|
fixAllDescription: "Add all missing members",
|
||||||
|
newFileContent:
|
||||||
|
`class C {
|
||||||
|
add(arg0: number, arg1: number): number {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const n: number = new C().add(1, 2);`,
|
||||||
|
});
|
18
tests/cases/fourslash/codeFixAddMissingMember11.ts
Normal file
18
tests/cases/fourslash/codeFixAddMissingMember11.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/// <reference path='fourslash.ts' />
|
||||||
|
|
||||||
|
//// class C {}
|
||||||
|
//// function f(v: number): void { }
|
||||||
|
//// f(new C().add(1, 2))
|
||||||
|
|
||||||
|
verify.codeFixAll({
|
||||||
|
fixId: "addMissingMember",
|
||||||
|
fixAllDescription: "Add all missing members",
|
||||||
|
newFileContent:
|
||||||
|
`class C {
|
||||||
|
add(arg0: number, arg1: number): number {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function f(v: number): void { }
|
||||||
|
f(new C().add(1, 2))`,
|
||||||
|
});
|
Loading…
Reference in a new issue