Skip to content

feat(33792): Add quick fix for `This condition will always return true since the function is always defined. Did you mean to call it instead #37152

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

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5373,6 +5373,14 @@
"category": "Message",
"code": 95066
},
"Add missing call parentheses": {
"category": "Message",
"code": 95067
},
"Add all missing call parentheses": {
"category": "Message",
"code": 95068
},
"Add 'unknown' conversion for non-overlapping types": {
"category": "Message",
"code": 95069
Expand Down
45 changes: 45 additions & 0 deletions src/services/codefixes/fixMissingCallParentheses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixMissingCallParentheses";
const errorCodes = [
Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead.code,
];

registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const { sourceFile, span } = context;
const callName = getCallName(sourceFile, span.start);
if (!callName) return;

const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, callName));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_call_parentheses, fixId, Diagnostics.Add_all_missing_call_parentheses)];
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const callName = getCallName(diag.file, diag.start);
if (callName) doChange(changes, diag.file, callName);
})
});

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, name: Identifier | PrivateIdentifier): void {
changes.replaceNodeWithText(sourceFile, name, `${ name.text }()`);
}

function getCallName(sourceFile: SourceFile, start: number): Identifier | PrivateIdentifier | undefined {
const token = getTokenAtPosition(sourceFile, start);
if (isPropertyAccessExpression(token.parent)) {
let current: PropertyAccessExpression = token.parent;
while (isPropertyAccessExpression(current.parent)) {
current = current.parent;
}
return current.name;
}

if (isIdentifier(token)) {
return token;
}

return undefined;
}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"codefixes/fixUnreachableCode.ts",
"codefixes/fixUnusedLabel.ts",
"codefixes/fixJSDocTypes.ts",
"codefixes/fixMissingCallParentheses.ts",
"codefixes/fixAwaitInSyncFunction.ts",
"codefixes/disableJsDiagnostics.ts",
"codefixes/helpers.ts",
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo(fn: () => boolean) {
//// fn/**/ ? console.log('test') : undefined;
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo(fn: () => boolean) {
fn() ? console.log('test') : undefined;
}`,
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////class Foo {
//// #test = () => true;
//// run() {
//// if (this.#test/**/) {
//// console.log('test')
//// }
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`class Foo {
#test = () => true;
run() {
if (this.#test()) {
console.log('test')
}
}
}`,
});
55 changes: 55 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////class Foo {
//// #test = () => true;
//// run() {
//// if (this.#test) {
//// console.log('test')
//// }
//// }
////}
////
////function foo() {
//// function test() { return Math.random() > 0.5; }
//// test ? console.log('test') : undefined;
////}
////
////function foo() {
//// const x = {
//// foo: {
//// bar() { return true; }
//// }
//// }
//// x.foo.bar ? console.log('test') : undefined;
//// if (x.foo.bar) {}
////}

verify.codeFixAll({
fixAllDescription: ts.Diagnostics.Add_all_missing_call_parentheses.message,
fixId: "fixMissingCallParentheses",
newFileContent:
`class Foo {
#test = () => true;
run() {
if (this.#test()) {
console.log('test')
}
}
}

function foo() {
function test() { return Math.random() > 0.5; }
test() ? console.log('test') : undefined;
}

function foo() {
const x = {
foo: {
bar() { return true; }
}
}
x.foo.bar() ? console.log('test') : undefined;
if (x.foo.bar()) {}
}`,
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo() {
//// function test() { return Math.random() > 0.5; }
//// test/**/ ? console.log('test') : undefined;
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo() {
function test() { return Math.random() > 0.5; }
test() ? console.log('test') : undefined;
}`,
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo() {
//// const x = {
//// foo: {
//// bar() { return true; }
//// }
//// }
//// x.foo.bar/**/ ? console.log('test') : undefined;
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo() {
const x = {
foo: {
bar() { return true; }
}
}
x.foo.bar() ? console.log('test') : undefined;
}`,
});
30 changes: 30 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////class Foo {
//// test() {
//// return true;
//// }
//// run() {
//// this.test/**/ ? console.log('test') : undefined;
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`class Foo {
test() {
return true;
}
run() {
this.test() ? console.log('test') : undefined;
}
}`,
});

25 changes: 25 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////class Foo {
//// #test = () => true;
//// run() {
//// this.#test/**/ ? console.log('test') : undefined;
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`class Foo {
#test = () => true;
run() {
this.#test() ? console.log('test') : undefined;
}
}`,
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo(fn: () => boolean) {
//// if (fn/**/) {
//// console.log('test');
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo(fn: () => boolean) {
if (fn()) {
console.log('test');
}
}`,
});
25 changes: 25 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo() {
//// function test() { return Math.random() > 0.5; }
//// if (test/**/) {
//// console.log('test')
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo() {
function test() { return Math.random() > 0.5; }
if (test()) {
console.log('test')
}
}`,
});
33 changes: 33 additions & 0 deletions tests/cases/fourslash/codeFixMissingCallParentheses8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////function foo() {
//// const x = {
//// foo: {
//// bar() { return true; }
//// }
//// }
//// if (x.foo.bar/**/) {
//// console.log('test')
//// }
////}

verify.codeFixAvailable([
{ description: ts.Diagnostics.Add_missing_call_parentheses.message }
]);

verify.codeFix({
description: ts.Diagnostics.Add_missing_call_parentheses.message,
index: 0,
newFileContent:
`function foo() {
const x = {
foo: {
bar() { return true; }
}
}
if (x.foo.bar()) {
console.log('test')
}
}`,
});
Loading