简体   繁体   中英

Generated Javascript from Coffeescript is not lint ready? -> Throws Lint Error in Yeoman / Grunt

at the moment i am trying to improve my workflow for building an web-app. With Yeoman installed, the command "grunt" gives me a good way to combine and minify my javascript files. My main.js file gets generated from a lot of coffeescript files, which i'm doing manually before. I don't use the integrated way of compiling coffeescript because of file dependencies. (this is ok)

But here's the problem: when i now trying to run "grunt" it gives me "lint" errors from my generated js file like:

line 3   col 3    Missing "use strict" statement.
line 3   col 3    Expected 'var' to have an indentation at 5 instead at 3.
line 4   col 102  Expected '{' and instead saw 'child'.
line 4   col 233  A constructor name should start with an uppercase letter.

Warning: Task "jshint:all" failed. Use --force to continue.

if i --force it to continue, the main.js in the outcoming dest folder is still empty

What can i do? I thought that compiled coffeescript is "lint ready" javascript?

To compile my coffeescript files i use "rehab": https://www.npmjs.org/package/rehab

which provides me this Cakefile:

{exec, spawn} = require 'child_process'
Rehab = require 'rehab'

build = ->
      console.log "Building project from src/*.coffee to app/scripts/main.js"

      files = new Rehab().process 'src'
      files = files.join " "

      join_to_single_file = "--join app/scripts/main.js"
      compile_from_files = "--compile #{files}"

      exec "coffee #{join_to_single_file} #{compile_from_files}", (err, stdout, stderr) ->
        throw err if err

task 'build', 'Build coffee2js using Rehab', sbuild = ->
  build()

UPDATE:

If I understand correctly my main.js file will never passing jshint, so i have to remove the checking from yeoman / grunt to go further? I removed this line from my gruntfile.js:

// Make sure code styles are up to par and there are no obvious mistakes
    jshint: {
        options: {
            jshintrc: '.jshintrc',
            reporter: require('jshint-stylish')
        },
        all: [
            'Gruntfile.js',
          // removed  '<%= yeoman.app %>/scripts/{,*/}*.js',
            '!<%= yeoman.app %>/scripts/vendor/*',
            'test/spec/{,*/}*.js'
        ]
    },

Now i got no errors if running "grunt", but my output in dest folder contains still an empty main.js :(

I don't think the Coffeescript team cares about having its generated code pass jslint. (Or they used to, but that time has passed). See JavaScript Lint warnings caused by default argument values bug on the Coffeescript Github repo.

You could use coffeelint to check your code (there's even a Grunt module ).

However, Coffeelint checks several things but misses things that you may expect if you're expecting jslint style checks. (Coffeelint is more a stylistic checker).

In a large Coffeescript project I've taken to using both coffeelint and coffee-jshint . js* hint * makes more pragmatic choices about Javascript style (and has an easier interface to turn everything off), which jives more with the Javascript that Coffeescript produces. The coffee-jshint package passes the right parameters to jshint to not be too strict with the machine generated code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM