-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conditional assignments used for side-effects trigger "never used" #28
Comments
Thanks for reporting this. IMO, this doesn't require a behavioral change as I don't consider it a bug (see #10) but I did update the unused-var error message to be more descriptive and hopefully cause less confusion. As of 0.4.2 (just released), your code will produce: To fix the error, you need to actually do something with cache: cache = null
work = ->
console.error 'doing work'
42
fn = ->
cache ?= work()
return cache
console.log fn()
console.log fn() Because in this case the two samples are functionally equivalent (the "return cache" statement is implicit), there's some debate to be had here about what does a) Return the value of cache after (conditionally) assigning "value" The generated javascript code for conditional assignments is somewhere in between: return cache != null ? cache : cache = "value" coffeescope2 assumes that the result of an assignment is the value of the assignment, thus it does not consider it as reading the assigned variable. This is to catch cases like this: foo = bar = baz = "qux"
console.log(foo)
# bar is never read
console.log(baz) If you have strong feelings the other way, please reopen and I'll put this (better handling of implicit returns) on the roadmap for 1.0 |
I don't know about strong feelings, but given the implicit return is what this code was aiming for (this was a real example that came up in my code, btw), and given that FWIW, great tool - coffeelint without coffeescope2 is sadly lacking :-) |
Anything bigger than 'meh' is a strong feeling in my book :) Reopening |
Keep in mind, this problem isn't specific to implicit returns; it's any use of the "return value" of any assignment-like: We had some code that looked like: module.exports = descriptiveFnName = -> whatever Where the name in the middle gets you better error traces. I had to convert that code to: descriptiveFnName = -> whatever
module.exports = descriptiveFnName ...which is debatably better code, but seems like a silly exercise in tricking the linter. |
There's definitely an exception to be made for I also agree on the handling of This is a longer discussion and one that I'm happy to have under normal circumstances; unfortunately, right now is not a good time for me to think about this stuff as I'm a bit stretched thin with other things. I do however want it on the record that I have no intention of abandoning or even just keeping this project on life support: all open issues will be fixed, it's just a matter of when I find the time to do it. Thanks for your report and thoughts! All feedback is appreciated as it helps me better understand how coffeescript is used in the wild (when I started this project, I figured most people just wrote python-like code, which is exactly why this issue has been overlooked: it's nowhere near to what the python equivalent would be). |
Closing this in the context of #43 |
If you're using the return value of
x ?= y()
for cheap memoization, that triggers "Variable ... is never used"Code sample
Output / stack trace
coffeelint.json
The text was updated successfully, but these errors were encountered: