Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add react-in-jsx-scope rule #5

Merged
merged 1 commit into from
Mar 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/rules/react-in-jsx-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Prevent errors from not requiring React when using JSX (react-in-jsx-scope)

When using JSX, `<a />` expands to `React.createElement("a")`. Therefore the
`React` variable must be in scope.

## Rule Details

The following patterns are considered warnings:

```js
var Hello = <div>Hello {this.props.name}</div>;
```

The following patterns are not considered warnings:

```js
var React = require('react'); // or equivalent import
var Hello = <div>Hello {this.props.name}</div>;
```

## When Not To Use It

If you are setting `React` as a global variable, you will not need this rule.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
'wrap-multilines': require('./lib/rules/wrap-multilines'),
'self-closing-comp': require('./lib/rules/self-closing-comp'),
'no-did-mount-set-state': require('./lib/rules/no-did-mount-set-state'),
'no-did-update-set-state': require('./lib/rules/no-did-update-set-state')
'no-did-update-set-state': require('./lib/rules/no-did-update-set-state'),
'react-in-jsx-scope': require('./lib/rules/react-in-jsx-scope')
},
rulesConfig: {
'no-multi-comp': 0,
Expand All @@ -17,6 +18,7 @@ module.exports = {
'wrap-multilines': 0,
'self-closing-comp': 0,
'no-did-mount-set-state': 0,
'no-did-update-set-state': 0
'no-did-update-set-state': 0,
'react-in-jsx-scope': 0
}
};
48 changes: 48 additions & 0 deletions lib/rules/react-in-jsx-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @fileoverview Validate that React is in scope when using JSX.
* @author Glen Mailer
*/
"use strict";

// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------

module.exports = function(context) {

var NOT_DEFINED_MESSAGE = "'React' must be in scope when using JSX";

function findVariable(variables, name) {
var i, len;
for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
return true;
}
}
return false;
}

function variablesInScope() {
var scope = context.getScope(),
variables = scope.variables;

while (scope.type !== "global") {
scope = scope.upper;
variables = scope.variables.concat(variables);
}

return variables;
}

return {

"JSXOpeningElement": function(node) {
var variables = variablesInScope();
if (!findVariable(variables, 'React')) {
context.report(node, NOT_DEFINED_MESSAGE);
}
}

};

};
37 changes: 37 additions & 0 deletions tests/lib/rules/react-in-jsx-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @fileoverview Tests for react-in-jsx-scope
* @author Glen Mailer
*/

"use strict";

// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------

var eslint = require('eslint').linter;
var ESLintTester = require('eslint-tester');

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/react-in-jsx-scope", {
valid: [
{code: "var React, App; <App />;", args: [1, {vars: "all"}], ecmaFeatures: {jsx: true}},
{code: "var React; <img />;", args: [1, {vars: "all"}], ecmaFeatures: {jsx: true}},
{code: "var React; <x-gif />;", args: [1, {vars: "all"}], ecmaFeatures: {jsx: true}},
{code: "var React, App, a=1; <App attr={a} />;", ecmaFeatures: {jsx: true}},
{code: "var React, App, a=1; function elem() { return <App attr={a} />; }",
ecmaFeatures: {jsx: true}}
],
invalid: [
{code: "var App, a = <App />;",
errors: [{message: "'React' must be in scope when using JSX"}], ecmaFeatures: {jsx: true}},
{code: "var a = <App />;",
errors: [{message: "'React' must be in scope when using JSX"}], ecmaFeatures: {jsx: true}},
{code: "var a = <img />;",
errors: [{message: "'React' must be in scope when using JSX"}], ecmaFeatures: {jsx: true}}
]
});