JsxHint 和 JSHint 并不是 linting JSX 的最佳工具。JSHint 不支持 JSX,JsxHint 所做的只是转换 JSX,然后在转换后的代码上运行 JSHint。我一直在使用(并且强烈推荐)ESLint和React 插件。这更好,因为 Eslint 可以使用esprima-fb或babel-eslint等自定义解析器解析任何 Javascript 风格(请参阅下面的更新)。
示例.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"
}
}
更新
esprima-fb 很快就会被 Facebook 弃用。使用babel-eslint作为 eslint 的解析器。了解更多关于如何设置 Babel 和 Eslint 以使用 React 的好地方是这个 Github 项目。