-
Notifications
You must be signed in to change notification settings - Fork 18
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 ExpressionStatement and SequenceExpression. #4
Comments
I agree this is a worthwhile addition to the rules. |
I really like this idea for pushing code to functional style. Off the top of my head, we'd need a few key exceptions to make it reasonable:
I might just have to implement it, start using it, then see what exceptions I'd want to put in place... |
I agree that there are certain exceptions to this rule, but the whole point of pushing code to functional style is to eliminate side-effects. 😄 In my opinion, the only exception would be using export const add = (a, b) => (
invariant(typeof a === 'number', 'a must be a number'),
invariant(typeof b === 'number', 'b must be a number'),
a + b
) But for a program to function, it needs side effects, right? I’d put all the imperative, side-effecting code outside the core of the application, where this rule is disabled. While the core of my application (with main logic) would all be functional and without side-effects. This idea is called “Functional Core, Imperative Shell.”. In fact, Haskell enforces this idea at a type level. All side effecting function has type For debugging via console, you can create a trace function. (This file must have the rule turned off). /* eslint-disable no-impure-code */
export function trace (...args) {
return (value) => {
console.log(...args, value)
return value
}
} Then you can add export const add = (a, b) => (
trace('a =')(a) + trace('b =', b)
) This breaks the purity rule, but when you’re debugging, anything goes. Haskell also contains the same |
As long as the documentation recommends something like installing it for only part of your application, then it could be reasonable with no exceptions. It's not a standard use of Also, where's the PR? :0) |
No one implemented it yet I guess… So I just pushed it as a separate plugin in order to experiment more quickly: I’m going to test this rule, along with other rules from this repo, with some of my personal projects and work projects. |
Alternate title: Disallow function calls whose return values are discarded.
Based on this blog post, when you call a function and don’t use it’s return value, chances are high that it is being called for its side effect. e.g.
Thinking it further, I found this to be an expansion of
no-unused-expressions
rule, but with every ExpressionStatement disallowed, even if it only contains a CallExpression.Disallowing SequenceExpression may also make sense because there’s no point in evaluating
a
,b
in(a, b, c)
except for the side-effects caused by evaluatinga
andb
.I think this can also potentially fix #2.
As a result, every expression that gets evaluated must either go into a constant or returned. If we also ensure that there are
no-unused-vars
, we can get a pure-functional-language-like semantic.The text was updated successfully, but these errors were encountered: