-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This rule somewhat unusual, in that it modifies the behaviour of a core eslint rule. The alternative would be to copy-paste the original rule and modify it. That would require users to disable the core rule and replace with this one
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Make JSX count towards use of a declared variable (jsx-uses-react) | ||
|
||
JSX expands to a call to `React.createElement`, a file which includes `React` | ||
but only uses JSX should still consider the `React` variable used. | ||
|
||
This rule has no effect if the `no-unused-vars` rule is not enabled. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var React = require('react'); // and other equivalent imports | ||
|
||
// nothing to do with React | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var React = require('react'); | ||
|
||
var elem = <div>Some Stuff</div>; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using JSX, or React is delcared as global variable, this rule | ||
will not be useful. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @fileoverview Count <Jsx /> as use of the React variable | ||
* @author Glen Mailer | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
function flagReactAsUsedInJSX() { | ||
var scope = context.getScope(), | ||
variables = scope.variables, | ||
i, | ||
len; | ||
|
||
while (scope.type !== "global") { | ||
scope = scope.upper; | ||
variables = [].concat.apply(scope.variables, variables); | ||
} | ||
|
||
// mark first React found with the same special flag used by no-unused-vars | ||
for (i = 0, len = variables.length; i < len; i++) { | ||
if (variables[i].name === 'React') { | ||
variables[i].eslintJSXUsed = true; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
'JSXOpeningElement': function() { | ||
flagReactAsUsedInJSX(); | ||
} | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @fileoverview Tests for jsx-uses-react | ||
* @author Glen Mailer | ||
*/ | ||
|
||
"use strict"; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
var eslint = require('eslint').linter; | ||
var ESLintTester = require('eslint-tester'); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslint.defineRule('jsx-uses-react', require('../../../lib/rules/jsx-uses-react')); | ||
eslintTester.addRuleTest("node_modules/eslint/lib/rules/no-unused-vars", { | ||
valid: [ | ||
{code: "/*eslint jsx-uses-react:1*/ var App, React; <App />;", ecmaFeatures: {jsx: true}}, | ||
{code: "/*eslint jsx-uses-react:1*/ var React; <div />;", ecmaFeatures: {jsx: true}}, | ||
{code: "/*eslint jsx-uses-react:1*/ var React; (function () { <div /> })();", ecmaFeatures: {jsx: true}} | ||
], | ||
invalid: [ | ||
{code: "/*eslint jsx-uses-react:1*/ var React;", | ||
errors: [{message: "React is defined but never used"}], ecmaFeatures: {jsx: true}} | ||
] | ||
}); |