Skip to content

Commit 9a3ab20

Browse files
committed
Add linting for CallExpessions like $.ajax()
Also clean up the no-jquery-method explanation a tiny bit.
1 parent cc34b16 commit 9a3ab20

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

guides/rules/no-jquery-methods.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ Pass an argument array containing the specific blacklisted jQuery methods your c
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
```

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)