Skip to content

Commit a5ee87d

Browse files
mislavdgraham
authored andcommitted
Add linter for ajax events
Covers these expressions: $(el).on("ajaxStart", ...) $(el).on("ajaxSend", ...) $(el).on("ajaxSuccess", ...) $(el).on("ajaxError", ...) $(el).on("ajaxComplete", ...) $(el).on("ajaxStop", ...)
1 parent 96e0df6 commit a5ee87d

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Add `jquery` to the plugins section of your `.eslintrc` configuration file. You
2929
],
3030
"rules": {
3131
"jquery/no-ajax": 2,
32+
"jquery/no-ajax-events": 2,
3233
"jquery/no-animate": 2,
3334
"jquery/no-attr": 2,
3435
"jquery/no-bind": 2,

rules/no-ajax-events.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
const utils = require('./utils.js')
4+
5+
const methodName = 'on'
6+
const disallowedEvents = {
7+
ajaxStart: true,
8+
ajaxSend: true,
9+
ajaxSuccess: true,
10+
ajaxError: true,
11+
ajaxComplete: true,
12+
ajaxStop: true
13+
}
14+
15+
const MemberExpression = 'MemberExpression'
16+
const Literal = 'Literal'
17+
18+
module.exports = function(context) {
19+
return {
20+
CallExpression: function(node) {
21+
if (
22+
node.callee.type === MemberExpression &&
23+
node.callee.property.name === methodName &&
24+
node.arguments.length >= 1
25+
) {
26+
const arg = node.arguments[0]
27+
if (
28+
arg.type === Literal &&
29+
arg.value in disallowedEvents &&
30+
utils.isjQuery(node)
31+
) {
32+
context.report({
33+
node: node,
34+
message: `Prefer remoteForm to ${arg.value}`
35+
})
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
module.exports.schema = []

tests/no-ajax-events.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
const rule = require('../rules/no-ajax-events')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
ruleTester.run('no-ajax-events', rule, {
8+
valid: [
9+
'$(document).on("click", function(e){ })',
10+
'$form.on("submit", function(e){ })',
11+
'$form.on()',
12+
'on("ajaxSuccess", ".js-select-menu", function(e){ })',
13+
'form.on("ajaxSend")'
14+
],
15+
invalid: [
16+
{
17+
code: '$(document).on("ajaxSend", function(e){ })',
18+
errors: [{
19+
message: 'Prefer remoteForm to ajaxSend',
20+
type: 'CallExpression'
21+
}]
22+
},
23+
{
24+
code: '$(document).on("ajaxSuccess", function(e){ })',
25+
errors: [{
26+
message: 'Prefer remoteForm to ajaxSuccess',
27+
type: 'CallExpression'
28+
}]
29+
},
30+
{
31+
code: '$form.on("ajaxError", function(e){ })',
32+
errors: [{
33+
message: 'Prefer remoteForm to ajaxError',
34+
type: 'CallExpression'
35+
}]
36+
},
37+
{
38+
code: '$form.on("ajaxComplete", function(e){ })',
39+
errors: [{
40+
message: 'Prefer remoteForm to ajaxComplete',
41+
type: 'CallExpression'
42+
}]
43+
},
44+
{
45+
code: '$form.on("ajaxStart", function(e){ })',
46+
errors: [{
47+
message: 'Prefer remoteForm to ajaxStart',
48+
type: 'CallExpression'
49+
}]
50+
},
51+
{
52+
code: '$form.on("ajaxStop", function(e){ })',
53+
errors: [{
54+
message: 'Prefer remoteForm to ajaxStop',
55+
type: 'CallExpression'
56+
}]
57+
}
58+
]
59+
})

0 commit comments

Comments
 (0)