This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathrequire-dollar-before-jquery-assignment.js
154 lines (129 loc) · 3.95 KB
/
require-dollar-before-jquery-assignment.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Require a $ before variable names that are jquery assignments.
*
* Types: `Boolean` or `String`
*
* Values: `true` or `"ignoreProperties"`
*
* #### Example
*
* ```js
* "requireDollarBeforejQueryAssignment": true
* ```
*
* ##### Valid example for mode `true`
*
* ```js
* var $x = $(".foo");
* var y = {
* $x: $(".bar")
* };
* ```
*
* ##### Invalid example for mode `true`
*
* ```js
* var x = $(".foo");
* var y = {
* x: $(".bar")
* };
* ```
*
* ##### Valid example for mode `ignoreProperties`
*
* ```js
* var $x = $(".foo");
* var y = {
* x: $(".bar")
* };
* ```
*
* ##### Invalid example for mode `ignoreProperties`
*
* ```js
* var x = $(".foo");
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
_ignoreProperties: false,
configure: function(options) {
assert(
options === true || options === 'ignoreProperties',
this.getOptionName() + ' option requires true or "ignoreProperties" value, or should be removed'
);
this._ignoreProperties = (options === 'ignoreProperties');
},
getOptionName: function() {
return 'requireDollarBeforejQueryAssignment';
},
check: function(file, errors) {
var ignoreProperties = this._ignoreProperties;
file.iterateNodesByType(['VariableDeclarator', 'AssignmentExpression', 'ObjectExpression'], function(node) {
var type = node.type;
var left;
var varName;
var right;
function checkIfVarNameShouldStartWithDollar(varName, left, right) {
if (/^_?\$/.test(varName)) {
return;
}
if (!right || right.type !== 'CallExpression') {
return;
}
var nextToken = right.callee.firstChild;
if (nextToken.value !== '$') {
return;
}
nextToken = file.getNextToken(nextToken);
if (nextToken.value !== '(') {
return;
}
while (!(nextToken.type === 'Punctuator' && nextToken.value === ')')) {
nextToken = file.getNextToken(nextToken);
}
nextToken = file.getNextToken(nextToken);
if (!nextToken || !(nextToken.type === 'Punctuator' && nextToken.value === '.')) {
errors.add(
'jQuery identifiers must start with a $',
left
);
}
}
if (type === 'VariableDeclarator') {
if (node.id.type === 'ObjectPattern' || node.id.type === 'ArrayPattern') {
return;
}
left = node.id;
varName = left.name;
right = node.init;
checkIfVarNameShouldStartWithDollar(varName, left, right);
} else if (ignoreProperties) {
return;
} else if (type === 'AssignmentExpression') {
left = node.left;
if (left.computed || left.type === 'ArrayPattern') {
return;
}
varName = left.name || left.property.name;
right = node.right;
checkIfVarNameShouldStartWithDollar(varName, left, right);
} else {// type === 'ObjectExpression'
var props = node.properties;
if (!props) {
return;
}
props.forEach(function(prop) {
left = prop.key;
if (!left || !left.name) {
return;
}
varName = left.name;
right = prop.value;
checkIfVarNameShouldStartWithDollar(varName, left, right);
});
}
});
}
};