question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

one-var rule: separate option for function expression assignments?

See original GitHub issue

Rule: 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:closed
  • Created 7 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
aubergine10commented, Jun 21, 2016

The more I think about it, it seems what is actually desirable is a "ignore" option, so I can do this:

    "one-var": [
      "error",
     {
       "var": "always",
       "let": "always",
       "const": "ignore", // new option value
     },
    ],

In the options above, the rule would not apply to const, but will still apply to var and let.

1reaction
pedrottimarkcommented, Jun 20, 2016

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:

The single-declaration school of thought is based in pre-ECMAScript 6 behaviors, where there was no such thing as block scope, only function scope. Since all var statements are hoisted to the top of the function anyway

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”

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - var functionName = function() {} vs function ...
The difference is that functionOne is a function expression and so only defined when that line is reached, whereas functionTwo is a function...
Read more >
one-var - ESLint - Pluggable JavaScript Linter
This rule enforces variables to be declared either together or separately per function ( for var ) or block (for let and const...
Read more >
Function expressions - The Modern JavaScript Tutorial
Functions are values. They can be assigned, copied or declared in any place of the code. If the function is declared as a...
Read more >
List of available rules - ESLint - Pluggable JavaScript linter
Rules. Rules in ESLint are grouped by category to help you understand their purpose. No rules are enabled by default. The "extends": "eslint:recommended" ......
Read more >
Function expression - JavaScript - MDN Web Docs
The name doesn't change if it's assigned to a different variable. If function name is omitted, it will be the variable name (implicit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found