Skip to content
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

feat(26217): Add missing member fix should work for missing function #41215

Merged
merged 6 commits into from
Jan 9, 2021
Merged
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
17 changes: 17 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -6171,6 +6171,23 @@
"category": "Message",
"code": 95155
},
"Add missing function declaration '{0}'": {
"category": "Message",
"code": 95156
},
"Add all missing function declarations": {
"category": "Message",
"code": 95157
},
"Method not implemented.": {
"category": "Message",
"code": 95158
},
"Function not implemented.": {
"category": "Message",
"code": 95159
},


"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",
139 changes: 97 additions & 42 deletions src/services/codefixes/fixAddMissingMember.ts

Large diffs are not rendered by default.

84 changes: 58 additions & 26 deletions src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
@@ -247,41 +247,69 @@ namespace ts.codefix {
);
}

export function createMethodFromCallExpression(
export function createSignatureDeclarationFromCallExpression(
kind: SyntaxKind.MethodDeclaration | SyntaxKind.FunctionDeclaration,
context: CodeFixContextBase,
importAdder: ImportAdder,
call: CallExpression,
methodName: string,
name: Identifier,
modifierFlags: ModifierFlags,
contextNode: Node,
inJs: boolean
): MethodDeclaration {
const body = !isInterfaceDeclaration(contextNode);
const { typeArguments, arguments: args, parent } = call;
contextNode: Node
) {
const quotePreference = getQuotePreference(context.sourceFile, context.preferences);
const scriptTarget = getEmitScriptTarget(context.program.getCompilerOptions());
const checker = context.program.getTypeChecker();
const tracker = getNoopSymbolTrackerWithResolver(context);
const types = map(args, arg =>
typeToAutoImportableTypeNode(checker, importAdder, checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(arg)), contextNode, scriptTarget, /*flags*/ undefined, tracker));
const checker = context.program.getTypeChecker();
const isJs = isInJSFile(contextNode);
const { typeArguments, arguments: args, parent } = call;

const contextualType = isJs ? undefined : checker.getContextualType(call);
const names = map(args, arg =>
isIdentifier(arg) ? arg.text : isPropertyAccessExpression(arg) && isIdentifier(arg.name) ? arg.name.text : undefined);
const contextualType = checker.getContextualType(call);
const returnType = (inJs || !contextualType) ? undefined : checker.typeToTypeNode(contextualType, contextNode, /*flags*/ undefined, tracker);
const quotePreference = getQuotePreference(context.sourceFile, context.preferences);
return factory.createMethodDeclaration(
const types = isJs ? [] : map(args, arg =>
typeToAutoImportableTypeNode(checker, importAdder, checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(arg)), contextNode, scriptTarget, /*flags*/ undefined, tracker));

const modifiers = modifierFlags
? factory.createNodeArray(factory.createModifiersFromModifierFlags(modifierFlags))
: undefined;
const asteriskToken = isYieldExpression(parent)
? factory.createToken(SyntaxKind.AsteriskToken)
: undefined;
const typeParameters = isJs || typeArguments === undefined
? undefined
: map(typeArguments, (_, i) =>
factory.createTypeParameterDeclaration(CharacterCodes.T + typeArguments.length - 1 <= CharacterCodes.Z ? String.fromCharCode(CharacterCodes.T + i) : `T${i}`));
const parameters = createDummyParameters(args.length, names, types, /*minArgumentCount*/ undefined, isJs);
const type = isJs || contextualType === undefined
? undefined
: checker.typeToTypeNode(contextualType, contextNode, /*flags*/ undefined, tracker);

if (kind === SyntaxKind.MethodDeclaration) {
return factory.createMethodDeclaration(
/*decorators*/ undefined,
modifiers,
asteriskToken,
name,
/*questionToken*/ undefined,
typeParameters,
parameters,
type,
isInterfaceDeclaration(contextNode) ? undefined : createStubbedMethodBody(quotePreference)
);
}
return factory.createFunctionDeclaration(
/*decorators*/ undefined,
/*modifiers*/ modifierFlags ? factory.createNodeArray(factory.createModifiersFromModifierFlags(modifierFlags)) : undefined,
/*asteriskToken*/ isYieldExpression(parent) ? factory.createToken(SyntaxKind.AsteriskToken) : undefined,
methodName,
/*questionToken*/ undefined,
/*typeParameters*/ inJs ? undefined : map(typeArguments, (_, i) =>
factory.createTypeParameterDeclaration(CharacterCodes.T + typeArguments!.length - 1 <= CharacterCodes.Z ? String.fromCharCode(CharacterCodes.T + i) : `T${i}`)),
/*parameters*/ createDummyParameters(args.length, names, types, /*minArgumentCount*/ undefined, inJs),
/*type*/ returnType,
body ? createStubbedMethodBody(quotePreference) : undefined);
modifiers,
asteriskToken,
name,
typeParameters,
parameters,
type,
createStubbedBody(Diagnostics.Function_not_implemented.message, quotePreference)
);
}

