From a038103dcb285e95e9970ee6fc071a5a35257b88 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 23 Jan 2019 19:02:47 +0000 Subject: [PATCH] De-deduplicate rule generation Creates four generators: * createCollectionMethodRule: $(...).method(...) * createCollectionPropertyRule: $(...).property * createUtilMethodRule: $.method(...) * createUtilPropertyRule: $.property Not touched by this change are rules with more complex criteria, and rules where the distinction hasn't been made between util methods and collection methods e.g. $.text() and $(...).text(). Doesn't touch the tests to ensure no functionality is changed. Part of #55 --- rules/no-ajax.js | 32 ++--------- rules/no-and-self.js | 26 ++------- rules/no-animate.js | 26 ++------- rules/no-bind.js | 26 ++------- rules/no-box-model.js | 21 +------ rules/no-browser.js | 21 +------ rules/no-class.js | 28 ++-------- rules/no-clone.js | 26 ++------- rules/no-closest.js | 26 ++------- rules/no-context-prop.js | 23 +------- rules/no-css.js | 26 ++------- rules/no-delegate.js | 26 ++------- rules/no-die.js | 26 ++------- rules/no-each-collection.js | 27 ++------- rules/no-each-util.js | 25 ++------- rules/no-error-shorthand.js | 27 ++------- rules/no-extend.js | 25 ++------- rules/no-fade.js | 28 ++-------- rules/no-global-eval.js | 22 +------- rules/no-grep.js | 25 ++------- rules/no-has.js | 23 +------- rules/no-hide.js | 23 +------- rules/no-html.js | 26 ++------- rules/no-in-array.js | 25 ++------- rules/no-is-array.js | 25 ++------- rules/no-is-function.js | 25 ++------- rules/no-is-numeric.js | 25 ++------- rules/no-is-window.js | 22 +------- rules/no-is.js | 26 ++------- rules/no-live.js | 26 ++------- rules/no-load.js | 26 ++------- rules/no-map-collection.js | 27 ++------- rules/no-map-util.js | 22 +------- rules/no-merge.js | 25 ++------- rules/no-noop.js | 24 ++------ rules/no-now.js | 25 ++------- rules/no-param.js | 25 ++------- rules/no-parent.js | 26 ++------- rules/no-parents.js | 26 ++------- rules/no-parse-html.js | 25 ++------- rules/no-parse-json.js | 25 ++------- rules/no-proxy.js | 25 ++------- rules/no-ready-shorthand.js | 27 ++------- rules/no-selector-prop.js | 23 +------- rules/no-serialize.js | 30 ++-------- rules/no-show.js | 23 +------- rules/no-size.js | 26 ++------- rules/no-slide.js | 28 ++-------- rules/no-submit.js | 26 ++------- rules/no-support.js | 21 +------ rules/no-toggle.js | 23 +------- rules/no-trigger.js | 26 ++------- rules/no-trim.js | 25 ++------- rules/no-type.js | 25 ++------- rules/no-unbind.js | 26 ++------- rules/no-undelegate.js | 26 ++------- rules/no-unique.js | 25 ++------- rules/no-unload-shorthand.js | 27 ++------- rules/no-val.js | 26 ++------- rules/no-when.js | 25 ++------- rules/no-wrap.js | 28 ++-------- rules/utils.js | 104 ++++++++++++++++++++++++++++++++++- tests/no-closest.js | 2 +- tests/no-parents.js | 2 +- 64 files changed, 338 insertions(+), 1315 deletions(-) diff --git a/rules/no-ajax.js b/rules/no-ajax.js index 275a5e71..aa0cbc21 100644 --- a/rules/no-ajax.js +++ b/rules/no-ajax.js @@ -1,30 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - - const name = node.callee.property.name - switch (name) { - case 'ajax': - case 'get': - case 'getJSON': - case 'getScript': - case 'post': - context.report({ - node: node, - message: 'Prefer fetch to $.' + name - }) - } - } - } - } -} +module.exports = utils.createUtilMethodRule( + ['ajax', 'get', 'getJSON', 'getScript', 'post'], + node => `Prefer fetch to $.${node.callee.property.name}` +) diff --git a/rules/no-and-self.js b/rules/no-and-self.js index cbc59873..975088a4 100644 --- a/rules/no-and-self.js +++ b/rules/no-and-self.js @@ -2,25 +2,7 @@ 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 !== 'andSelf') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.addBack to $.andSelf' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'andSelf', + 'Prefer $.addBack to $.andSelf' +) diff --git a/rules/no-animate.js b/rules/no-animate.js index 126f23b0..db9af1e0 100644 --- a/rules/no-animate.js +++ b/rules/no-animate.js @@ -2,25 +2,7 @@ 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 !== 'animate') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer CSS transitions to $.animate' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'animate', + 'Prefer CSS transitions to $.animate' +) diff --git a/rules/no-bind.js b/rules/no-bind.js index 130d28c4..881408ab 100644 --- a/rules/no-bind.js +++ b/rules/no-bind.js @@ -2,25 +2,7 @@ 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 !== 'bind') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.on/addEventListener to bind' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'bind', + 'Prefer $.on/addEventListener to bind' +) diff --git a/rules/no-box-model.js b/rules/no-box-model.js index 4a473c7d..29b2e6cd 100644 --- a/rules/no-box-model.js +++ b/rules/no-box-model.js @@ -1,22 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - MemberExpression: function(node) { - if (node.object.name !== '$') return - if (node.property.name !== 'boxModel') return - - context.report({ - node: node, - message: '$.boxModel is not allowed' - }) - } - } - } -} +module.exports = utils.createUtilPropertyRule('boxModel') diff --git a/rules/no-browser.js b/rules/no-browser.js index 76755af5..63c5faad 100644 --- a/rules/no-browser.js +++ b/rules/no-browser.js @@ -1,22 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - MemberExpression: function(node) { - if (node.object.name !== '$') return - if (node.property.name !== 'browser') return - - context.report({ - node: node, - message: '$.browser is not allowed' - }) - } - } - } -} +module.exports = utils.createUtilPropertyRule('browser') diff --git a/rules/no-class.js b/rules/no-class.js index f8885c8e..e5445344 100644 --- a/rules/no-class.js +++ b/rules/no-class.js @@ -2,27 +2,7 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - const forbidden = ['addClass', 'hasClass', 'removeClass', 'toggleClass'] - - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (forbidden.indexOf(node.callee.property.name) === -1) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer classList to ' + node.callee.property.name - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + ['addClass', 'hasClass', 'removeClass', 'toggleClass'], + node => `Prefer classList to ${node.callee.property.name}` +) diff --git a/rules/no-clone.js b/rules/no-clone.js index 71a15def..275e611e 100644 --- a/rules/no-clone.js +++ b/rules/no-clone.js @@ -2,25 +2,7 @@ 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 !== 'clone') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer cloneNode to clone' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'clone', + 'Prefer cloneNode to clone' +) diff --git a/rules/no-closest.js b/rules/no-closest.js index f2d0ed13..24c764ea 100644 --- a/rules/no-closest.js +++ b/rules/no-closest.js @@ -2,25 +2,7 @@ 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 !== 'closest') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer closest to closest' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'closest', + 'Prefer Element.closest to $.closest' +) diff --git a/rules/no-context-prop.js b/rules/no-context-prop.js index 024deb76..bcfd211f 100644 --- a/rules/no-context-prop.js +++ b/rules/no-context-prop.js @@ -2,25 +2,4 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - return { - MemberExpression: function(node) { - if (node.property.name !== 'context') return - if (node.parent.callee === node) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.context is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionPropertyRule('context') diff --git a/rules/no-css.js b/rules/no-css.js index 3ee3f1ae..fe7f1fe1 100644 --- a/rules/no-css.js +++ b/rules/no-css.js @@ -2,25 +2,7 @@ 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 !== 'css') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer getComputedStyle to css' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'css', + 'Prefer getComputedStyle to css' +) diff --git a/rules/no-delegate.js b/rules/no-delegate.js index a33db933..1d1173f4 100644 --- a/rules/no-delegate.js +++ b/rules/no-delegate.js @@ -2,25 +2,7 @@ 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 !== 'delegate') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.on/addEventListener to delegate' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'delegate', + 'Prefer $.on/addEventListener to delegate' +) diff --git a/rules/no-die.js b/rules/no-die.js index cffbca0e..723a1581 100644 --- a/rules/no-die.js +++ b/rules/no-die.js @@ -2,25 +2,7 @@ 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 !== 'die') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.off/removeEventListener to die' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'die', + 'Prefer $.off/removeEventListener to die' +) diff --git a/rules/no-each-collection.js b/rules/no-each-collection.js index 856ce4f9..7f6e10f9 100644 --- a/rules/no-each-collection.js +++ b/rules/no-each-collection.js @@ -2,26 +2,7 @@ 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.object.name === '$') return - if (node.callee.property.name !== 'each') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer Array#forEach to $.each' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'each', + 'Prefer Array#forEach to $.each' +) diff --git a/rules/no-each-util.js b/rules/no-each-util.js index 7bd18c31..7dfeba21 100644 --- a/rules/no-each-util.js +++ b/rules/no-each-util.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'each') return - - context.report({ - node: node, - message: 'Prefer Array#forEach to $.each' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'each', + 'Prefer Array#forEach to $.each' +) diff --git a/rules/no-error-shorthand.js b/rules/no-error-shorthand.js index 228b5137..50f3d2b3 100644 --- a/rules/no-error-shorthand.js +++ b/rules/no-error-shorthand.js @@ -2,26 +2,7 @@ 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.object.name === '$') return - if (node.callee.property.name !== 'error') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.on or $.trigger to $.error' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'error', + 'Prefer $.on or $.trigger to $.error' +) diff --git a/rules/no-extend.js b/rules/no-extend.js index 47e145be..0d701d5f 100644 --- a/rules/no-extend.js +++ b/rules/no-extend.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'extend') return - - context.report({ - node: node, - message: 'Prefer Object#assign or the spread operator to $.extend' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'extend', + 'Prefer Object#assign or the spread operator to $.extend' +) diff --git a/rules/no-fade.js b/rules/no-fade.js index af043df6..ceafaa65 100644 --- a/rules/no-fade.js +++ b/rules/no-fade.js @@ -2,27 +2,7 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - const forbidden = ['fadeIn', 'fadeOut', 'fadeTo', 'fadeToggle'] - - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (forbidden.indexOf(node.callee.property.name) === -1) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer CSS transitions to $.' + node.callee.property.name - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + ['fadeIn', 'fadeOut', 'fadeTo', 'fadeToggle'], + node => `Prefer CSS transitions to $.${node.callee.property.name}` +) diff --git a/rules/no-global-eval.js b/rules/no-global-eval.js index 65715de0..5e944f15 100644 --- a/rules/no-global-eval.js +++ b/rules/no-global-eval.js @@ -1,23 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'globalEval') return - - context.report({ - node: node, - message: '$.globalEval is not allowed' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule('globalEval') diff --git a/rules/no-grep.js b/rules/no-grep.js index 059b2ff4..d8fabde8 100644 --- a/rules/no-grep.js +++ b/rules/no-grep.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'grep') return - - context.report({ - node: node, - message: 'Prefer Array#filter to $.grep' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'grep', + 'Prefer Array#filter to $.grep' +) diff --git a/rules/no-has.js b/rules/no-has.js index 986e4bd2..9c5d43fa 100644 --- a/rules/no-has.js +++ b/rules/no-has.js @@ -2,25 +2,4 @@ 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 !== 'has') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.has is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule('has') diff --git a/rules/no-hide.js b/rules/no-hide.js index e820c4f3..9d1c9789 100644 --- a/rules/no-hide.js +++ b/rules/no-hide.js @@ -2,25 +2,4 @@ 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 !== 'hide') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.hide is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule('hide') diff --git a/rules/no-html.js b/rules/no-html.js index 1381ffca..f73e3219 100644 --- a/rules/no-html.js +++ b/rules/no-html.js @@ -2,25 +2,7 @@ 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 !== 'html') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer innerHTML to $.html' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'html', + 'Prefer innerHTML to $.html' +) diff --git a/rules/no-in-array.js b/rules/no-in-array.js index 8d5e502f..3b099d8d 100644 --- a/rules/no-in-array.js +++ b/rules/no-in-array.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'inArray') return - - context.report({ - node: node, - message: 'Prefer Array#indexOf to $.inArray' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'inArray', + 'Prefer Array#indexOf to $.inArray' +) diff --git a/rules/no-is-array.js b/rules/no-is-array.js index 2d8db82e..592d45a1 100644 --- a/rules/no-is-array.js +++ b/rules/no-is-array.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'isArray') return - - context.report({ - node: node, - message: 'Prefer Array#isArray to $.isArray' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'isArray', + 'Prefer Array#isArray to $.isArray' +) diff --git a/rules/no-is-function.js b/rules/no-is-function.js index b86b762a..64b8f141 100644 --- a/rules/no-is-function.js +++ b/rules/no-is-function.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'isFunction') return - - context.report({ - node: node, - message: 'Prefer typeof to $.isFunction' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'isFunction', + 'Prefer typeof to $.isFunction' +) diff --git a/rules/no-is-numeric.js b/rules/no-is-numeric.js index 58fd2980..9c2a1d1e 100644 --- a/rules/no-is-numeric.js +++ b/rules/no-is-numeric.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'isNumeric') return - - context.report({ - node: node, - message: 'Prefer typeof to $.isNumeric' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'isNumeric', + 'Prefer typeof to $.isNumeric' +) diff --git a/rules/no-is-window.js b/rules/no-is-window.js index 4241c862..0776a1b0 100644 --- a/rules/no-is-window.js +++ b/rules/no-is-window.js @@ -1,23 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'isWindow') return - - context.report({ - node: node, - message: '$.isWindow is not allowed' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule('isWindow') diff --git a/rules/no-is.js b/rules/no-is.js index ab57531d..5b00b2a8 100644 --- a/rules/no-is.js +++ b/rules/no-is.js @@ -2,25 +2,7 @@ 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 !== 'is') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer matches to $.is' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'is', + 'Prefer matches to $.is' +) diff --git a/rules/no-live.js b/rules/no-live.js index 937974a7..b77ad80e 100644 --- a/rules/no-live.js +++ b/rules/no-live.js @@ -2,25 +2,7 @@ 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 !== 'live') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.on/addEventListener to live' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'live', + 'Prefer $.on/addEventListener to live' +) diff --git a/rules/no-load.js b/rules/no-load.js index 7d64a3b6..c218e167 100644 --- a/rules/no-load.js +++ b/rules/no-load.js @@ -2,25 +2,7 @@ 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 !== 'load') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer fetch to $.load' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'load', + 'Prefer fetch to $.load' +) diff --git a/rules/no-map-collection.js b/rules/no-map-collection.js index 95c20c32..ba860941 100644 --- a/rules/no-map-collection.js +++ b/rules/no-map-collection.js @@ -2,26 +2,7 @@ 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.object.name === '$') return - if (node.callee.property.name !== 'map') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer Array#map to $.map' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'map', + 'Prefer Array#map to $.map' +) diff --git a/rules/no-map-util.js b/rules/no-map-util.js index 1a99a137..3c9411fd 100644 --- a/rules/no-map-util.js +++ b/rules/no-map-util.js @@ -1,23 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'map') return - - context.report({ - node: node, - message: 'Prefer Array#map to $.map' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule('map', 'Prefer Array#map to $.map') diff --git a/rules/no-merge.js b/rules/no-merge.js index b3f882c8..ff15ee3d 100644 --- a/rules/no-merge.js +++ b/rules/no-merge.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'merge') return - - context.report({ - node: node, - message: 'Prefer Array#concat to $.merge' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'merge', + 'Prefer Array#concat to $.merge' +) diff --git a/rules/no-noop.js b/rules/no-noop.js index d680fa65..55d0a201 100644 --- a/rules/no-noop.js +++ b/rules/no-noop.js @@ -1,22 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - MemberExpression: function(node) { - if (node.object.name !== '$') return - if (node.property.name !== 'noop') return - - context.report({ - node: node, - message: 'Prefer function() {} to $.noop' - }) - } - } - } -} +module.exports = utils.createUtilPropertyRule( + 'noop', + 'Prefer function() {} to $.noop' +) diff --git a/rules/no-now.js b/rules/no-now.js index ba6b8cda..df7e5803 100644 --- a/rules/no-now.js +++ b/rules/no-now.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'now') return - - context.report({ - node: node, - message: 'Prefer (new Date).getTime() to $.now' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'now', + 'Prefer (new Date).getTime() to $.now' +) diff --git a/rules/no-param.js b/rules/no-param.js index a90c7c7e..20623f9b 100644 --- a/rules/no-param.js +++ b/rules/no-param.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'param') return - - context.report({ - node: node, - message: 'Prefer FormData or URLSearchParams to $.param' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'param', + 'Prefer FormData or URLSearchParams to $.param' +) diff --git a/rules/no-parent.js b/rules/no-parent.js index 1a156d74..1f7ba414 100644 --- a/rules/no-parent.js +++ b/rules/no-parent.js @@ -2,25 +2,7 @@ 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 !== 'parent') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer parentElement to $.parent' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'parent', + 'Prefer parentElement to $.parent' +) diff --git a/rules/no-parents.js b/rules/no-parents.js index 59b747d2..9e5f5255 100644 --- a/rules/no-parents.js +++ b/rules/no-parents.js @@ -2,25 +2,7 @@ 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 !== 'parents') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer closest to $.parents' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'parents', + 'Prefer Element.closest to $.parents' +) diff --git a/rules/no-parse-html.js b/rules/no-parse-html.js index 2f711cb4..055978f1 100644 --- a/rules/no-parse-html.js +++ b/rules/no-parse-html.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'parseHTML') return - - context.report({ - node: node, - message: 'Prefer createHTMLDocument to $.parseHTML' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'parseHTML', + 'Prefer createHTMLDocument to $.parseHTML' +) diff --git a/rules/no-parse-json.js b/rules/no-parse-json.js index 0890fb58..656c4057 100644 --- a/rules/no-parse-json.js +++ b/rules/no-parse-json.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'parseJSON') return - - context.report({ - node: node, - message: 'Prefer JSON.parse to $.parseJSON' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'parseJSON', + 'Prefer JSON.parse to $.parseJSON' +) diff --git a/rules/no-proxy.js b/rules/no-proxy.js index 839c3091..35b98bf7 100644 --- a/rules/no-proxy.js +++ b/rules/no-proxy.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'proxy') return - - context.report({ - node: node, - message: 'Prefer Function#bind to $.proxy' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'proxy', + 'Prefer Function#bind to $.proxy' +) diff --git a/rules/no-ready-shorthand.js b/rules/no-ready-shorthand.js index 709e62d1..7761eb74 100644 --- a/rules/no-ready-shorthand.js +++ b/rules/no-ready-shorthand.js @@ -2,26 +2,7 @@ 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.object.name === '$') return - if (node.callee.property.name !== 'ready') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $ to $.ready' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'ready', + 'Prefer $ to $.ready' +) diff --git a/rules/no-selector-prop.js b/rules/no-selector-prop.js index 38dcf9aa..00524285 100644 --- a/rules/no-selector-prop.js +++ b/rules/no-selector-prop.js @@ -2,25 +2,4 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - return { - MemberExpression: function(node) { - if (node.property.name !== 'selector') return - if (node.parent.callee === node) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.selector is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionPropertyRule('selector') diff --git a/rules/no-serialize.js b/rules/no-serialize.js index 33386af9..f6ee94f3 100644 --- a/rules/no-serialize.js +++ b/rules/no-serialize.js @@ -2,29 +2,7 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - const forbidden = ['serialize', 'serializeArray'] - - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (forbidden.indexOf(node.callee.property.name) === -1) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: - 'Prefer FormData or URLSearchParams to $.' + - node.callee.property.name - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + ['serialize', 'serializeArray'], + node => `Prefer FormData or URLSearchParams to $.${node.callee.property.name}` +) diff --git a/rules/no-show.js b/rules/no-show.js index f74d1ddb..eb5f330a 100644 --- a/rules/no-show.js +++ b/rules/no-show.js @@ -2,25 +2,4 @@ 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 !== 'show') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.show is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule('show') diff --git a/rules/no-size.js b/rules/no-size.js index 8e70263a..dcebeaa7 100644 --- a/rules/no-size.js +++ b/rules/no-size.js @@ -2,25 +2,7 @@ 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 !== 'size') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer length to $.size' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'size', + 'Prefer length to $.size' +) diff --git a/rules/no-slide.js b/rules/no-slide.js index fc3da93d..0f040700 100644 --- a/rules/no-slide.js +++ b/rules/no-slide.js @@ -2,27 +2,7 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - const forbidden = ['slideDown', 'slideToggle', 'slideUp'] - - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (forbidden.indexOf(node.callee.property.name) === -1) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer CSS transitions to $.' + node.callee.property.name - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + ['slideDown', 'slideToggle', 'slideUp'], + node => `Prefer CSS transitions to $.${node.callee.property.name}` +) diff --git a/rules/no-submit.js b/rules/no-submit.js index 4b7a32a3..95b2b235 100644 --- a/rules/no-submit.js +++ b/rules/no-submit.js @@ -2,25 +2,7 @@ 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 !== 'submit') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer dispatchEvent + form.submit() to $.submit' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'submit', + 'Prefer dispatchEvent + form.submit() to $.submit' +) diff --git a/rules/no-support.js b/rules/no-support.js index fd41e64c..b8bea591 100644 --- a/rules/no-support.js +++ b/rules/no-support.js @@ -1,22 +1,5 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - MemberExpression: function(node) { - if (node.object.name !== '$') return - if (node.property.name !== 'support') return - - context.report({ - node: node, - message: '$.support is not allowed' - }) - } - } - } -} +module.exports = utils.createUtilPropertyRule('support') diff --git a/rules/no-toggle.js b/rules/no-toggle.js index b83084c3..4f7926d0 100644 --- a/rules/no-toggle.js +++ b/rules/no-toggle.js @@ -2,25 +2,4 @@ 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 !== 'toggle') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.toggle is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule('toggle') diff --git a/rules/no-trigger.js b/rules/no-trigger.js index c91173cc..fcec4d19 100644 --- a/rules/no-trigger.js +++ b/rules/no-trigger.js @@ -2,25 +2,7 @@ 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 !== 'trigger') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer dispatchEvent to $.trigger' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'trigger', + 'Prefer dispatchEvent to $.trigger' +) diff --git a/rules/no-trim.js b/rules/no-trim.js index 958c4f29..c6755b73 100644 --- a/rules/no-trim.js +++ b/rules/no-trim.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'trim') return - - context.report({ - node: node, - message: 'Prefer String#trim to $.trim' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'trim', + 'Prefer String#trim to $.trim' +) diff --git a/rules/no-type.js b/rules/no-type.js index e5342e5f..2dedd822 100644 --- a/rules/no-type.js +++ b/rules/no-type.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'type') return - - context.report({ - node: node, - message: 'Prefer typeof/instanceof to $.type' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'type', + 'Prefer typeof/instanceof to $.type' +) diff --git a/rules/no-unbind.js b/rules/no-unbind.js index fa470587..f3fc2dcd 100644 --- a/rules/no-unbind.js +++ b/rules/no-unbind.js @@ -2,25 +2,7 @@ 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 !== 'unbind') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.off/removeEventListener to unbind' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'unbind', + 'Prefer $.off/removeEventListener to unbind' +) diff --git a/rules/no-undelegate.js b/rules/no-undelegate.js index 596e20b0..2ca314f8 100644 --- a/rules/no-undelegate.js +++ b/rules/no-undelegate.js @@ -2,25 +2,7 @@ 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 !== 'undelegate') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.off/removeEventListener to undelegate' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'undelegate', + 'Prefer $.off/removeEventListener to undelegate' +) diff --git a/rules/no-unique.js b/rules/no-unique.js index 9bc53eb3..9d0d197e 100644 --- a/rules/no-unique.js +++ b/rules/no-unique.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'unique') return - - context.report({ - node: node, - message: 'Prefer $.uniqueSort to $.unique' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'unique', + 'Prefer $.uniqueSort to $.unique' +) diff --git a/rules/no-unload-shorthand.js b/rules/no-unload-shorthand.js index 5920f915..78ba504a 100644 --- a/rules/no-unload-shorthand.js +++ b/rules/no-unload-shorthand.js @@ -2,26 +2,7 @@ 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.object.name === '$') return - if (node.callee.property.name !== 'unload') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer $.on or $.trigger to $.unload' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'unload', + 'Prefer $.on or $.trigger to $.unload' +) diff --git a/rules/no-val.js b/rules/no-val.js index 96d60149..afc53a9c 100644 --- a/rules/no-val.js +++ b/rules/no-val.js @@ -2,25 +2,7 @@ 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 !== 'val') return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: 'Prefer value to $.val' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + 'val', + 'Prefer value to $.val' +) diff --git a/rules/no-when.js b/rules/no-when.js index ee1b2ae5..bddb3ae1 100644 --- a/rules/no-when.js +++ b/rules/no-when.js @@ -1,23 +1,8 @@ 'use strict' -module.exports = { - meta: { - docs: {}, - schema: [] - }, +const utils = require('./utils.js') - create: function(context) { - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (node.callee.object.name !== '$') return - if (node.callee.property.name !== 'when') return - - context.report({ - node: node, - message: 'Prefer Promise.all to $.when' - }) - } - } - } -} +module.exports = utils.createUtilMethodRule( + 'when', + 'Prefer Promise.all to $.when' +) diff --git a/rules/no-wrap.js b/rules/no-wrap.js index 5b9a8c59..1b7f42f1 100644 --- a/rules/no-wrap.js +++ b/rules/no-wrap.js @@ -2,27 +2,7 @@ const utils = require('./utils.js') -module.exports = { - meta: { - docs: {}, - schema: [] - }, - - create: function(context) { - const forbidden = ['wrap', 'wrapAll', 'wrapInner', 'unwrap'] - - return { - CallExpression: function(node) { - if (node.callee.type !== 'MemberExpression') return - if (forbidden.indexOf(node.callee.property.name) === -1) return - - if (utils.isjQuery(node)) { - context.report({ - node: node, - message: '$.' + node.callee.property.name + ' is not allowed' - }) - } - } - } - } -} +module.exports = utils.createCollectionMethodRule( + ['wrap', 'wrapAll', 'wrapInner', 'unwrap'], + node => `$.${node.callee.property.name} is not allowed` +) diff --git a/rules/utils.js b/rules/utils.js index d94da23a..1bf729df 100644 --- a/rules/utils.js +++ b/rules/utils.js @@ -51,6 +51,108 @@ function isjQuery(node) { return traverse(node, id => !!id && id.name.startsWith('$')) } +function createRule(create) { + return { + meta: { + docs: {}, + schema: [] + }, + create: create + } +} + +function createCollectionMethodRule(methods, message) { + methods = Array.isArray(methods) ? methods : [methods] + return createRule(function(context) { + return { + CallExpression: function(node) { + if (node.callee.type !== 'MemberExpression') return + const name = node.callee.property.name + if (methods.indexOf(name) === -1) return + if (node.callee.object.name === '$') return + + if (isjQuery(node)) { + context.report({ + node: node, + message: + typeof message === 'function' + ? message(node) + : message || '$.' + name + ' is not allowed' + }) + } + } + } + }) +} + +function createCollectionPropertyRule(property, message) { + return createRule(function(context) { + return { + MemberExpression: function(node) { + const name = node.property.name + if (name !== property) return + if (node.parent.callee === node) return + + if (isjQuery(node)) { + context.report({ + node: node, + message: + typeof message === 'function' + ? message(node) + : message || '$.' + name + ' is not allowed' + }) + } + } + } + }) +} + +function createUtilMethodRule(methods, message) { + methods = Array.isArray(methods) ? methods : [methods] + return createRule(function(context) { + return { + CallExpression: function(node) { + if (node.callee.type !== 'MemberExpression') return + const name = node.callee.property.name + if (methods.indexOf(name) === -1) return + if (node.callee.object.name !== '$') return + + context.report({ + node: node, + message: + typeof message === 'function' + ? message(node) + : message || '$.' + name + ' is not allowed' + }) + } + } + }) +} + +function createUtilPropertyRule(property, message) { + return createRule(function(context) { + return { + MemberExpression: function(node) { + if (node.object.name !== '$') return + const name = node.property.name + if (name !== property) return + + context.report({ + node: node, + message: + typeof message === 'function' + ? message(node) + : message || '$.' + name + ' is not allowed' + }) + } + } + }) +} + module.exports = { - isjQuery: isjQuery + isjQuery: isjQuery, + createCollectionMethodRule: createCollectionMethodRule, + createCollectionPropertyRule: createCollectionPropertyRule, + createUtilMethodRule: createUtilMethodRule, + createUtilPropertyRule: createUtilPropertyRule } diff --git a/tests/no-closest.js b/tests/no-closest.js index a47311f4..9973fbca 100644 --- a/tests/no-closest.js +++ b/tests/no-closest.js @@ -3,7 +3,7 @@ const rule = require('../rules/no-closest') const RuleTester = require('eslint').RuleTester -const error = 'Prefer closest to closest' +const error = 'Prefer Element.closest to $.closest' const ruleTester = new RuleTester() ruleTester.run('no-closest', rule, { diff --git a/tests/no-parents.js b/tests/no-parents.js index 156af284..804ba78e 100644 --- a/tests/no-parents.js +++ b/tests/no-parents.js @@ -3,7 +3,7 @@ const rule = require('../rules/no-parents') const RuleTester = require('eslint').RuleTester -const error = 'Prefer closest to $.parents' +const error = 'Prefer Element.closest to $.parents' const ruleTester = new RuleTester() ruleTester.run('no-parents', rule, {