-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Disallow comma operator with side-effect-free left operands #10814
Comments
Tweaking what is/isn't SEF:
|
Thinking about this more, I think we should define SEF to be slightly more prescriptive than an actual runtime description. For example, an array-literal is known to be SEF if its elements are SEF, but we still don't want to allow this clearly-wrong (or at least clearly-wat) code: let a = ([x++], 4); Same for let b = (x * ++y, 4); Basically, if an expression's top-level form isn't one with potential side effects, we should error, because even if there's a nested non-SEF expression, the parent expression is doing something that doesn't go anywhere, which doesn't pass the smell test. |
@RyanCavanaugh aren't these two separate issues? One is the OP issue with comma expressions, the other is about expressions whose top-level result is not used. If TypeScript disallowed (I'm assuming the guiding principle here is "statically identify constructs that are likely to be errors.") |
After looking at the PR, it seems like it's just pragmatic to only check the top-level is SEF since it simplifies SEF checking of comma expressions, has the desired effect, and may catch a few additional programmer errors as a bonus. I'd still argue for performing similar SEF checks on ExpressionStatements for consistency sake. I think you'd catch more 'constructs likely to be errors' of this kind in ExpressionStatetments than in rarely-used comma expressions. Of course |
I agree in principle that The comma operator specifically was just really compelling because of the "looks like it works but really doesn't" aspect of |
Right, it's really convenient to use naked SEF ExpressionStatements to check type inference/narrowing in test code and code under development. It would be very annoying if all of these expressions were compiler errors. Your PR uses the |
One example of a SEF ExpressionStatement I have come across is a (possibly very long) IIFE that doesn't actually get executed. Something like this: (function () {
/* IIFE code... */
}) // oops forgot to call it |
@mhegazy just showed me a great hit from our partner suite 😆
|
Could this be allowed for things like |
Just saw #12978 (comment), guess that's fine as a workaround. |
It's easy to write code like this (#10802)
Or this
Or this:
We should disallow left comma operands when they are side-effect free. An expression is side-effect free (SEF) if it is:
The text was updated successfully, but these errors were encountered: