Skip to content

Commit 526c8f5

Browse files
committed
Fix jsx-uses-react for ESLint 0.17.0
1 parent c122735 commit 526c8f5

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

docs/rules/jsx-uses-vars.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Prevent variables used in JSX to be incorrectly marked as unused (jsx-uses-vars)
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.
4+
5+
This rule has no effect if the `no-unused-vars` rule is not enabled.
6+
7+
## Rule Details
8+
9+
The following patterns are considered warnings:
10+
11+
```js
12+
var Hello = require('./Hello');
13+
```
14+
15+
The following patterns are not considered warnings:
16+
17+
```js
18+
var Hello = require('./Hello');
19+
20+
<Hello name="John" />;
21+
```
22+
23+
## When Not To Use It
24+
25+
If you are not using JSX or if you do not use the `no-unused-vars` rule then you can disable this rule.

lib/rules/jsx-uses-react.js

+1-21
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,14 @@
1010

1111
module.exports = function(context) {
1212

13-
function flagReactAsUsedInJSX() {
14-
var scope = context.getScope(),
15-
variables = scope.variables,
16-
i,
17-
len;
18-
19-
while (scope.type !== 'global') {
20-
scope = scope.upper;
21-
variables = [].concat.apply(scope.variables, variables);
22-
}
23-
24-
// mark first React found with the same special flag used by no-unused-vars
25-
for (i = 0, len = variables.length; i < len; i++) {
26-
if (variables[i].name === 'React') {
27-
variables[i].eslintJSXUsed = true;
28-
return;
29-
}
30-
}
31-
}
32-
3313
// --------------------------------------------------------------------------
3414
// Public
3515
// --------------------------------------------------------------------------
3616

3717
return {
3818

3919
'JSXOpeningElement': function() {
40-
flagReactAsUsedInJSX();
20+
context.markVariableAsUsed('React');
4121
}
4222
};
4323

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
2525
"devDependencies": {
2626
"coveralls": "2.11.2",
27-
"eslint": "0.15.0",
27+
"eslint": "0.17.0",
2828
"eslint-tester": "0.6.0",
2929
"istanbul": "0.3.6",
3030
"mocha": "2.1.0"

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

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var eslintTester = new ESLintTester(eslint);
2020
eslint.defineRule('jsx-uses-react', require('../../../lib/rules/jsx-uses-react'));
2121
eslintTester.addRuleTest('node_modules/eslint/lib/rules/no-unused-vars', {
2222
valid: [
23-
{code: '/*eslint jsx-uses-react:1*/ var App, React; <App />;', ecmaFeatures: {jsx: true}},
2423
{code: '/*eslint jsx-uses-react:1*/ var React; <div />;', ecmaFeatures: {jsx: true}},
2524
{code: '/*eslint jsx-uses-react:1*/ var React; (function () { <div /> })();', ecmaFeatures: {jsx: true}}
2625
],

0 commit comments

Comments
 (0)