grunt-jsxhint 和 jsxhint 的行为不一样

grunt-jsxhint and jsxhint not acting the same

当我使用 npm install -g jsxhint 时,我可以用它来检查我的 jsx 代码,就像这样:

但是当我尝试使用 grunt-jsxhint 设置 lint 命令时,它失败了:

我的 Gruntfile 非常简单:

module.exports = function(grunt) {

  grunt.loadNpmTasks('grunt-jsxhint');

  /* Project configuration */
  grunt.initConfig({
    options: {
      jshintrc: '.jshintrc',
      ignores: [],
      additionalSuffixes: ['.js', '.ios.js']
    },
    jshint: {
      all: ['./app/index.ios.js']
    }
  });

  /* Test Tasks */
  grunt.registerTask('test', ['jshint']);
};

为什么这些输出不同的结果?

JsxHint 和 JSHint 不是检查 JSX 的最佳工具。 JSHint 不支持 JSX,所有 JsxHint 所做的就是转换 JSX,然后在转换后的代码上运行 JSHint。 我一直在使用(并强烈推荐)ESLint with the React plugin. This is better since Eslint can parse any Javascript flavor using custom parsers (like esprima-fb)

示例 .eslintrc 文件:

{
    "parser": "esprima-fb",
    "env": {
        "browser": true,
        "node": true
    },

    "rules": {
        "no-mixed-requires": [0, false],
        "quotes": [2, "single"],
        "strict": [1, "never"],
        "semi": [2, "always"],
        "curly": 1,
        "no-bitwise": 1,
        "max-len": [1, 110, 4],
        "vars-on-top": 0,
        "guard-for-in": 1,
        "react/display-name": 1,
        "react/jsx-quotes": [2, "double", "avoid-escape"],
        "react/jsx-no-undef": 2,
        "react/jsx-sort-props": 0,
        "react/jsx-uses-react": 1,
        "react/jsx-uses-vars": 1,
        "react/no-did-mount-set-state": 2,
        "react/no-did-update-set-state": 2,
        "react/no-multi-comp": 0,
        "react/no-unknown-property": 1,
        "react/prop-types": 2,
        "react/react-in-jsx-scope": 1,
        "react/self-closing-comp": 1,
        "react/wrap-multilines": 2
    },

    "ecmaFeatures": {
        "jsx": true
    },

    "plugins": [ "react" ],

    "globals": {
        "d3": true,
        "require": "true",
        "module": "true",
        "$": "true",
        "d3": "true"
    }
}

我发现我的 Gruntfile 出了什么问题。

您需要将任务加载到:grunt.loadNpmTasks('grunt-jsxhint');

设置grunt.initConfig({})

这看起来像这样:

module.exports = function(grunt) {

  /* Project configuration */
  grunt.initConfig({
    options: {
      jshintrc: '.jshintrc',
      ignores: [],
      additionalSuffixes: ['.js', '.ios.js']
    },
    jshint: {
      all: ['./app/index.ios.js']
    }
  });

  grunt.loadNpmTasks('grunt-jsxhint');

  /* Test Tasks */
  grunt.registerTask('test', ['jshint']);
};