js2-mode icon indicating copy to clipboard operation
js2-mode copied to clipboard

Missing warning for: ReferenceError: variable is not defined

Open knobo opened this issue 6 years ago • 9 comments

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 avatar Mar 07 '19 12:03 knobo

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

knobo avatar Jul 11 '19 10:07 knobo

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

dgutov avatar Jul 26 '19 23:07 dgutov

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

lelit avatar Jul 27 '19 04:07 lelit

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.

lelit avatar Aug 05 '19 06:08 lelit

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

knobo avatar Aug 05 '19 06:08 knobo

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();

lelit avatar Aug 05 '19 08:08 lelit

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 avatar Aug 05 '19 09:08 knobo

Yes that's right.. #515

knobo avatar Aug 05 '19 09:08 knobo

This one is related:

image

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

knobo avatar Aug 22 '19 09:08 knobo