Skip to content

Commit 088d440

Browse files
authored
Merge pull request #36 from joshkel/es6-method-handling
Extend no-es6-methods to detect common jQuery and Lodash uses
2 parents 89edbcf + f54e706 commit 088d440

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ESLint plugin for ES5 users.
99
Why?
1010
----
1111

12-
Sometimes someone doesn't want or can't to use Babel.
12+
Sometimes someone doesn't want to or can't use Babel.
1313
Even if you support modern browsers or node.js, JS engines have bugs
1414
like broken [block-scoping](http://stackoverflow.com/q/32665347).
1515
Maybe you only want to forbid usage of `for-of` in your project.

src/rules/no-es6-methods.js

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
'use strict';
22

3+
/**
4+
* Walks backwards through a chain of callees to get the original.
5+
*/
6+
function getOriginalCallee(callee) {
7+
while (callee.type === 'MemberExpression' && callee.object.type === 'CallExpression') {
8+
callee = callee.object.callee;
9+
}
10+
return callee;
11+
}
12+
13+
function isLodash(callee) {
14+
// Check for direct calls
15+
if(callee.object.name === '_') {
16+
return true;
17+
}
18+
19+
// Check for _.chain
20+
const original = getOriginalCallee(callee);
21+
if(original.type === 'MemberExpression'
22+
&& original.object.name === '_'
23+
&& original.property.name === 'chain') {
24+
return true;
25+
}
26+
27+
return false;
28+
}
29+
30+
function isJQuery(callee) {
31+
const original = getOriginalCallee(callee);
32+
33+
if(original.type === 'Identifier' && (original.name === '$' || original.name === 'jQuery')) {
34+
return true;
35+
}
36+
37+
// Support the popular convention of prefixing a variable with $ to show that
38+
// it refers to a jQuery object: https://stackoverflow.com/a/553734
39+
if(original.type === 'MemberExpression' && original.object.type === 'Identifier' && original.object.name[0] === '$') {
40+
return true;
41+
}
42+
43+
return false;
44+
}
45+
46+
function isPermitted(callee) {
47+
return isLodash(callee) || isJQuery(callee);
48+
}
49+
350
module.exports = {
451
meta: {
552
docs: {
@@ -10,34 +57,32 @@ module.exports = {
1057
create(context) {
1158
return {
1259
CallExpression(node) {
13-
const objectExceptions = ['_'];
14-
if(node.callee && node.callee.property && objectExceptions.indexOf(node.callee.object.name) === -1) {
15-
const functionName = node.callee.property.name;
16-
17-
const es6ArrayFunctions = [
18-
'find',
19-
'findIndex',
20-
'copyWithin',
21-
'values',
22-
'fill'
23-
];
24-
const es6StringFunctions = [
25-
'startsWith',
26-
'endsWith',
27-
'includes',
28-
'repeat'
29-
];
30-
31-
const es6Functions = [].concat(
32-
es6ArrayFunctions,
33-
es6StringFunctions
34-
);
35-
if(es6Functions.indexOf(functionName) > -1) {
36-
context.report({
37-
node: node.callee.property,
38-
message: 'ES6 methods not allowed: ' + functionName
39-
});
40-
}
60+
if(!node.callee || !node.callee.property) {
61+
return;
62+
}
63+
const functionName = node.callee.property.name;
64+
const es6ArrayFunctions = [
65+
'find',
66+
'findIndex',
67+
'copyWithin',
68+
'values',
69+
'fill'
70+
];
71+
const es6StringFunctions = [
72+
'startsWith',
73+
'endsWith',
74+
'includes',
75+
'repeat'
76+
];
77+
const es6Functions = [].concat(
78+
es6ArrayFunctions,
79+
es6StringFunctions
80+
);
81+
if (es6Functions.indexOf(functionName) > -1 && !isPermitted(node.callee)) {
82+
context.report({
83+
node: node.callee.property,
84+
message: 'ES6 methods not allowed: ' + functionName
85+
});
4186
}
4287
}
4388
};

tests/rules/no-es6-methods.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
module.exports = {
44
valid: [
55
'test.testMethod()',
6-
'test.otherTestMethod()'
6+
'test.otherTestMethod()',
7+
'_.find([1, 2, 3])',
8+
'_.chain([1, 2, 3]).reverse().find().value()',
9+
'$("#myDiv").find(".child")',
10+
'jQuery("#myDiv").parent().find(".child")',
11+
'$myForm.find(".child")'
712
],
813
invalid: [
914
{ code: '[1, 2, 3].find(x => x == 3);', errors: [{ message: 'ES6 methods not allowed: find' }] },

0 commit comments

Comments
 (0)