mooz / js2-mode

Improved JavaScript editing mode for GNU Emacs
GNU General Public License v3.0
1.33k stars 186 forks source link

Missing warning for: ReferenceError: variable is not defined #516

Open knobo opened 5 years ago

knobo commented 5 years ago

js2-mode should warn about "unused var0" and "var1 is not defined".

function missingWarning (var0) {
  const var1 = {var1};
  console.log("Var1:", var1);
}
missingWarning();
$ node ./missing-warning.jsx 
/tmp/missing-warning.jsx:82
  const var1 = {var1};
                ^

ReferenceError: var1 is not defined
knobo commented 5 years ago

And this one...

function destructme ({foo: bar}) {
  console.log('Foo contains: ', bar);
}
line 4: Variable 'bar' referenced but never initialized
line 5: Variable 'bar' referenced but never initialized

Maybe it should be a separate issue? image

dgutov commented 5 years ago

@lelit It would be great if you could take a look.

lelit commented 5 years ago

Will do, and sorry for not noticing this kind of reports!

lelit commented 5 years ago

WRT the unused parameter, it's a deliberate behavior, given how often you write a function that does not consume all its arguments. Maybe there could be an opt-in/opt-out option to select a different reaction.

On the undefined var, it's indeed a general defect I will try to address, as it manifests itself even in a simpler case:

function foo() {
  var x = x + 1;
}

I seem to remember that the logic behind the check is "was this variable declared in var-block before this point?"... it should probably add the check "... and, if this is a declaration, a DIFFERENT var-block" or something like that.

knobo commented 5 years ago

About the unused parameter. It is in fact used, so why should it produce a warning?

image

This variable is destructured and given a new name, as documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names

lelit commented 5 years ago

I was talking about the comment

js2-mode should warn about "unused var0" and "var1 is not defined".

in your first example:

function missingWarning (var0) {
  const var1 = {var1};
  console.log("Var1:", var1);
}
missingWarning();
knobo commented 5 years ago

Aha! My mistake. I suspect I did not get a warning about var0 in my editor when I created the example. I'm not sure but that warning might have been implemented after I created my example.

knobo commented 5 years ago

Yes that's right.. #515

knobo commented 5 years ago

This one is related:

image

  if (changed) {
    for (let [path, value] of Object.entries(changed)) {
      updated = _.set(path, value, updated);
    }
  }