one-var rule: separate option for function expression assignments?
See original GitHub issueRule: http://eslint.org/docs/rules/one-var.html
What version of ESLint are you using? 2.11.1
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"one-var": [
"error",
"always",
],
"space-infix-ops": [
"error",
{ "int32Hint": false }
],
"template-curly-spacing": [
"warn",
"never"
],
}
};
What did you do? Please include the actual source code causing the issue.
I have a number of const
definitions, particularly at the top-level of the script but also within functions, many of which are function expression assignments, eg:
const
foo,
bar = 1
const baz = function() {
// ...
}
const quux = (meh) => {
// ...
}
If I use the uninitialized
and initialized
options object, I’m forced to either give bar
it’s own const
declaration, or merge all the function expr assignments in to a single const
declaration. The same issue arises with the var
, let
, const
options object - there’s no way to treat function expr assignments differently.
This is bearable if there are only a few trivial (bar
-like) assignments, but regularly at the top-level of the script I will have dozens of them. To have my function expr assignments require individual declarations, it means I also have to have dozens of trivial assignments require individual declarations …that makes the start of scripts look very cluttered due to excess declaration keywords.
There doesn’t seem to be a way to set it up so that it’s:
"always"
(or"never"
) for everything, except a function expression assignment.
What did you [want] to happen?
Ideally I’d like an extra setting in the var
, let
, const
options object: functionExpr
I could then do something like this:
// ...
"rules": {
"one-var": [
"error",
{
"var": "always",
"let": "always",
"const": "always",
"functionExpr": "never", // new option
},
],
// ...
The functionExpr
option could also be applicable to the uninitialized
and initialized
options object, but primarily it would be most useful in the var
, const
, let
options object.
A better approach might be to have "always-except-fn"
and "never-except-fn"
values for the var
, let
and const
options? That would allow something like:
"one-var": [
"error",
{
"var": "always",
"let": "always",
"const": "always-except-fn", // new option value
},
],
What actually happened? Please include the actual, raw output from ESLint.
The rule is working as advertised, for every combination of options, it’s just that I want an extra option to handle assignments of function expressions differently without affecting other, more trivial assignments (like the bar
example earlier).
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
The more I think about it, it seems what is actually desirable is a
"ignore"
option, so I can do this:In the options above, the rule would not apply to
const
, but will still apply tovar
andlet
.Agree with https://github.com/eslint/eslint/issues/6433#issuecomment-226808127 that the best benefits of this rule are for ES5 code with
var
declarations:To the (admittedly limited) extent that I have studied ES6 code with a maximum of
const
declarations, the configuration seems more like: one-var: [“error”, “never”] // to simplify diffs when declarations are added or deleted no-use-before-define: “error”