1 | /**
|
---|
2 | * @fileoverview Disallow undeclared variables in JSX
|
---|
3 | * @author Yannick Croissant
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const docsUrl = require('../util/docsUrl');
|
---|
9 | const eslintUtil = require('../util/eslint');
|
---|
10 | const jsxUtil = require('../util/jsx');
|
---|
11 | const report = require('../util/report');
|
---|
12 |
|
---|
13 | // ------------------------------------------------------------------------------
|
---|
14 | // Rule Definition
|
---|
15 | // ------------------------------------------------------------------------------
|
---|
16 |
|
---|
17 | const messages = {
|
---|
18 | undefined: '\'{{identifier}}\' is not defined.',
|
---|
19 | };
|
---|
20 |
|
---|
21 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
22 | module.exports = {
|
---|
23 | meta: {
|
---|
24 | docs: {
|
---|
25 | description: 'Disallow undeclared variables in JSX',
|
---|
26 | category: 'Possible Errors',
|
---|
27 | recommended: true,
|
---|
28 | url: docsUrl('jsx-no-undef'),
|
---|
29 | },
|
---|
30 |
|
---|
31 | messages,
|
---|
32 |
|
---|
33 | schema: [{
|
---|
34 | type: 'object',
|
---|
35 | properties: {
|
---|
36 | allowGlobals: {
|
---|
37 | type: 'boolean',
|
---|
38 | },
|
---|
39 | },
|
---|
40 | additionalProperties: false,
|
---|
41 | }],
|
---|
42 | },
|
---|
43 |
|
---|
44 | create(context) {
|
---|
45 | const config = context.options[0] || {};
|
---|
46 | const allowGlobals = config.allowGlobals || false;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Compare an identifier with the variables declared in the scope
|
---|
50 | * @param {ASTNode} node - Identifier or JSXIdentifier node
|
---|
51 | * @returns {void}
|
---|
52 | */
|
---|
53 | function checkIdentifierInJSX(node) {
|
---|
54 | let scope = eslintUtil.getScope(context, node);
|
---|
55 | const sourceCode = eslintUtil.getSourceCode(context);
|
---|
56 | const sourceType = sourceCode.ast.sourceType;
|
---|
57 | const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
|
---|
58 | let variables = scope.variables;
|
---|
59 | let i;
|
---|
60 | let len;
|
---|
61 |
|
---|
62 | // Ignore 'this' keyword (also maked as JSXIdentifier when used in JSX)
|
---|
63 | if (node.name === 'this') {
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | while (scope.type !== scopeUpperBound && scope.type !== 'global') {
|
---|
68 | scope = scope.upper;
|
---|
69 | variables = scope.variables.concat(variables);
|
---|
70 | }
|
---|
71 | if (scope.childScopes.length) {
|
---|
72 | variables = scope.childScopes[0].variables.concat(variables);
|
---|
73 | // Temporary fix for babel-eslint
|
---|
74 | if (scope.childScopes[0].childScopes.length) {
|
---|
75 | variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | for (i = 0, len = variables.length; i < len; i++) {
|
---|
80 | if (variables[i].name === node.name) {
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | report(context, messages.undefined, 'undefined', {
|
---|
86 | node,
|
---|
87 | data: {
|
---|
88 | identifier: node.name,
|
---|
89 | },
|
---|
90 | });
|
---|
91 | }
|
---|
92 |
|
---|
93 | return {
|
---|
94 | JSXOpeningElement(node) {
|
---|
95 | switch (node.name.type) {
|
---|
96 | case 'JSXIdentifier':
|
---|
97 | if (jsxUtil.isDOMComponent(node)) {
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | node = node.name;
|
---|
101 | break;
|
---|
102 | case 'JSXMemberExpression':
|
---|
103 | node = node.name;
|
---|
104 | do {
|
---|
105 | node = node.object;
|
---|
106 | } while (node && node.type !== 'JSXIdentifier');
|
---|
107 | break;
|
---|
108 | case 'JSXNamespacedName':
|
---|
109 | return;
|
---|
110 | default:
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | checkIdentifierInJSX(node);
|
---|
114 | },
|
---|
115 | };
|
---|
116 | },
|
---|
117 | };
|
---|