Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 9fd8706

Browse files
bjdixonhzoo
authored andcommitted
Fix: requireDollarBeforeJqueryAssignment - validate all keys
Added a loop to check each property on an object and stuffed the logic that checks whether a varName should start with a dollar sign into a helper function as we may be needing it more than once per file.iterateNodesByType cycle. Fixes #1688 Closes gh-1856
1 parent 90d38c9 commit 9fd8706

2 files changed

Lines changed: 54 additions & 40 deletions

File tree

lib/rules/require-dollar-before-jquery-assignment.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ module.exports.prototype = {
7575
var varName;
7676
var right;
7777

78+
function checkIfVarNameShouldStartWithDollar(varName, left, right) {
79+
80+
if (/^_?\$/.test(varName)) {
81+
return;
82+
}
83+
84+
if (!right || right.type !== 'CallExpression') {
85+
return;
86+
}
87+
88+
var nextToken = file.getTokenByRangeStart(right.callee.range[0]);
89+
if (nextToken.value !== '$') {
90+
return;
91+
}
92+
93+
nextToken = file.getNextToken(nextToken);
94+
if (nextToken.value !== '(') {
95+
return;
96+
}
97+
98+
while (!(nextToken.type === 'Punctuator' && nextToken.value === ')')) {
99+
nextToken = file.getNextToken(nextToken);
100+
}
101+
102+
nextToken = file.getNextToken(nextToken);
103+
if (!nextToken || !(nextToken.type === 'Punctuator' && nextToken.value === '.')) {
104+
errors.add(
105+
'jQuery identifiers must start with a $',
106+
left.loc.start.line,
107+
left.loc.start.column
108+
);
109+
}
110+
}
111+
78112
if (type === 'VariableDeclarator') {
79113
if (token.id.type === 'ObjectPattern' || token.id.type === 'ArrayPattern') {
80114
return;
@@ -83,7 +117,7 @@ module.exports.prototype = {
83117
left = token.id;
84118
varName = left.name;
85119
right = token.init;
86-
120+
checkIfVarNameShouldStartWithDollar(varName, left, right);
87121
} else if (ignoreProperties) {
88122
return;
89123

@@ -95,55 +129,27 @@ module.exports.prototype = {
95129

96130
varName = left.name || left.property.name;
97131
right = token.right;
98-
132+
checkIfVarNameShouldStartWithDollar(varName, left, right);
99133
} else {// type === 'ObjectExpression'
100-
var props = token.properties[0];
134+
var props = token.properties;
101135

102136
if (!props) {
103137
return;
104138
}
105139

106-
left = props.key;
140+
props.forEach(function(prop) {
141+
left = prop.key;
107142

108-
if (!left.name) {
109-
return;
110-
}
143+
if (!left.name) {
144+
return;
145+
}
111146

112-
varName = left.name;
113-
right = props.value;
147+
varName = left.name;
148+
right = prop.value;
149+
checkIfVarNameShouldStartWithDollar(varName, left, right);
150+
});
114151
}
115152

116-
if (varName.indexOf('$') === 0 || varName.indexOf('_$') === 0) {
117-
return;
118-
}
119-
120-
if (!right || right.type !== 'CallExpression') {
121-
return;
122-
}
123-
124-
var nextToken = file.getTokenByRangeStart(right.callee.range[0]);
125-
if (nextToken.value !== '$') {
126-
return;
127-
}
128-
129-
nextToken = file.getNextToken(nextToken);
130-
if (nextToken.value !== '(') {
131-
return;
132-
}
133-
134-
while (!(nextToken.type === 'Punctuator' && nextToken.value === ')')) {
135-
nextToken = file.getNextToken(nextToken);
136-
}
137-
138-
nextToken = file.getNextToken(nextToken);
139-
140-
if (!nextToken || !(nextToken.type === 'Punctuator' && nextToken.value === '.')) {
141-
errors.add(
142-
'jQuery identifiers must start with a $',
143-
left.loc.start.line,
144-
left.loc.start.column
145-
);
146-
}
147153
});
148154
}
149155
};

test/specs/rules/require-dollar-before-jquery-assignment.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ describe('rules/require-dollar-before-jquery-assignment', function() {
3131
expect(checker.checkString('var x = 2;')).to.have.no.errors();
3232
});
3333

34+
it('should not report for var declaration without assignment', function() {
35+
expect(checker.checkString('var x;')).to.have.no.errors();
36+
});
37+
3438
it('should not report function assignment', function() {
3539
expect(checker.checkString('var x = function() {};')).to.have.no.errors();
3640
});
@@ -181,6 +185,10 @@ describe('rules/require-dollar-before-jquery-assignment', function() {
181185
expect(checker.checkString('var x = { foo: $(\'.foo\') }'))
182186
.to.have.one.validation.error.from('requireDollarBeforejQueryAssignment');
183187
});
188+
189+
it('should look at keys besides the first', function() {
190+
expect(checker.checkString('var x = { bar: 1, foo: $(".foo") }')).to.have.error.count.equal(1);
191+
});
184192
});
185193

186194
describe('in object properties', function() {

0 commit comments

Comments
 (0)