File tree 5 files changed +105
-3
lines changed
5 files changed +105
-3
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ Finally, enable all of the rules that you would like to use.
50
50
"react/no-did-mount-set-state" : 1 ,
51
51
"react/no-did-update-set-state" : 1 ,
52
52
"react/jsx-uses-react" : 1 ,
53
+ "react/jsx-uses-vars" : 1 ,
53
54
"react/react-in-jsx-scope" : 1
54
55
}
55
56
}
@@ -65,6 +66,7 @@ Finally, enable all of the rules that you would like to use.
65
66
* [ no-did-mount-set-state] ( docs/rules/no-did-mount-set-state.md ) : Prevent usage of setState in componentDidMount
66
67
* [ no-did-update-set-state] ( docs/rules/no-did-update-set-state.md ) : Prevent usage of setState in componentDidUpdate
67
68
* [ 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
68
70
* [ react-in-jsx-scope] ( docs/rules/react-in-jsx-scope.md ) : Prevent missing React when using JSX
69
71
70
72
## To Do
Original file line number Diff line number Diff line change 1
1
# Prevent variables used in JSX to be incorrectly marked as unused (jsx-uses-vars)
2
2
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.
4
4
5
5
This rule has no effect if the ` no-unused-vars ` rule is not enabled.
6
6
Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ module.exports = {
10
10
'self-closing-comp' : require ( './lib/rules/self-closing-comp' ) ,
11
11
'no-did-mount-set-state' : require ( './lib/rules/no-did-mount-set-state' ) ,
12
12
'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' )
14
15
} ,
15
16
rulesConfig : {
16
17
'jsx-uses-react' : 0 ,
@@ -21,6 +22,7 @@ module.exports = {
21
22
'self-closing-comp' : 0 ,
22
23
'no-did-mount-set-state' : 0 ,
23
24
'no-did-update-set-state' : 0 ,
24
- 'react-in-jsx-scope' : 0
25
+ 'react-in-jsx-scope' : 0 ,
26
+ 'jsx-uses-vars' : 0
25
27
}
26
28
} ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments