Skip to content

Commit e677c94

Browse files
authored
Merge pull request #79 from nlfurniss/lint-callexpression
Add linting for CallExpessions like $.ajax()
2 parents cc34b16 + 1c1e287 commit e677c94

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

guides/rules/no-jquery-methods.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
**TL;DR** The intent of this rule is to identify any blacklisted jQuery methods as deprecated.
44

5-
Provides the ability to blacklist a set of jQuery methods for deprecation.
5+
Provides the ability to blacklist a set of jQuery methods for deprecation.
66
The eventual goal being a complete removal of jQuery.
77

88
# Usage
99

10-
Pass an argument array containing the specific blacklisted jQuery methods your consuming application is flagging for.
10+
Pass an argument array containing the specific blacklisted jQuery methods your consuming application is flagging for.
1111

1212
```js
1313
const BLACKLIST = [
14-
- 'add',
15-
- 'addBack',
16-
- 'after',
17-
- 'ajaxComplete'
14+
'add',
15+
'addBack',
16+
'after',
17+
'ajaxComplete'
1818
];
1919

2020
rules: {
21-
'ember-best-practices/no-jquery-methods': [2, BLACKLIST]
21+
'ember-best-practices/no-jquery-methods': ['error', BLACKLIST]
2222
}
2323
```
24+
To help generate this blacklist array, you can run this snippet in the console of your app:
25+
```js
26+
// In the dev tools console of your app.
27+
let $methods = [...new Set(Object.keys($).concat(Object.keys($.fn)))];
28+
copy(`'${$methods.join('\',\r\n\'')}'`)
29+
```

lib/rules/no-jquery-methods.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const { get } = require('../utils/get');
77

88
function getMessage(blackListName) {
9-
return `The use of ${blackListName} method has been deprecated.`;
9+
return `The use of jQuery's ${blackListName} method has been deprecated.`;
1010
}
1111

1212
function isJQueryCaller(node, name = null) {
@@ -53,6 +53,14 @@ module.exports = {
5353
jQueryLocalName = jQueryLocalName === get(node, 'init.name') ? varDecIDName : null;
5454
},
5555

56+
CallExpression(node) {
57+
const calleeName = get(node, 'callee.property.name');
58+
const parentCalleeName = get(node, 'callee.object.property.name');
59+
if (BLACKLIST.includes(calleeName) && parentCalleeName === '$') {
60+
context.report({ node: node.callee.property, message: getMessage(`${parentCalleeName}.${calleeName}()`) });
61+
}
62+
},
63+
5664
MemberExpression(node) {
5765
let propertyName = get(node, 'property.name');
5866
let blackListName = BLACKLIST.includes(propertyName) ? propertyName : false;

tests/lib/rules/no-jquery-methods.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,25 @@ ruleTester.run('no-jquery-methods', rule, {
3434
export default Ember.Component({
3535
init() {
3636
const myObj = {};
37-
myObj[${BLACKLISTMETHOD}]();
37+
myObj[${BLACKLISTMETHOD}]();
38+
}
39+
});`,
40+
options: [BLACKLISTMETHOD]
41+
},
42+
{
43+
code: `
44+
export default Ember.Component({
45+
init() {
46+
this.$.notBlacklistedMethod();
47+
}
48+
});`,
49+
options: [BLACKLISTMETHOD]
50+
},
51+
{
52+
code: `
53+
export default Ember.Component({
54+
init() {
55+
Ember.$.notBlacklistedMethod();
3856
}
3957
});`,
4058
options: [BLACKLISTMETHOD]
@@ -109,7 +127,7 @@ ruleTester.run('no-jquery-methods', rule, {
109127
export default Ember.Component({
110128
init() {
111129
const myJQueryObj = this.$();
112-
myJQueryObj[${BLACKLISTMETHOD}]();
130+
myJQueryObj[${BLACKLISTMETHOD}]();
113131
}
114132
});`,
115133
options: [BLACKLISTMETHOD],
@@ -130,6 +148,30 @@ ruleTester.run('no-jquery-methods', rule, {
130148
errors: [{
131149
message: getMessage(BLACKLISTMETHOD)
132150
}]
151+
},
152+
{
153+
code: `
154+
export default Ember.Component({
155+
init() {
156+
this.$.add();
157+
}
158+
});`,
159+
options: [BLACKLISTMETHOD],
160+
errors: [{
161+
message: getMessage(`$.${BLACKLISTMETHOD}()`)
162+
}]
163+
},
164+
{
165+
code: `
166+
export default Ember.Component({
167+
init() {
168+
Ember.$.add();
169+
}
170+
});`,
171+
options: [BLACKLISTMETHOD],
172+
errors: [{
173+
message: getMessage(`$.${BLACKLISTMETHOD}()`)
174+
}]
133175
}
134176
]
135177
});

0 commit comments

Comments
 (0)