Ignore camelcase variable in JSHint

JavascriptCoding StyleJshintCode Cleanup

Javascript Problem Overview


Having a bit of an issue with JShint and the following line of code.

$location.path('map-' + map.id + '/venue-' + map.attributes.default_venue.value);

I'm getting the error, Identifier 'default_venue' is not in camel case. This wouldn't be a problem normally but I don't have any control over the variable name - it's brought in via a JSON API.

Is there any way I could suppress this issue for either the affected variables or on the lines in which they appear?

Apologies if this has been asked before, I'm pretty sure it must have been but I can't find a solution.

Javascript Solutions


Solution 1 - Javascript

JSHint obeys directives at a function level, so you can find the enclosing function and add a camelcase option to it. Here's an example:

/*jshint camelcase: true */

var not_camel_case = 1; // Warns

function example() {
  /*jshint camelcase: false */
  var not_camel_case = 2; // Does not warn
}

Solution 2 - Javascript

According to the JSHint Docs, you can make a config file in the same directory called .jshintrc, or any directory all the way to your root directory. I just set mine using this:

  {
    "camelcase": false
  }

There are loads of other options here: http://jshint.com/docs/options/#camelcase

Solution 3 - Javascript

I put the name of the property coming from the api in a separate string. E.g.:

var defaultVenueAttributeKey = 'default_venue';
$location.path('map-' + map.id + '/venue-' + map.attributes[defaultVenueAttributeKey].value);

It's a bit more verbose but you could group all property names coming from your API together and then it makes responding to the API changing easier.

Solution 4 - Javascript

The accepted answer /*jshint camelcase: true */ didn't work for me. I was still getting the errors.

I looks at the docs and found this solution that worked for me:

/*eslint camelcase: ["error", {properties: "never"}]*/

Solution 5 - Javascript

Try out something like this.. Though wicked, it will work.

var foo;
$.each( jsonArray, function ( i, value ) {
    if ( i === 'array_element' ) {
        foo = value;
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSam BeckhamView Question on Stackoverflow
Solution 1 - JavascriptJames AllardiceView Answer on Stackoverflow
Solution 2 - JavascriptryanaboothView Answer on Stackoverflow
Solution 3 - JavascriptsamView Answer on Stackoverflow
Solution 4 - JavascriptAryeh BeitzView Answer on Stackoverflow
Solution 5 - JavascriptinchikuttyView Answer on Stackoverflow