export function typeToAutoImportableTypeNode(checker: TypeChecker, importAdder: ImportAdder, type: Type, contextNode: Node, scriptTarget: ScriptTarget, flags?: NodeBuilderFlags, tracker?: SymbolTracker): TypeNode | undefined {
export function typeToAutoImportableTypeNode(checker: TypeChecker, importAdder: ImportAdder, type: Type, contextNode: Node | undefined, scriptTarget: ScriptTarget, flags?: NodeBuilderFlags, tracker?: SymbolTracker): TypeNode | undefined {
const typeNode = checker.typeToTypeNode(type, contextNode, flags, tracker);
if (typeNode && isImportTypeNode(typeNode)) {
const importableReference = tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget);
@@ -381,14 +409,18 @@ namespace ts.codefix {
createStubbedMethodBody(quotePreference));
}

function createStubbedMethodBody(quotePreference: QuotePreference): Block {
function createStubbedMethodBody(quotePreference: QuotePreference) {
return createStubbedBody(Diagnostics.Method_not_implemented.message, quotePreference);
}

export function createStubbedBody(text: string, quotePreference: QuotePreference): Block {
return factory.createBlock(
[factory.createThrowStatement(
factory.createNewExpression(
factory.createIdentifier("Error"),
/*typeArguments*/ undefined,
// TODO Handle auto quote preference.
[factory.createStringLiteral("Method not implemented.", /*isSingleQuote*/ quotePreference === QuotePreference.Single)]))],
[factory.createStringLiteral(text, /*isSingleQuote*/ quotePreference === QuotePreference.Single)]))],
/*multiline*/ true);
}

Original file line number Diff line number Diff line change
@@ -2,4 +2,14 @@

////[x, y()] = [0, () => 1];

verify.not.codeFixAvailable();
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "y"],
newFileContent:
`[x, y()] = [0, () => 1];

function y() {
throw new Error("Function not implemented.");
}
`
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />

////foo();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent:
`foo();

function foo() {
throw new Error("Function not implemented.");
}
`
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

////namespace Foo {
//// export const x = 0;
////}
////
////Foo.test<string, number>();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
newFileContent:
`namespace Foo {
export const x = 0;

export function test<T, U>() {
throw new Error("Function not implemented.");
}
}

Foo.test<string, number>();`
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////test.foo();

goTo.file("/foo.ts");
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent: {
"/test.ts":
`export const x = 1;

export function foo() {
throw new Error("Function not implemented.");
}
`
}
});
28 changes: 28 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////test.foo();
////test.foo();
////test.foo();

goTo.file("/foo.ts");
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
applyChanges: true,
newFileContent: {
"/test.ts":
`export const x = 1;

export function foo() {
throw new Error("Function not implemented.");
}
`
}
});

verify.not.codeFixAvailable();
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////test.foo(1, "", { x: 1, y: 1 });

goTo.file("/foo.ts");
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent: {
"/test.ts":
`export const x = 1;

export function foo(arg0: number, arg1: string, arg2: { x: number; y: number; }) {
throw new Error("Function not implemented.");
}
`
}
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////const foo: string = test.foo();

goTo.file("/foo.ts");
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent: {
"/test.ts":
`export const x = 1;

export function foo(): string {
throw new Error("Function not implemented.");
}
`
}
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration15.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////test.foo<string, number>();

goTo.file("/foo.ts");
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent: {
"/test.ts":
`export const x = 1;

export function foo<T, U>() {
throw new Error("Function not implemented.");
}
`
}
});
12 changes: 12 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />

// @moduleResolution: node
// @filename: /node_modules/test/index.js
////export const x = 1;

// @filename: /foo.ts
////import * as test from "test";
////test.foo();

goTo.file("/foo.ts");
verify.not.codeFixAvailable();
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

////foo();
////foo();
////foo();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
applyChanges: true,
newFileContent:
`foo();
foo();
foo();

function foo() {
throw new Error("Function not implemented.");
}
`
});

verify.not.codeFixAvailable();
15 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />

