Skip to content

Commit 8f24d0f

Browse files
committed
Add jsx-uses-vars rule
1 parent 526c8f5 commit 8f24d0f

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Finally, enable all of the rules that you would like to use.
5050
"react/no-did-mount-set-state": 1,
5151
"react/no-did-update-set-state": 1,
5252
"react/jsx-uses-react": 1,
53+
"react/jsx-uses-vars": 1,
5354
"react/react-in-jsx-scope": 1
5455
}
5556
}
@@ -65,6 +66,7 @@ Finally, enable all of the rules that you would like to use.
6566
* [no-did-mount-set-state](docs/rules/no-did-mount-set-state.md): Prevent usage of setState in componentDidMount
6667
* [no-did-update-set-state](docs/rules/no-did-update-set-state.md): Prevent usage of setState in componentDidUpdate
6768
* [jsx-uses-react](docs/rules/jsx-uses-react.md): Prevent React to be incorrectly marked as unused
69+
* [jsx-uses-vars](docs/rules/jsx-uses-vars.md): Prevent variables used in JSX to be incorrectly marked as unused
6870
* [react-in-jsx-scope](docs/rules/react-in-jsx-scope.md): Prevent missing React when using JSX
6971

7072
## To Do

docs/rules/jsx-uses-vars.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Prevent variables used in JSX to be incorrectly marked as unused (jsx-uses-vars)
22

3-
Since 0.17.0 the ESLint `no-unused-vars` rule does not detect variables used in JSX ([See details](eslint.org/blog/2015/03/eslint-0.17.0-released/#changes-to-jsx/react-handling)). This rules will find varaibles used in JSX and mark them as used.
3+
Since 0.17.0 the ESLint `no-unused-vars` rule does not detect variables used in JSX ([see details](http://eslint.org/blog/2015/03/eslint-0.17.0-released/#changes-to-jsx/react-handling)). This rules will find variables used in JSX and mark them as used.
44

55
This rule has no effect if the `no-unused-vars` rule is not enabled.
66

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = {
1010
'self-closing-comp': require('./lib/rules/self-closing-comp'),
1111
'no-did-mount-set-state': require('./lib/rules/no-did-mount-set-state'),
1212
'no-did-update-set-state': require('./lib/rules/no-did-update-set-state'),
13-
'react-in-jsx-scope': require('./lib/rules/react-in-jsx-scope')
13+
'react-in-jsx-scope': require('./lib/rules/react-in-jsx-scope'),
14+
'jsx-uses-vars': require('./lib/rules/jsx-uses-vars')
1415
},
1516
rulesConfig: {
1617
'jsx-uses-react': 0,
@@ -21,6 +22,7 @@ module.exports = {
2122
'self-closing-comp': 0,
2223
'no-did-mount-set-state': 0,
2324
'no-did-update-set-state': 0,
24-
'react-in-jsx-scope': 0
25+
'react-in-jsx-scope': 0,
26+
'jsx-uses-vars': 0
2527
}
2628
};

lib/rules/jsx-uses-vars.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @fileoverview Prevent variables used in JSX to be marked as unused
3+
* @author Yannick Croissant
4+
*/
5+
6+
'use strict';
7+
8+
// ------------------------------------------------------------------------------
9+
// Rule Definition
10+
// ------------------------------------------------------------------------------
11+
12+
module.exports = function(context) {
13+
14+
return {
15+
'JSXExpressionContainer': function(node) {
16+
if (node.expression.type === 'Identifier') {
17+
context.markVariableAsUsed(node.expression.name);
18+
}
19+
},
20+
21+
'JSXIdentifier': function(node) {
22+
context.markVariableAsUsed(node.name);
23+
}
24+
25+
};
26+
27+
};

tests/lib/rules/jsx-uses-vars.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @fileoverview Tests for jsx-uses-vars
3+
* @author Yannick Croissant
4+
*/
5+
6+
'use strict';
7+
8+
// -----------------------------------------------------------------------------
9+
// Requirements
10+
// -----------------------------------------------------------------------------
11+
12+
var eslint = require('eslint').linter;
13+
var ESLintTester = require('eslint-tester');
14+
15+
// -----------------------------------------------------------------------------
16+
// Tests
17+
// -----------------------------------------------------------------------------
18+
19+
var eslintTester = new ESLintTester(eslint);
20+
eslint.defineRule('jsx-uses-vars', require('../../../lib/rules/jsx-uses-vars'));
21+
eslintTester.addRuleTest('node_modules/eslint/lib/rules/no-unused-vars', {
22+
valid: [
23+
{
24+
code: '\
25+
/*eslint jsx-uses-vars:1*/\
26+
function foo() {\
27+
var App;\
28+
var bar = React.render(<App/>);\
29+
return bar;\
30+
};\
31+
foo()',
32+
ecmaFeatures: {
33+
jsx: true
34+
}
35+
},
36+
{
37+
code: '\
38+
/*eslint jsx-uses-vars:1*/\
39+
var App;\
40+
React.render(<App/>);',
41+
ecmaFeatures: {
42+
jsx: true
43+
}
44+
}, {
45+
code: '\
46+
/*eslint jsx-uses-vars:1*/\
47+
var a=1;\
48+
React.render(<img src={a} />);',
49+
ecmaFeatures: {
50+
jsx: true
51+
}
52+
}, {
53+
code: '\
54+
/*eslint jsx-uses-vars:1*/\
55+
var App;\
56+
function f() {\
57+
return <App />;\
58+
}\
59+
f();',
60+
ecmaFeatures: {
61+
jsx: true
62+
}
63+
}
64+
],
65+
invalid: [
66+
{
67+
code: '/*eslint jsx-uses-vars:1*/ var App;',
68+
errors: [{message: 'App is defined but never used'}], ecmaFeatures: {jsx: true}
69+
}
70+
]
71+
});

0 commit comments

Comments
 (0)