You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.3 KiB

/**
* @fileoverview Prevent missing React when using JSX
* @author Glen Mailer
*/
'use strict';
const variableUtil = require('../util/variable');
const pragmaUtil = require('../util/pragma');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
const messages = {
notInScope: '\'{{name}}\' must be in scope when using JSX',
};
module.exports = {
meta: {
docs: {
description: 'Prevent missing React when using JSX',
category: 'Possible Errors',
recommended: true,
url: docsUrl('react-in-jsx-scope'),
},
messages,
schema: [],
},
create(context) {
const pragma = pragmaUtil.getFromContext(context);
function checkIfReactIsInScope(node) {
const variables = variableUtil.variablesInScope(context);
if (variableUtil.findVariable(variables, pragma)) {
return;
}
report(context, messages.notInScope, 'notInScope', {
node,
data: {
name: pragma,
},
});
}
return {
JSXOpeningElement: checkIfReactIsInScope,
JSXOpeningFragment: checkIfReactIsInScope,
};
},
};