【问题标题】:Generated Javascript from Coffeescript is not lint ready? -> Throws Lint Error in Yeoman / Grunt从 Coffeescript 生成的 Javascript 没有 lint 准备好? -> 在 Yeoman / Grunt 中引发 Lint 错误
【发布时间】:2014-04-03 17:08:26
【问题描述】:

目前我正在尝试改进构建网络应用程序的工作流程。 安装 Yeoman 后,“grunt”命令为我提供了一种合并和缩小我的 javascript 文件的好方法。 我的 main.js 文件是从很多咖啡脚本文件中生成的,我之前是手动完成的。由于文件依赖性,我不使用编译coffeescript的集成方式。 (没关系)

但问题来了: 当我现在尝试运行“grunt”时,它会给我生成的 js 文件中的“lint”错误,例如:

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.

如果我 --force 让它继续,输出 dest 文件夹中的 main.js 仍然是空的

我能做什么? 我认为编译后的 coffeescript 是“lint ready”的 javascript?

要编译我的咖啡脚本文件,我使用“rehab”: https://www.npmjs.org/package/rehab

它为我提供了这个 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()

更新:

如果我理解正确,我的 main.js 文件将永远不会通过 jshint,所以我必须删除 yeoman / grunt 的检查才能更进一步?我从 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'
        ]
    },

现在如果运行“grunt”我没有错误,但我在 dest 文件夹中的输出仍然包含一个空的 main.js :(

【问题讨论】:

  • 对编译后的 CoffeeScript 进行 lint 检查是没有意义的。您可以调整您的设置以适应 CoffeeScript 生成的内容,但它不会让您获得任何结果,因为 CoffeeScript 确实总是生成适合该模式的代码。

标签: javascript coffeescript gruntjs yeoman


【解决方案1】:

我认为 Coffeescript 团队并不关心让其生成的代码通过 jslint。 (或者他们曾经,但那个时候已经过去了)。请参阅 Coffeescript Github 存储库中的 JavaScript Lint warnings caused by default argument values 错误。

您可以使用coffeelint 来检查您的代码(甚至还有Grunt module)。

但是,Coffeelint 会检查几项内容,但如果您期望进行 jslint 样式检查,则会遗漏一些您可能期望的内容。 (Coffeelint 更像是一种风格检查器)。

在一个大型 Coffeescript 项目中,我开始同时使用 coffeelint coffee-jshint。 js*hint* 对 Javascript 样式做出了更务实的选择(并且有一个更简单的界面可以关闭所有内容),这与 Coffeescript 生成的 Javascript 更加相得益彰。 coffee-jshint 包将正确的参数传递给 jshint,以免对机器生成的代码过于严格。

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多