grunt-jsxhint和jsxhint表现不一样

时间:2015-04-08 22:27:01

标签: javascript gruntjs react-native grunt-jsxhint jsxhint

当我使用npm install -g jsxhint时,我可以使用它来lint我的jsx代码,如下所示:

enter image description here

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

enter image description here

我的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']);
};

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

2 个答案:

答案 0 :(得分:1)

JsxHint和JSHint不是linting JSX的最佳工具。 JSHint不支持JSX,所有JsxHint都会转换JSX,然后在转换后的代码上运行JSHint。 我一直在使用({强烈推荐)ESLintReact plugin。这是更好的,因为Eslint可以使用自定义解析器解析任何Javascript风格(如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"
    }
}

答案 1 :(得分:0)

我弄清楚我的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']);
};