From c57d22fe08e8370696cd2df7384ae4923b0cdf0a Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 24 Jan 2019 16:26:16 +0000 Subject: [PATCH] Add no-on-ready rule and add to deprecated 1.8 Part of #32 --- README.md | 1 + index.js | 2 ++ rules/no-on-ready.js | 28 ++++++++++++++++++++++++++++ tests/no-on-ready.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 rules/no-on-ready.js create mode 100644 tests/no-on-ready.js diff --git a/README.md b/README.md index 1706d852..a355abde 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,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 b551e8b9..19f96454 100644 --- a/index.js +++ b/index.js @@ -50,6 +50,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'), @@ -131,6 +132,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..2e1d36ab --- /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-animate-on', 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'}] + } + ] +})