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.

Make vars-on-top work with let and const

See original GitHub issue

Right now this gives no warning:

function foo(bar) {
  if(bar) {
    doSomething();
  }
  const baz = "bar";
  return baz;
}
foo();

given this config:

{
  "rules": {
    "strict": 0,
    "vars-on-top": 2
  },
  "ecmaFeatures": {"blockBindings": true},
  "globals": {
    "doSomething": false
  }
}

(Note, it does warn if I include a regular var in there):

function foo(bar) {
  if(bar) {
    doSomething();
  }
  const baz = "bar";
  var foobar = "eggs";
  return {baz: baz, foobar: foobar};
}
foo();

I would expect that it should warn in the first case.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
localpcguycommented, Apr 11, 2017

Was a plugin for this ever made? I agree that it should be block scoped, and fine with it being off by default, but it’d be nice to be able to enforce this from a stylistic aspect.

1reaction
kentcdoddscommented, Jun 27, 2015

Though, perhaps I should leave this open to add an option for stylistic reasons. Anyone think that would be handy? I’d definitely use it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Var, Let, and Const – What's the Difference? - freeCodeCamp
Scope essentially means where these variables are available for use. var declarations are globally scoped or function/locally scoped. The scope ...
Read more >
let, var, and const - Go Make Things
ES6 introduced two new ways to define variables: let and const. ... This works because it's a different scope let chips = 'Wise'; ......
Read more >
ES6 In Depth: let and const - the Web developer blog
I like to imagine the JS engine lifting each var and function to the top of the enclosing function with a tiny code...
Read more >
Hoisting in Modern JavaScript — let, const, and var
Many JavaScript programmers explain hoisting as JavaScript's behavior of moving declarations (variable and function) to the top of their ...
Read more >
Are variables declared with let or const hoisted?
Is there any difference between let and const in this matter? ... 1: var declarations are still working only on the function level, ......
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