Skip to content

Commit d51b0cc

Browse files
committed
Add fragment support to react-in-jsx-scope (fixes #1758)
1 parent fb7ce94 commit d51b0cc

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

lib/rules/react-in-jsx-scope.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ module.exports = {
2727
const pragma = pragmaUtil.getFromContext(context);
2828
const NOT_DEFINED_MESSAGE = '\'{{name}}\' must be in scope when using JSX';
2929

30-
return {
31-
32-
JSXOpeningElement: function(node) {
33-
const variables = variableUtil.variablesInScope(context);
34-
if (variableUtil.findVariable(variables, pragma)) {
35-
return;
36-
}
37-
context.report({
38-
node: node,
39-
message: NOT_DEFINED_MESSAGE,
40-
data: {
41-
name: pragma
42-
}
43-
});
30+
function checkIfReactIsInScope(node) {
31+
const variables = variableUtil.variablesInScope(context);
32+
if (variableUtil.findVariable(variables, pragma)) {
33+
return;
4434
}
35+
context.report({
36+
node: node,
37+
message: NOT_DEFINED_MESSAGE,
38+
data: {
39+
name: pragma
40+
}
41+
});
42+
}
4543

44+
return {
45+
JSXOpeningElement: checkIfReactIsInScope,
46+
JSXOpeningFragment: checkIfReactIsInScope
4647
};
4748
}
4849
};

tests/lib/rules/react-in-jsx-scope.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ruleTester.run('react-in-jsx-scope', rule, {
3636
valid: [
3737
{code: 'var React, App; <App />;'},
3838
{code: 'var React; <img />;'},
39+
{code: 'var React; <>fragment</>;', parser: 'babel-eslint'},
3940
{code: 'var React; <x-gif />;'},
4041
{code: 'var React, App, a=1; <App attr={a} />;'},
4142
{code: 'var React, App, a=1; function elem() { return <App attr={a} />; }'},
@@ -64,6 +65,10 @@ ruleTester.run('react-in-jsx-scope', rule, {
6465
}, {
6566
code: 'var a = <img />;',
6667
errors: [{message: '\'React\' must be in scope when using JSX'}]
68+
}, {
69+
code: 'var a = <>fragment</>;',
70+
parser: 'babel-eslint',
71+
errors: [{message: '\'React\' must be in scope when using JSX'}]
6772
}, {
6873
code: '/** @jsx React.DOM */ var a = <img />;',
6974
errors: [{message: '\'React\' must be in scope when using JSX'}]

0 commit comments

Comments
 (0)