////foo(1, "", { x: 1, y: 1 });

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent:
`foo(1, "", { x: 1, y: 1 });

function foo(arg0: number, arg1: string, arg2: { x: number; y: number; }) {
throw new Error("Function not implemented.");
}
`
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />

////const test: string = foo();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent:
`const test: string = foo();

function foo(): string {
throw new Error("Function not implemented.");
}
`
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />

////foo<string, number>();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"],
newFileContent:
`foo<string, number>();
function foo<T, U>() {
throw new Error("Function not implemented.");
}
`
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

////namespace Foo {
//// export const x = 0;
////}
////
////Foo.test();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
newFileContent:
`namespace Foo {
export const x = 0;
export function test() {
throw new Error("Function not implemented.");
}
}
Foo.test();`
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

////namespace Foo {
//// export const x = 0;
////}
////
////Foo.test();
////Foo.test();
////Foo.test();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
applyChanges: true,
newFileContent:
`namespace Foo {
export const x = 0;
export function test() {
throw new Error("Function not implemented.");
}
}
Foo.test();
Foo.test();
Foo.test();`
});

verify.not.codeFixAvailable();
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

////namespace Foo {
//// export const x = 0;
////}
////
////Foo.test(1, "", { x: 1, y: 1 });

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
newFileContent:
`namespace Foo {
export const x = 0;
export function test(arg0: number, arg1: string, arg2: { x: number; y: number; }) {
throw new Error("Function not implemented.");
}
}
Foo.test(1, "", { x: 1, y: 1 });`
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

////namespace Foo {
//// export const x = 0;
////}
////
////const test: string = Foo.test();

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
newFileContent:
`namespace Foo {
export const x = 0;
export function test(): string {
throw new Error("Function not implemented.");
}
}
const test: string = Foo.test();`
});
49 changes: 49 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingFunctionDeclaration_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference path='fourslash.ts' />

// @filename: /test.ts
////export const x = 1;

// @filename: /foo.ts
////import * as test from "./test";
////
////namespace Foo {
//// export const x = 0;
////}
////
////test.f();
////Foo.f();
////f();

goTo.file("/foo.ts");
verify.codeFixAll({
fixId: "fixMissingFunctionDeclaration",
fixAllDescription: ts.Diagnostics.Add_all_missing_function_declarations.message,
newFileContent: {
"/test.ts":
`export const x = 1;
export function f() {
throw new Error("Function not implemented.");
}
`,
"/foo.ts":
`import * as test from "./test";
namespace Foo {
export const x = 0;
export function f() {
throw new Error("Function not implemented.");
}
}
test.f();
Foo.f();
f();
function f() {
throw new Error("Function not implemented.");
}
`
}
});
16 changes: 15 additions & 1 deletion tests/cases/fourslash/codeFixAddMissingMember16.ts
Original file line number Diff line number Diff line change
@@ -6,4 +6,18 @@
////}
////Foo.test();

verify.not.codeFixAvailable();
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "test"],
applyChanges: true,
newFileContent:
`interface Foo {}
namespace Foo {
export function bar() { }
export function test() {
throw new Error("Function not implemented.");
}
}
Foo.test();`
});
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddMissingMember_all.ts
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
////En.A;

verify.codeFixAll({
fixId: "addMissingMember",
fixId: "fixMissingMember",
fixAllDescription: "Add all missing members",
newFileContent:
`class C {
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddMissingMember_all_js.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
////}

verify.codeFixAll({
fixId: "addMissingMember",
fixId: "fixMissingMember",
fixAllDescription: "Add all missing members",
newFileContent:
`class C {
1 change: 1 addition & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction7.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
////}

verify.codeFix({
index: 0,
description: "Add async modifier to containing function",
newFileContent:
`async function f() {
Original file line number Diff line number Diff line change
@@ -6,4 +6,6 @@
////declare var x: number;
////export = x;

verify.not.codeFixAvailable(); // See GH#20191
verify.codeFixAvailable([
{ description: "Add missing function declaration 'valueOf'" }
]);
Original file line number Diff line number Diff line change
@@ -18,5 +18,6 @@
//// "types": "bin/lib/libfile.d.ts"
//// }

// No fix because this accesses a nested node_modules
verify.not.codeFixAvailable();
verify.codeFixAvailable([
{ description: "Add missing function declaration 'f1'" }
]);
4 changes: 3 additions & 1 deletion tests/cases/fourslash/incompleteFunctionCallCodefix3.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,6 @@
// @noImplicitAny: true
//// function ...q) {}} f(10);

verify.not.codeFixAvailable();
verify.codeFixAvailable([
{ description: "Add missing function declaration 'f'" }
]);