javascript - 从 Coffeescript 生成的 Javascript 还没有准备好 lint 吗? -> 在 Yeoman/Grunt 中抛出 Lint 错误

标签 javascript coffeescript gruntjs yeoman

目前,我正在努力改进构建网络应用程序的工作流程。 安装 Yeoman 后,“grunt”命令为我提供了一种合并和缩小 javascript 文件的好方法。 我的 main.js 文件是从很多 coffeescript 文件生成的,我之前是手动生成的。由于文件依赖性,我没有使用编译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?

为了编译我的 coffeescript 文件,我使用了“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 团队不关心让其生成的代码通过 jslint。 (或者他们曾经是,但那个时间已经过去了)。参见 JavaScript Lint warnings caused by default argument values Coffeescript Github 存储库上的错误。

你可以使用 coffeelint检查您的代码(甚至有一个 Grunt module )。

但是,Coffeelint 会检查几项内容,但会遗漏您可能期望的 jslint 样式检查内容。 (Coffeelint 更像是一种文体检查器)。

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

关于javascript - 从 Coffeescript 生成的 Javascript 还没有准备好 lint 吗? -> 在 Yeoman/Grunt 中抛出 Lint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133431/

相关文章:

javascript - 如何使用 Chai.should 测试某物是否是一个函数

javascript - ImageMapster javascript 插件工具提示未显示正确位置

javascript - 如何将 Immutable JS 与类型化的 ES6 类一起使用?

javascript - 重复的 CSS 类?

gruntjs - 设置 grunt 来启动和实时重新加载浏览器(使用最少的插件)

javascript - 在 chrome/webkit 中使用 XSLT 转换 HTML 节点

javascript - Coffeescript 在嵌套循环中关闭此引用列表

Node.js - Gunzip 已读取的文件异步问题

javascript - 如何从 grunt 任务中运行 grunt 任务?

javascript - 如何在 JavaScript 中编写返回错误的函数