Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List outer scoped variables of W083 #3211

Closed
flaviojs opened this issue Nov 15, 2017 · 3 comments · May be fixed by ajesse11x/cjdns#1 or ajesse11x/amplify-js#5
Closed

List outer scoped variables of W083 #3211

flaviojs opened this issue Nov 15, 2017 · 3 comments · May be fixed by ajesse11x/cjdns#1 or ajesse11x/amplify-js#5

Comments

@flaviojs
Copy link
Contributor

flaviojs commented Nov 15, 2017

I was getting W083 with this code:

$.ajax({
  contentType: "application/json; charset=utf-8",
  type: "GET",
  url: api("/machines"),
  beforeSend: function() { $('#machines-spinner').removeClass('hidden'); },
  cache: false,
}).done(function (machines) {
  if (machines.length > 0) {
    var $machines = $('#machines').html('');
    for (var i = 0; i < machines.length; ++i) {
      var html = $('#template-machine').html().replace(/{{\w*}}/g, function (x) { // W083
        if (x === '{{id}}') return machines[i].id;
        if (x === '{{name}}') return machines[i].name;
        if (x === '{{targetoee}}') return machines[i].targetoee;
        if (x === '{{code}}') return machines[i].code;
        return x;
      });
      $machines.append($(html));
    }
  }
  else {
    $('#machines').text('No data.');
  }
  $('#machines-spinner').addClass('hidden');
}).fail(...);

I tried wrapping the code but it didn't remove the error:

...
      (function (machines, i, $machines) { // W083
        var html = $('#template-machine').html().replace(/{{\w*}}/g, function (x) {
          if (x === '{{id}}') return machines[i].id;
          if (x === '{{name}}') return machines[i].name
          if (x === '{{targetoee}}') return machines[i].targetoee
          if (x === '{{code}}') return machines[i].code;
          return x;
        });
        $machines.append($(html));
      })(machines, i, $machines);
...

Not knowing what else to try I gave up and added the loopfunc option.

I still don't know which "outer scoped variable" he was referring to.
I'd like jshint to list the offending outer scoped variable(s) with W086, even if it's only in --verbose mode.

@jugglinmike
Copy link
Member

Hi there, Flávio, thanks for the report! The problem is the reference to the $ function. There are two ways to resolve this:

  1. Include $ in the parameters of the immediately-invoked function expression (and the corresponding argument list)
  2. Configure JSHint to recognize $ as an immutable global binding (i.e. $: false). JSHint should correctly recognize that there is no risk for confusion when the reference cannot be modified

That said, I agree that debugging this warning is kind of difficult without the list of bindings. Implementing your request shouldn't require very much code. I'd be happy to help you write a patch--do you have any interest?

@flaviojs
Copy link
Contributor Author

flaviojs commented Nov 20, 2017

I was expecting the jquery option to handle $ since jQuery defines it, I was wrong.

I'll try to make a pull request.

flaviojs added a commit to flaviojs/tmp-jshint that referenced this issue Nov 20, 2017
Debugging W083 is kind of difficult without knowing which outer
scoped variables it is refering to. This commit shows the variable
names inside parenthesis at the end of the warning message.

References:

    Closes jshintGH-3211
flaviojs added a commit to flaviojs/tmp-jshint that referenced this issue Nov 20, 2017
Debugging W083 is kind of difficult without knowing which outer
scoped variables it is referring to. This commit shows the variable
names inside parenthesis at the end of the warning message.

References:

    Closes jshintGH-3211
@flaviojs
Copy link
Contributor Author

Note that the current master (a11d631) doesn't complain about $ with the jquery option, but the current jshint in npm does (v2.9.5).

flaviojs added a commit to flaviojs/tmp-jshint that referenced this issue Nov 23, 2017
Debugging W083 is kind of difficult without knowing which outer
scoped variables it is referring to. This commit shows the variable
names inside parenthesis at the end of the warning message.

References:

    Closes jshintGH-3211
jugglinmike pushed a commit to jugglinmike/jshint that referenced this issue Jan 16, 2018
Debugging W083 is kind of difficult without knowing which outer
scoped variables it is referring to. This commit shows the variable
names inside parenthesis at the end of the warning message.

References:

    Closes jshintGH-3211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants