jshint / jshint

JSHint is a tool that helps to detect errors and potential problems in your JavaScript code
http://jshint.com
MIT License
8.99k stars 1.65k forks source link

JSON detection heuristic is too naive #2899

Open dustContributor opened 8 years ago

dustContributor commented 8 years ago

Hi! I'm using jshint through this Atom linter https://atom.io/packages/linter-jshint

I get "Error: Expected a JSON value" on code like this:

{
  const id = createEntity()
  // Do other stuff
}

And a few other related errors "Expected a string and instead saw const" and so on. I'm just trying to add a block scope so not to mess up the global scope (I'm runing the JS code on Nashorn/JVM btw). Code works as intended.

My config file is quite simple:

{
  "esversion" : 6,
  "asi" : true,
  "unused" : "strict"
}

Is this error appropriate in this context?

jugglinmike commented 8 years ago

Thanks for the report! This is definitely a bug. It results from the mechanism JSHint uses to determine if the input should be interpreted as JSON or as JavaScript code. It guessed incorrectly in your case, but there is nothing particularly ambiguous about the input you shared.

I've updated the title of this report to describe the issue a little more directly. Here are a few ideas for temporary workarounds that will fix the issue for you today:

These are clearly sub-optimal solutions, but I hope they'll do in a pinch.

dustContributor commented 8 years ago

Yay, found a bug \o/ :smile: I see, adding a semicolon gets it to recognize it as normal JS. I hope its gets fixed, thanks for your time.