Skip to content

Commit 6846329

Browse files
committed
Teach jsx-uses-vars about components as object properties
When I'm importing a component (say Input) which has other subcomponents as it's properties (like Input.Placeholder, Input.Button, etc), and there is another different component imported by a same name (say Button), and then if I use <Input.Button> in my code then eslint doesn't tell that <Button> is unused. Reproducible code: ``` import React from 'react'; import Button from './Button'; import Input from './Input'; export default class X extends React.Component { render() { return <Input.Button />; } } ``` What should be expected? The import Button from './Button'; line should be highlighted with a no-unused-vars error. You can see this if you just use <Input> in the render function instead of <Input.Button>. This was happening because the rule was checking for all JSXIdentifiers, which included the object properties. I fixed this by having it check for JSXOpeningElements and then drilling down to get the variable name used from there. Fixes jsx-eslint#694
1 parent 00cdab8 commit 6846329

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/rules/jsx-uses-vars.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@ module.exports = function(context) {
2020
variableUtil.markVariableAsUsed(context, node.expression.name);
2121
},
2222

23-
JSXIdentifier: function(node) {
24-
if (node.parent.type === 'JSXAttribute') {
23+
JSXOpeningElement: function(node) {
24+
var name;
25+
if (node.name.namespace && node.name.namespace.name) {
26+
// <Foo:Bar>
27+
name = node.name.namespace.name;
28+
} else if (node.name.name) {
29+
// <Foo>
30+
name = node.name.name;
31+
} else if (node.name.object && node.name.object.name) {
32+
// <Foo.Bar> - node.name.object.name
33+
name = node.name.object.name;
34+
} else {
2535
return;
2636
}
27-
variableUtil.markVariableAsUsed(context, node.name);
37+
38+
variableUtil.markVariableAsUsed(context, name);
2839
}
2940

3041
};

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ ruleTester.run('no-unused-vars', rule, {
112112
React.render(<App unused=""/>);',
113113
errors: [{message: '\'unused\' is defined but never used'}],
114114
parserOptions: parserOptions
115+
}, {
116+
code: '\
117+
/*eslint jsx-uses-vars:1*/\
118+
var App;\
119+
var Hello;\
120+
React.render(<App:Hello/>);',
121+
errors: [{message: '\'Hello\' is defined but never used'}],
122+
parserOptions: parserOptions
123+
}, {
124+
code: '\
125+
/*eslint jsx-uses-vars:1*/\
126+
var Button;\
127+
var Input;\
128+
React.render(<Button.Input unused=""/>);',
129+
errors: [{message: '\'Input\' is defined but never used'}],
130+
parserOptions: parserOptions
115131
}, {
116132
code: '\
117133
/*eslint jsx-uses-vars:1*/\

0 commit comments

Comments
 (0)