diff --git a/README.md b/README.md index f13de273..02cafb91 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Alternatively, you can pick out rules individually: "jquery/no-merge": 2, "jquery/no-noop": 2, "jquery/no-now": 2, + "jquery/no-on-ready": 2, "jquery/no-param": 2, "jquery/no-parent": 2, "jquery/no-parents": 2, diff --git a/index.js b/index.js index adf8e043..e6f6ce6e 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,7 @@ module.exports = { 'no-merge': require('./rules/no-merge'), 'no-noop': require('./rules/no-noop'), 'no-now': require('./rules/no-now'), + 'no-on-ready': require('./rules/no-on-ready'), 'no-param': require('./rules/no-param'), 'no-parent': require('./rules/no-parent'), 'no-parents': require('./rules/no-parents'), @@ -133,6 +134,7 @@ module.exports = { // FIXME: `deferred.pipe()` 'jquery/no-error-shorthand': 2, 'jquery/no-load-shorthand': 2, + 'jquery/no-on-ready': 2, 'jquery/no-size': 2, // FIXME: `$(...).toggle(fn,fn) (excluding https://api.jquery.com/toggle/) 'jquery/no-unload-shorthand': 2 diff --git a/rules/no-on-ready.js b/rules/no-on-ready.js new file mode 100644 index 00000000..1902797e --- /dev/null +++ b/rules/no-on-ready.js @@ -0,0 +1,28 @@ +'use strict' + +const utils = require('./utils.js') + +module.exports = { + meta: { + docs: {}, + schema: [] + }, + + create: function(context) { + return { + CallExpression: function(node) { + if (node.callee.type !== 'MemberExpression') return + if (node.callee.property.name !== 'on') return + const arg = node.arguments[0] + if (!arg || arg.value !== 'ready') return + + if (utils.isjQuery(node)) { + context.report({ + node: node, + message: '.on("ready") is not allowed' + }) + } + } + } + } +} diff --git a/tests/no-on-ready.js b/tests/no-on-ready.js new file mode 100644 index 00000000..042e5d57 --- /dev/null +++ b/tests/no-on-ready.js @@ -0,0 +1,36 @@ +'use strict' + +const rule = require('../rules/no-on-ready') +const RuleTester = require('eslint').RuleTester + +const error = '.on("ready") is not allowed' + +const ruleTester = new RuleTester() +ruleTester.run('no-on-ready', rule, { + valid: [ + '$(document).on("click", function(){})', + '$(document).on()', + '$document.on("click", function(){})', + '$document.on()', + '$document.on(ready)', + '$document.on(ready())', + '$document.ready()', + '$(function(){})', + 'document.on("ready")', + 'document.on("ready", function(){})' + ], + invalid: [ + { + code: '$(document).on("ready", function(){})', + errors: [{message: error, type: 'CallExpression'}] + }, + { + code: '$document.on("ready", function(){})', + errors: [{message: error, type: 'CallExpression'}] + }, + { + code: '$document.on("ready")', + errors: [{message: error, type: 'CallExpression'}] + } + ] +})