66

Is there an option to and/or how do I suppress errors like the following?

175,14:['tracker'] is better written in dot notation.

Machavity
  • 30,841
  • 27
  • 92
  • 100
TomFuertes
  • 7,150
  • 5
  • 35
  • 49

3 Answers3

153

If it's a feature and not a bug, place this at the top of your file.

/*jshint sub:true*/

If it's a bug, you should refactor your code

foo['tracker'] = bar // from this...
foo.tracker = bar;   // to this!

Good post on the reasons here: https://stackoverflow.com/a/2001410/94668

As suggested by: @ThorSummoner you can use below in your .jshintrc file

"sub": true
Neeraj Gulia
  • 640
  • 8
  • 24
TomFuertes
  • 7,150
  • 5
  • 35
  • 49
  • 13
    This always bugged me cause the only way to reference elements in an associative array whose keys are reserved keywords is to use the bracket style. It'd be nice if jsHint could check the value in the brackets and not generate the warning if it's a JS keyword. Otherwise, jsLint ends up telling me to write something in dot notation that would cause a syntax error :P – reblace Oct 25 '13 at 03:53
  • @reblace JSHint (note, *not the same as jsLint*) [supposedly does exactly that](http://jslinterrors.com/a-is-better-written-in-dot-notation). – Carl G Jun 03 '14 at 22:02
  • 15
    that's `"sub": true,` in your .jshintrc file – ThorSummoner Feb 23 '15 at 23:16
  • 4
    I hoped for the linked "good post" to give me a reason, why I should prefer the dot notation over the bracket notation. The only reason the post gives, is that JSLint's author decided so. To me it's still just a matter of preference and not correctness, so why are JSLint/JSHint marking it as warning? – whY Mar 30 '17 at 06:36
  • 1
    @whY I prefer dot notation as you can hover over the property while debugging to see the value while with bracket notation debugger cant evaluate it on hover. – Amitesh Aug 04 '17 at 06:31
  • It doesn't work when adding a new key to JSON object. Like, `let obj = {"name":"okay", "address":"India"};` // now adding a new property to this object, won't work. `obj.gender = "M";` // while it works ...`obj["gender"] = "M"` – Ram Apr 23 '20 at 06:04
16

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W069.

This means you can tell JSHint to not issue this warning with the /*jshint -W069 */ directive.

You can even wrap several lines of code and then reenable the warning as the example below (with a note to future you why it was a good idea):

/*jshint -W069 */
/*Disable Warning Justification:
    Using bracket notation so Google Closure Compiler 
    ADVANCED_OPTIMIZATIONS will keep the original property names. */
obj['prop1'] ='foo';
obj['prop2'] ='bar';
/*jshint +W069 */
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
1

I assume you are asking about Dreamweaver or another editor.

Dreamweaver

You may go to Edit -> Preferences -> Linting

Select JS in the dropdown and hit Edit & Apply changes

Find

 "sub": false,

and change that to true. Save the file and that notice will disappear.

If you have OTHER Linting things you wish to edit, you can find a helpful list of them all at https://jshint.com/docs/options/

Case
  • 4,244
  • 5
  • 35
  • 53