Skip to content

Commit

Permalink
Add linter for ajax events
Browse files Browse the repository at this point in the history
Covers these expressions:

    $(el).on("ajaxStart", ...)
    $(el).on("ajaxSend", ...)
    $(el).on("ajaxSuccess", ...)
    $(el).on("ajaxError", ...)
    $(el).on("ajaxComplete", ...)
    $(el).on("ajaxStop", ...)
  • Loading branch information
mislav authored and dgraham committed Dec 6, 2017
1 parent 96e0df6 commit a5ee87d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Add `jquery` to the plugins section of your `.eslintrc` configuration file. You
],
"rules": {
"jquery/no-ajax": 2,
"jquery/no-ajax-events": 2,
"jquery/no-animate": 2,
"jquery/no-attr": 2,
"jquery/no-bind": 2,
Expand Down
42 changes: 42 additions & 0 deletions rules/no-ajax-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const utils = require('./utils.js')

const methodName = 'on'
const disallowedEvents = {
ajaxStart: true,
ajaxSend: true,
ajaxSuccess: true,
ajaxError: true,
ajaxComplete: true,
ajaxStop: true
}

const MemberExpression = 'MemberExpression'
const Literal = 'Literal'

module.exports = function(context) {
return {
CallExpression: function(node) {
if (
node.callee.type === MemberExpression &&
node.callee.property.name === methodName &&
node.arguments.length >= 1
) {
const arg = node.arguments[0]
if (
arg.type === Literal &&
arg.value in disallowedEvents &&
utils.isjQuery(node)
) {
context.report({
node: node,
message: `Prefer remoteForm to ${arg.value}`
})
}
}
}
}
}

module.exports.schema = []
59 changes: 59 additions & 0 deletions tests/no-ajax-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const rule = require('../rules/no-ajax-events')
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester()
ruleTester.run('no-ajax-events', rule, {
valid: [
'$(document).on("click", function(e){ })',
'$form.on("submit", function(e){ })',
'$form.on()',
'on("ajaxSuccess", ".js-select-menu", function(e){ })',
'form.on("ajaxSend")'
],
invalid: [
{
code: '$(document).on("ajaxSend", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxSend',
type: 'CallExpression'
}]
},
{
code: '$(document).on("ajaxSuccess", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxSuccess',
type: 'CallExpression'
}]
},
{
code: '$form.on("ajaxError", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxError',
type: 'CallExpression'
}]
},
{
code: '$form.on("ajaxComplete", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxComplete',
type: 'CallExpression'
}]
},
{
code: '$form.on("ajaxStart", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxStart',
type: 'CallExpression'
}]
},
{
code: '$form.on("ajaxStop", function(e){ })',
errors: [{
message: 'Prefer remoteForm to ajaxStop',
type: 'CallExpression'
}]
}
]
})

0 comments on commit a5ee87d

Please sign in to comment.