Skip to content

feat(47698): Detect uncalled function statements #47719

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37156,7 +37156,7 @@ namespace ts {
function checkExpressionStatement(node: ExpressionStatement) {
// Grammar checking
checkGrammarStatementInAmbientContext(node);

checkUncalledFunction(node);
checkExpression(node.expression);
}

Expand Down Expand Up @@ -39944,6 +39944,21 @@ namespace ts {
return targetSymbol;
}

function checkUncalledFunction(node: ExpressionStatement) {
if (!strictNullChecks) return;

const expression = node.expression;
switch (expression.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression: {
if (isFunctionType(getTypeOfNode(expression))) {
error(expression, Diagnostics.This_expression_refers_to_function_Did_you_mean_to_call_it_instead);
}
}
}
}

function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) {
checkCollisionsForDeclarationName(node, node.name);
checkAliasSymbol(node);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,10 @@
"category": "Error",
"code": 2822
},
"This expression refers to function. Did you mean to call it instead?": {
"category": "Error",
"code": 2823
},
"Cannot find namespace '{0}'. Did you mean '{1}'?": {
"category": "Error",
"code": 2833
Expand Down
1 change: 1 addition & 0 deletions src/services/codefixes/fixMissingCallParentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ts.codefix {
const fixId = "fixMissingCallParentheses";
const errorCodes = [
Diagnostics.This_condition_will_always_return_true_since_this_function_is_always_defined_Did_you_mean_to_call_it_instead.code,
Diagnostics.This_expression_refers_to_function_Did_you_mean_to_call_it_instead.code,
];

registerCodeFix({
Expand Down
71 changes: 70 additions & 1 deletion tests/baselines/reference/controlFlowOptionalChain.errors.txt

Large diffs are not rendered by default.

243 changes: 243 additions & 0 deletions tests/baselines/reference/discriminantPropertyCheck.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
tests/cases/compiler/discriminantPropertyCheck.ts(94,17): error TS2823: This expression refers to function. Did you mean to call it instead?


==== tests/cases/compiler/discriminantPropertyCheck.ts (1 errors) ====
type Item = Item1 | Item2;

interface Base {
bar: boolean;
}

interface Item1 extends Base {
kind: "A";
foo: string | undefined;
baz: boolean;
qux: true;
}

interface Item2 extends Base {
kind: "B";
foo: string | undefined;
baz: boolean;
qux: false;
}

function goo1(x: Item) {
if (x.kind === "A" && x.foo !== undefined) {
x.foo.length;
}
}

function goo2(x: Item) {
if (x.foo !== undefined && x.kind === "A") {
x.foo.length; // Error, intervening discriminant guard
}
}

function foo1(x: Item) {
if (x.bar && x.foo !== undefined) {
x.foo.length;
}
}

function foo2(x: Item) {
if (x.foo !== undefined && x.bar) {
x.foo.length;
}
}

function foo3(x: Item) {
if (x.baz && x.foo !== undefined) {
x.foo.length;
}
}

function foo4(x: Item) {
if (x.foo !== undefined && x.baz) {
x.foo.length;
}
}

function foo5(x: Item) {
if (x.qux && x.foo !== undefined) {
x.foo.length;
}
}

function foo6(x: Item) {
if (x.foo !== undefined && x.qux) {
x.foo.length; // Error, intervening discriminant guard
}
}

// Repro from #27493

enum Types { Str = 1, Num = 2 }

type Instance = StrType | NumType;

interface StrType {
type: Types.Str;
value: string;
length: number;
}

interface NumType {
type: Types.Num;
value: number;
}

function func2(inst: Instance) {
while (true) {
switch (inst.type) {
case Types.Str: {
inst.value.length;
break;
}
case Types.Num: {
inst.value.toExponential;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2823: This expression refers to function. Did you mean to call it instead?
break;
}
}
}
}

// Repro from #29106

const f = (_a: string, _b: string): void => {};

interface A {
a?: string;
b?: string;
}

interface B {
a: string;
b: string;
}

type U = A | B;

const u: U = {} as any;

u.a && u.b && f(u.a, u.b);

u.b && u.a && f(u.a, u.b);

// Repro from #29012

type Additive = '+' | '-';
type Multiplicative = '*' | '/';

interface AdditiveObj {
key: Additive
}

interface MultiplicativeObj {
key: Multiplicative
}

type Obj = AdditiveObj | MultiplicativeObj

export function foo(obj: Obj) {
switch (obj.key) {
case '+': {
onlyPlus(obj.key);
return;
}
}
}

function onlyPlus(arg: '+') {
return arg;
}

// Repro from #29496

declare function never(value: never): never;

const enum BarEnum {
bar1 = 1,
bar2 = 2,
}

type UnionOfBar = TypeBar1 | TypeBar2;
type TypeBar1 = { type: BarEnum.bar1 };
type TypeBar2 = { type: BarEnum.bar2 };

function func3(value: Partial<UnionOfBar>) {
if (value.type !== undefined) {
switch (value.type) {
case BarEnum.bar1:
break;
case BarEnum.bar2:
break;
default:
never(value.type);
}
}
}

// Repro from #30557

interface TypeA {
Name: "TypeA";
Value1: "Cool stuff!";
}

interface TypeB {
Name: "TypeB";
Value2: 0;
}

type Type = TypeA | TypeB;

declare function isType(x: unknown): x is Type;

function WorksProperly(data: Type) {
if (data.Name === "TypeA") {
const value1 = data.Value1;
}
}

function DoesNotWork(data: unknown) {
if (isType(data)) {
if (data.Name === "TypeA") {
const value1 = data.Value1;
}
}
}

// Repro from #36777

type TestA = {
type: 'testA';
bananas: 3;
}

type TestB = {
type: 'testB';
apples: 5;
}

type AllTests = TestA | TestB;

type MapOfAllTests = Record<string, AllTests>;

const doTestingStuff = (mapOfTests: MapOfAllTests, ids: string[]) => {
ids.forEach(id => {
let test;
test = mapOfTests[id];
if (test.type === 'testA') {
console.log(test.bananas);
}
switch (test.type) {
case 'testA': {
console.log(test.bananas);
}
}
});
};

16 changes: 16 additions & 0 deletions tests/baselines/reference/iteratorsAndStrictNullChecks.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/compiler/iteratorsAndStrictNullChecks.ts(3,5): error TS2823: This expression refers to function. Did you mean to call it instead?


==== tests/cases/compiler/iteratorsAndStrictNullChecks.ts (1 errors) ====
// for..of
for (const x of ["a", "b"]) {
x.substring;
~~~~~~~~~~~
!!! error TS2823: This expression refers to function. Did you mean to call it instead?
}

// Spread
const xs = [1, 2, 3];
const ys = [4, 5];
xs.push(...ys);

Loading