grunt-jsxhint and jsxhint not acting the same

258 views Asked by At

When I use npm install -g jsxhint I am able to use it to lint my jsx code, like so:

enter image description here

But when I try to use grunt-jsxhint to setup a lint command, it fails:

enter image description here

My Gruntfile is super simple:

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

Why are these outputting different results?

2

There are 2 answers

0
Amey On BEST ANSWER

JsxHint and JSHint arent the best tools for linting JSX. JSHint does not support JSX and all JsxHint does is transforms JSX and then runs JSHint on the transformed code. I have been using (and would highly recommend) ESLint with the React plugin. This is better since Eslint can parse any Javascript flavor using custom parsers (like esprima-fb)

Sample .eslintrc file:

{
    "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"
    }
}
0
Arian Faurtosh On

I figured out what was wrong with my Gruntfile.

You need to load the task in: grunt.loadNpmTasks('grunt-jsxhint');

After grunt.initConfig({}) has been set.

This would look like this:

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