Skip to content

Report error for common no-op statements #5669

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
elisee opened this issue Nov 14, 2015 · 5 comments
Closed

Report error for common no-op statements #5669

elisee opened this issue Nov 14, 2015 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints

Comments

@elisee
Copy link

elisee commented Nov 14, 2015

Assuming the following code:

function doSomething() {
  alert("Did something");
}

doSomething;

Would it be possible for TypeScript to complain about the doSomething; line, notifying the user that the statement is a no-op and maybe even suggesting adding parentheses to call the function? Or do you folks consider that kind of problems to definitely be a linter error, rather than something the compiler should look out for?

I searched the bugtracker but couldn't find any similar requests, my apologies if I missed them. I don't even know how feasible this would be, maybe ECMAScript has intricacies that make it impossible in all but the simplest cases, but I thought it couldn't hurt to suggest it.

@elisee elisee changed the title Report error for commoon no-op statements Report error for common no-op statements Nov 14, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Nov 14, 2015

What if doSomething was implemented as a getter?

this looks like a tslinlt rule to me.

@DanielRosenwasser
Copy link
Member

doSomething in this context can't be a getter though because it's a standalone identifier. This would only be a problem as a property/element access.

@DanielRosenwasser
Copy link
Member

Though I'm leaning towards this being a linter rule like @mhegazy says. For this purpose, I think tslint's no-unused-expression would be the rule you're looking for @elisee.

@elisee
Copy link
Author

elisee commented Nov 14, 2015

I expect the vast majority of getters to have no side-effects in pratice and as such, they don't make sense as standalone expressions either (whether this is too a big assumption to make in the compiler or not, I don't know.)

Thanks for the pointers to tslint, I started looking at it right after posting this and it's a nifty tool indeed.

Still thinking it would be nice to prevent this kind of footgun at the language level, similarly to #2575 and the upcoming reachability analysis, but I'll defer to you. Please close if you still think it doesn't belong in the language.

@DanielRosenwasser
Copy link
Member

So one reason a person might use a standalone identifier as a statement expression is to ensure that we don't perform module import elision. Basically, if we never use any value entity from a module, then we assume it was only used for types. This means people end up needing to do something like

import * as foo from "fooModule";
import "fooModule";

However, another reasonable way to trigger this would be

import * as foo from "fooModule";
foo;

So I'm going to lean on the side that this is more in the scope of linters.

@DanielRosenwasser DanielRosenwasser added Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints labels Nov 14, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints
Projects
None yet
Development

No branches or pull requests

3 participants