You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Detect uncalled functions that appear in void expressionsΒ #47699
Today TS can detect uncalled functions that appear in conditionals. I propose also extending this to detect uncalled functions that appear an an expression where the result is unused
π Motivating Example
exportinterfaceFoo{doImportantStuff(): void;}declareconstfoo: Foo;if(foo.doImportantStuff){// This one is detected
...
}[foo].forEach(x=>foo.doImportantStuff);// Mistake here. The user forgot to call `doImportantStuff`
π» Use Cases
This was inspired by a real world bug in the VS Code codebase. The case with forEach shows the problem we ran into
Off the top of my head, I don't think there would be false positives but may need some more thought to make sure
This specific example isn't detectable in practice. We have a function isSideEffectFree that we used in other positions like the comma-sequence operator. It explicitly doesn't consider a.b side-effect free because JS getters can and do have side effects, and we don't know from the declaration of Foo that doImportantStuff isn't a getter.
It still does seem a bit inconsistent that if (a.b) {}
is detected as a "forgot-to-call" error, while the similar construction [ a ].forEach(a => a.b)
isn't.
Activity
RyanCavanaugh commentedon Feb 2, 2022
This specific example isn't detectable in practice. We have a function
isSideEffectFreethat we used in other positions like the comma-sequence operator. It explicitly doesn't considera.bside-effect free because JS getters can and do have side effects, and we don't know from the declaration ofFoothatdoImportantStuffisn't a getter.fatcerberus commentedon Feb 2, 2022
It still does seem a bit inconsistent that
if (a.b) {}is detected as a "forgot-to-call" error, while the similar construction
[ a ].forEach(a => a.b)isn't.
mjbvz commentedon Feb 2, 2022
Yes getters make this difficult to reason about. Just as a code example of the problem:
Famous last words, but the above does seem uncommon though. Would be interesting to see how often it appears in real world code