-
Notifications
You must be signed in to change notification settings - Fork 8
/
jsx-jcs-no-undef.js
103 lines (86 loc) · 3.81 KB
/
jsx-jcs-no-undef.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @fileoverview Extends generic eslint no-undef rule with custom logic for <For> and <With> statements.
* @author Alex Gilleran
*
* Version of https://github.com/eslint/eslint/blob/master/lib/rules/no-undef.js that filters out variables created
* by JSX Control Statements for statements.
*/
"use strict";
var utils = require("../utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Checks if the given node is the argument of a typeof operator.
* @param {ASTNode} node The AST node being checked.
* @returns {boolean} Whether or not the node is the argument of a typeof operator.
*/
function hasTypeOfOperator(node) {
var parent = node.parent;
return parent.type === "UnaryExpression" && parent.operator === "typeof";
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
var options = context.options[0];
var considerTypeOf = options && options.typeof === true || false;
return {
"Program:exit": function(/* node */) {
var globalScope = context.getScope();
globalScope.through.forEach(function(ref) {
var identifier = ref.identifier;
if (!considerTypeOf && hasTypeOfOperator(identifier)) {
return;
}
// Recurse up the tree of ancestors.
var thisElement = identifier;
while (thisElement.parent) {
thisElement = thisElement.parent;
// If this ancestor is a <For> element...
if (thisElement.type === "JSXElement" && utils.isForComponent(thisElement.openingElement)) {
// Index the <For>'s attributes by name.
var attrMap = thisElement.openingElement.attributes.reduce(function(result, attr) {
var map = result; // reassign this to appease eslint.
map[attr.name.name] = attr;
return result;
}, {});
// If the undefined variable is specified in "each" or "index", that's fine - ignore it.
// if (attrMap.each.value.value === identifier.name || attrMap.index.value.value === identifier.name) {
var attrIndex = attrMap.index;
var attrEach = attrMap.each;
var identifierName = identifier.name;
if ((attrEach && attrEach.value.value === identifierName) || (attrIndex && attrIndex.value.value === identifierName)) {
return;
}
}
// If this ancestor is a <With> element...
if (thisElement.type === "JSXElement" && utils.isWithComponent(thisElement.openingElement)) {
var someAttr = thisElement.openingElement.attributes.some(function(attr) {
return attr.name.name === identifier.name;
});
if (someAttr) {
return;
}
}
}
context.report({
node: identifier,
message: "'{{name}}' is not defined.",
data: identifier
});
});
}
};
};
module.exports.schema = [
{
"type": "object",
"properties": {
"typeof": {
"type": "boolean"
}
},
"additionalProperties": false
}
];