Skip to content

Commit

Permalink
Merge pull request #1013 from jomasti/jsx-no-undef-imported-only
Browse files Browse the repository at this point in the history
react/jsx-no-undef: `allowGlobals` option
  • Loading branch information
ljharb committed Apr 23, 2017
2 parents 6d1485f + dc3a643 commit ed928c2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
35 changes: 35 additions & 0 deletions docs/rules/jsx-no-undef.md
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
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
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
}]
});

0 comments on commit ed928c2

Please sign in to comment.