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

react/jsx-no-undef allowGlobals option #1013

Merged
merged 1 commit into from
Apr 23, 2017
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
35 changes: 35 additions & 0 deletions docs/rules/jsx-no-undef.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ The following patterns are considered warnings:
<Hello name="John" />;
```

```jsx
// will ignore Text in the global scope and warn
var Hello = React.createClass({
render: function() {
return <Text>Hello</Text>;
}
});
module.exports = Hello;
```


The following patterns are not considered warnings:

```jsx
Expand All @@ -18,6 +29,30 @@ var Hello = require('./Hello');
<Hello name="John" />;
```

## Rule Options

```js
...
"react/jsx-no-undef": [<enabled>, { "allowGlobals": <boolean> }]
...
```

### `allowGlobals`

When `true` the rule will consider the global scope when checking for defined Components.

The following patterns are considered okay and do not cause warnings:

```jsx
var Text = require('./Text');
var Hello = React.createClass({
render: function() {
return <Text>Hello</Text>;
}
});
module.exports = Hello;
```

## When Not To Use It

If you are not using JSX then you can disable this rule.
22 changes: 20 additions & 2 deletions lib/rules/jsx-no-undef.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,33 @@ module.exports = {
category: 'Possible Errors',
recommended: true
},
schema: []
schema: [{
type: 'object',
properties: {
allowGlobals: {
type: 'boolean'
}
},
additionalProperties: false
}]
},

create: function(context) {

var config = context.options[0] || {};
var allowGlobals = config.allowGlobals || false;

/**
* Compare an identifier with the variables declared in the scope
* @param {ASTNode} node - Identifier or JSXIdentifier node
* @returns {void}
*/
function checkIdentifierInJSX(node) {
var scope = context.getScope();
var sourceCode = context.getSourceCode();
var sourceType = sourceCode.ast.sourceType;
var variables = scope.variables;
var scopeType = 'global';
var i;
var len;

Expand All @@ -47,7 +61,11 @@ module.exports = {
return;
}

while (scope.type !== 'global') {
if (!allowGlobals && sourceType === 'module') {
scopeType = 'module';
}

while (scope.type !== scopeType) {
scope = scope.upper;
variables = scope.variables.concat(variables);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/lib/rules/jsx-no-undef.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ ruleTester.run('jsx-no-undef', rule, {
'}'
].join('\n'),
parserOptions: parserOptions
}, {
code: 'var React; React.render(<Text />);',
parserOptions: parserOptions,
globals: {
Text: true
}
}, {
code: [
'import Text from "cool-module";',
'const TextWrapper = function (props) {',
' return (',
' <Text />',
' );',
'};'
].join('\n'),
options: [{
allowGlobals: false
}],
parser: 'babel-eslint',
parserOptions: parserOptions
}],
invalid: [{
code: '/*eslint no-undef:1*/ var React; React.render(<App />);',
Expand Down Expand Up @@ -87,5 +107,31 @@ ruleTester.run('jsx-no-undef', rule, {
message: '\'appp\' is not defined.'
}],
parserOptions: parserOptions
}, {
code: [
'const TextWrapper = function (props) {',
' return (',
' <Text />',
' );',
'};',
'export default TextWrapper;'
].join('\n'),
errors: [{
message: '\'Text\' is not defined.'
}],
options: [{
allowGlobals: false
}],
parser: 'babel-eslint',
parserOptions: parserOptions,
globals: {
Text: true
}
}, {
code: '/*eslint no-undef:1*/ var React; React.render(<Foo />);',
errors: [{
message: '\'Foo\' is not defined.'
}],
parserOptions: parserOptions
}]
});