4

I use the examples from the CoffeeScript homepage and it doesn't validate.

The for loop one is a perfect example, if you use the coffeescript statement it doesn't wrap the body in an if statement.

  • Expected '{' and instead saw 'child'.
  • Possible strict violation.
  • A constructor name should start with an uppercase letter.
  • 'insertclassnamehere' is already defined.
  • Did you mean to return a conditional instead of an assignment?
  • Expected '===' and instead saw '=='.
  • Unexpected '~'.
  • Expected '!==' and instead saw '!='.
  • The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.
sam
  • 40,318
  • 2
  • 41
  • 37
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • 1
    From their site, "JSHint is a tool to detect errors and potential problems in ***JavaScript*** code." JavaScript is not CoffeeScript. They are *completely* different languages. Also, your question should at least include the sample of code you're trying to validate. How are we supposed to give you any useful information without at least that? – user229044 Oct 02 '12 at 05:06
  • 4
    I'm not trying to validate coffeescript, i'm validating the resulting javascript .. and like I said .. the code samples from their website. – Kirk Strobeck Oct 02 '12 at 05:46
  • In that case, JSHint is not a "validator". It is a linter, and linting compiled CoffeeScript is pointless. It's not important or particularly desirable for it to yield no warnings from JSHint. – user229044 Oct 02 '12 at 05:47
  • An if statement on the for loop is best practice, i want to make my compiled coffeescript adhere to those standards – Kirk Strobeck Oct 02 '12 at 05:49
  • linter == debugger == validator – Kirk Strobeck Oct 02 '12 at 05:49
  • Linter != debugger. They are completely different things. *Completely* different. And a linter is no more a validator than a dubugger is a linter. They are different words with different meanings that represent different concepts. And you can't "make" your compiled CoffeeScript do anything. It's essentially none of your business how CoffeeScript compiles, and they're free to change it at any point. What you want is a CoffeeScript linter. – user229044 Oct 02 '12 at 05:50
  • There *is* no answer. Compiled CoffeeScript doesn't lint well because that's not important, and the authors of CoffeeScript expended no effort trying to make their output pass JSHint's linting. If you want it to, your only solution is to *write your own CoffeeScript compiler*. – user229044 Oct 02 '12 at 05:53
  • see jashkenas response to https://github.com/jashkenas/coffee-script/issues/2500 – Kinjal Dixit Oct 13 '12 at 06:56
  • this a perfectly valid question, imho. I want to debug the js coffeescript produces. So I want to check it validates. – NimChimpsky May 14 '14 at 08:40

1 Answers1

8

My compiled CoffeeScript won't validate in JShint .. why?

The short answer would be: Because the creators of the CoffeeScript compiler didn't deem it necessary.

It makes sense to lint code which is written and maintained by developers. It avoids human errors by making code readable.

The code generated by a compiler on the other hand has completely different requirements. Readability is usually not a concern. It's more important that the code is efficient and small.

If you really want this then you need to modify the CoffeeScript compiler source.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Christoph Leiter
  • 9,147
  • 4
  • 29
  • 37
  • "It avoids human errors by making code readable." not if you normally write code that validates in jslint – NimChimpsky May 14 '14 at 08:40
  • 2
    Coffeescript compilation and linting should be seen as complementary. Coffeescript itself goes a long way to helping write maintainable JS which avoids bad JS syntax and practices. But still, when writing Coffeescript I do get bitten by bugs that JS wtih JSHint would have avoided (like misspelt variable references etc). Appreciate coffeescript does now have some linting capabilities. – arcseldon Aug 01 '14 at 22:45