2 * @fileoverview Disallow undeclared variables in JSX
3 * @author Yannick Croissant
9 * Checks if a node name match the JSX tag convention.
10 * @param {String} name - Name of the node to check.
11 * @returns {boolean} Whether or not the node name match the JSX tag convention.
13 var tagConvention = /^[a-z]|\-/;
14 function isTagName(name) {
15 return tagConvention.test(name);
18 // ------------------------------------------------------------------------------
20 // ------------------------------------------------------------------------------
22 module.exports = function(context) {
25 * Compare an identifier with the variables declared in the scope
26 * @param {ASTNode} node - Identifier or JSXIdentifier node
29 function checkIdentifierInJSX(node) {
30 var scope = context.getScope();
31 var variables = scope.variables;
35 while (scope.type !== 'global') {
37 variables = scope.variables.concat(variables);
39 if (scope.childScopes.length) {
40 variables = scope.childScopes[0].variables.concat(variables);
41 // Temporary fix for babel-eslint
42 if (scope.childScopes[0].childScopes.length) {
43 variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
47 for (i = 0, len = variables.length; i < len; i++) {
48 if (variables[i].name === node.name) {
53 context.report(node, '\'' + node.name + '\' is not defined.');
57 JSXOpeningElement: function(node) {
58 if (isTagName(node.name.name)) {
61 checkIdentifierInJSX(node.name);