Skip to content

Commit c62665c

Browse files
committed
First revision
1 parent d406540 commit c62665c

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

README.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
1-
eslint-plugin-react
1+
ESLint-plugin-React
22
===================
33

44
React specific linting rules for ESLint
5+
6+
# Installation
7+
8+
Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally.
9+
10+
npm install eslint@es6jsx
11+
12+
ESLint-plugin-React requires `ESLint` with JSX support which is only available on the `es6jsx` branch for now.
13+
14+
If you installed `ESLint` globally, you have to install React plugin globally too. Otherwise, install it locally.
15+
16+
$ npm install eslint-plugin-react
17+
18+
# Configuration
19+
20+
Add `plugins` section and specify ESLint-plugin-React as a plugin.
21+
22+
```json
23+
{
24+
"plugins": [
25+
"react"
26+
]
27+
}
28+
```
29+
30+
If it is not already the case you must also configure `ESLint` to support ECMAScript 6 and JSX.
31+
32+
```json
33+
{
34+
"settings": {
35+
"ecmascript": 6,
36+
"jsx": true
37+
}
38+
}
39+
```
40+
41+
Finally, enable all of the rules that you would like to use.
42+
43+
```json
44+
{
45+
"rules": {
46+
"react/no-multi-comp": 1,
47+
"react/prop-types": 1
48+
}
49+
}
50+
```
51+
52+
# List of supported rules
53+
54+
* no-multi-comp: Prevent multiple component definition per file
55+
* prop-types: Prevent missing propTypes in a React component definition
56+
57+
## Not supported yet
58+
59+
* display-name: Prevent missing displayName in a React component definition
60+
* 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))
61+
* no-classic: Prevent usage of "classic" methods ([#2700](https://github.com/facebook/react/pull/2700))
62+
63+
[Any rule idea is welcome !](https://github.com/yannickcr/eslint-plugin-react/issues)
64+
65+
# License
66+
67+
ESLint-plugin-React is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = {
4+
rules: {
5+
'no-multi-comp': require('./lib/rules/no-multi-comp'),
6+
'prop-types': require('./lib/rules/prop-types')
7+
},
8+
rulesConfig: {
9+
'no-multi-comp': 0,
10+
'prop-types': 0
11+
}
12+
};

lib/rules/no-multi-comp.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @fileoverview Prevent multiple component definition per file
3+
* @author Yannick Croissant
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = function(context) {
12+
13+
var componentCounter = 0;
14+
15+
//--------------------------------------------------------------------------
16+
// Public
17+
//--------------------------------------------------------------------------
18+
19+
return {
20+
'MemberExpression': function(node) {
21+
if (node.object.name === 'React' && node.property.name === 'createClass' && ++componentCounter > 1) {
22+
context.report(node, 'Declare only one React component per file');
23+
}
24+
}
25+
};
26+
};

lib/rules/prop-types.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @fileoverview Prevent missing propTypes in a React component definition
3+
* @author Yannick Croissant
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = function(context) {
12+
13+
var hasPropTypes = false;
14+
15+
return {
16+
17+
'ObjectExpression': function(node) {
18+
19+
if (!node.parent.callee || node.parent.callee.object.name !== 'React' || node.parent.callee.property.name !== 'createClass') {
20+
return;
21+
}
22+
23+
node.properties.forEach(function(property) {
24+
var keyName = property.key.name || property.key.value;
25+
if (keyName === 'propTypes') {
26+
hasPropTypes = true;
27+
}
28+
});
29+
30+
},
31+
32+
'ObjectExpression:exit': function(node) {
33+
if (!node.parent.callee || node.parent.callee.object.name !== 'React' || node.parent.callee.property.name !== 'createClass') {
34+
return;
35+
}
36+
if (!hasPropTypes) {
37+
context.report(node, 'Component definition is missing props validation');
38+
}
39+
hasPropTypes = false;
40+
}
41+
};
42+
43+
};

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "eslint-plugin-react",
3+
"version": "1.0.0",
4+
"description": "React specific linting rules for ESLint",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/yannickcr/eslint-plugin-react"
12+
},
13+
"keywords": [
14+
"eslint-plugin",
15+
"eslintplugin",
16+
"eslint",
17+
"react"
18+
],
19+
"author": "Yannick Croissant (https://github.com/yannickcr)",
20+
"licenses": [
21+
{
22+
"type": "MIT",
23+
"url": "https://raw.github.com/yannickcr/eslint-plugin-react/master/LICENSE"
24+
}
25+
],
26+
"bugs": {
27+
"url": "https://github.com/yannickcr/eslint-plugin-react/issues"
28+
},
29+
"homepage": "https://github.com/yannickcr/eslint-plugin-react"
30+
}

0 commit comments

Comments
 (0)