-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,67 @@ | ||
eslint-plugin-react | ||
ESLint-plugin-React | ||
=================== | ||
|
||
React specific linting rules for ESLint | ||
|
||
# Installation | ||
|
||
Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally. | ||
|
||
npm install eslint@es6jsx | ||
|
||
ESLint-plugin-React requires `ESLint` with JSX support which is only available on the `es6jsx` branch for now. | ||
|
||
If you installed `ESLint` globally, you have to install React plugin globally too. Otherwise, install it locally. | ||
|
||
$ npm install eslint-plugin-react | ||
|
||
# Configuration | ||
|
||
Add `plugins` section and specify ESLint-plugin-React as a plugin. | ||
|
||
```json | ||
{ | ||
"plugins": [ | ||
"react" | ||
] | ||
} | ||
``` | ||
|
||
If it is not already the case you must also configure `ESLint` to support ECMAScript 6 and JSX. | ||
|
||
```json | ||
{ | ||
"settings": { | ||
"ecmascript": 6, | ||
"jsx": true | ||
} | ||
} | ||
``` | ||
|
||
Finally, enable all of the rules that you would like to use. | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"react/no-multi-comp": 1, | ||
"react/prop-types": 1 | ||
} | ||
} | ||
``` | ||
|
||
# List of supported rules | ||
|
||
* no-multi-comp: Prevent multiple component definition per file | ||
* prop-types: Prevent missing propTypes in a React component definition | ||
|
||
## Not supported yet | ||
|
||
* display-name: Prevent missing displayName in a React component definition | ||
* no-deprecated: Prevent usage of deprecated methods ([React 0.12 Updated API](http://facebook.github.io/react/blog/2014/10/28/react-v0.12.html#new-terminology-amp-updated-apis)) | ||
* no-classic: Prevent usage of "classic" methods ([#2700](https://github.com/facebook/react/pull/2700)) | ||
|
||
[Any rule idea is welcome !](https://github.com/yannickcr/eslint-plugin-react/issues) | ||
|
||
# License | ||
|
||
ESLint-plugin-React is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
rules: { | ||
'no-multi-comp': require('./lib/rules/no-multi-comp'), | ||
'prop-types': require('./lib/rules/prop-types') | ||
}, | ||
rulesConfig: { | ||
'no-multi-comp': 0, | ||
'prop-types': 0 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @fileoverview Prevent multiple component definition per file | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
var componentCounter = 0; | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
'MemberExpression': function(node) { | ||
if (node.object.name === 'React' && node.property.name === 'createClass' && ++componentCounter > 1) { | ||
context.report(node, 'Declare only one React component per file'); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @fileoverview Prevent missing propTypes in a React component definition | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
var hasPropTypes = false; | ||
|
||
return { | ||
|
||
'ObjectExpression': function(node) { | ||
|
||
if (!node.parent.callee || node.parent.callee.object.name !== 'React' || node.parent.callee.property.name !== 'createClass') { | ||
return; | ||
} | ||
|
||
node.properties.forEach(function(property) { | ||
var keyName = property.key.name || property.key.value; | ||
if (keyName === 'propTypes') { | ||
hasPropTypes = true; | ||
} | ||
}); | ||
|
||
}, | ||
|
||
'ObjectExpression:exit': function(node) { | ||
if (!node.parent.callee || node.parent.callee.object.name !== 'React' || node.parent.callee.property.name !== 'createClass') { | ||
return; | ||
} | ||
if (!hasPropTypes) { | ||
context.report(node, 'Component definition is missing props validation'); | ||
} | ||
hasPropTypes = false; | ||
} | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "eslint-plugin-react", | ||
"version": "1.0.0", | ||
"description": "React specific linting rules for ESLint", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yannickcr/eslint-plugin-react" | ||
}, | ||
"keywords": [ | ||
"eslint-plugin", | ||
"eslintplugin", | ||
"eslint", | ||
"react" | ||
], | ||
"author": "Yannick Croissant (https://github.com/yannickcr)", | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://raw.github.com/yannickcr/eslint-plugin-react/master/LICENSE" | ||
} | ||
], | ||
"bugs": { | ||
"url": "https://github.com/yannickcr/eslint-plugin-react/issues" | ||
}, | ||
"homepage": "https://github.com/yannickcr/eslint-plugin-react" | ||
} |