-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Covers these expressions: $(el).on("ajaxStart", ...) $(el).on("ajaxSend", ...) $(el).on("ajaxSuccess", ...) $(el).on("ajaxError", ...) $(el).on("ajaxComplete", ...) $(el).on("ajaxStop", ...)
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}] | ||
} | ||
] | ||
}) |