diff --git a/docs/no-bind.md b/docs/no-bind.md index 06d2d253..0d626c9d 100644 --- a/docs/no-bind.md +++ b/docs/no-bind.md @@ -1,6 +1,6 @@ # no-bind -Disallows the `.bind` method. Prefer `$.on`/`EventTarget#addEventListener` to `$.bind`. +Disallows the `.bind`/`.unbind` methods. Prefer `$.on`/`$.off` or `EventTarget#addEventListener`/`removeEventListener`. This rule is enabled in `plugin:no-jquery/deprecated-3.0`. @@ -12,6 +12,10 @@ $( 'div' ).bind(); $div.bind(); $( 'div' ).first().bind(); $( 'div' ).append( $( 'input' ).bind() ); +$( 'div' ).unbind(); +$div.unbind(); +$( 'div' ).first().unbind(); +$( 'div' ).append( $( 'input' ).unbind() ); ``` ✔️ The following patterns are not considered errors: @@ -21,6 +25,11 @@ bind(); div.bind(); div.bind; $div.remove.bind( $div ); +unbind(); +[].unbind(); +div.unbind(); +div.unbind; +$div.remove.unbind( $div ); ``` ## Rule source diff --git a/docs/no-die.md b/docs/no-die.md index ad8ca342..6d387d83 100644 --- a/docs/no-die.md +++ b/docs/no-die.md @@ -2,7 +2,7 @@ Disallows the `.die` method. Prefer `$.off`/`EventTarget#removeEventListener` to `$.die`. -This rule is enabled in `plugin:no-jquery/deprecated-1.7`. +⚠️ This rule is deprecated. Use [no-live](no-live.md) instead. ## Rule details diff --git a/docs/no-live.md b/docs/no-live.md index 6f43e0c5..cb552814 100644 --- a/docs/no-live.md +++ b/docs/no-live.md @@ -1,6 +1,6 @@ # no-live -Disallows the `.live` method. Prefer `$.on`/`EventTarget#addEventListener` to `$.live`. +Disallows the `.live`/`.die` methods. Prefer `$.on`/`$.off` or `EventTarget#addEventListener`/`removeEventListener`. This rule is enabled in `plugin:no-jquery/deprecated-1.7`. diff --git a/docs/no-unbind.md b/docs/no-unbind.md index f8f2f1b1..9abd00ea 100644 --- a/docs/no-unbind.md +++ b/docs/no-unbind.md @@ -2,7 +2,7 @@ Disallows the `.unbind` method. Prefer `$.off`/`EventTarget#removeEventListener` to `$.unbind`. -This rule is enabled in `plugin:no-jquery/deprecated-3.0`. +⚠️ This rule is deprecated. Use [no-bind](no-bind.md) instead. ## Rule details diff --git a/docs/no-undelegate.md b/docs/no-undelegate.md index 70d1afe1..838be2f2 100644 --- a/docs/no-undelegate.md +++ b/docs/no-undelegate.md @@ -2,7 +2,7 @@ Disallows the `.undelegate` method. Prefer `$.off`/`EventTarget#removeEventListener` to `$.undelegate`. -This rule is enabled in `plugin:no-jquery/deprecated-3.0`. +⚠️ This rule is deprecated. Use [no-delegate](no-delegate.md) instead. ## Rule details diff --git a/index.js b/index.js index 4cc5e182..5af1a205 100644 --- a/index.js +++ b/index.js @@ -151,9 +151,7 @@ module.exports = { extends: 'plugin:no-jquery/deprecated-1.10', rules: { 'no-jquery/no-bind': 'error', - 'no-jquery/no-unbind': 'error', 'no-jquery/no-delegate': 'error', - 'no-jquery/no-undelegate': 'error', 'no-jquery/no-fx-interval': 'error', 'no-jquery/no-parse-json': 'error', 'no-jquery/no-ready-shorthand': 'error', @@ -193,7 +191,6 @@ module.exports = { extends: 'plugin:no-jquery/deprecated-1.3', rules: { // FIXME: `deferred.isRejected()/isResolved()` - 'no-jquery/no-die': 'error', 'no-jquery/no-live': 'error', 'no-jquery/no-sub': 'error', 'no-jquery/no-selector-prop': 'error' diff --git a/rules/no-bind.js b/rules/no-bind.js index 2d0b61fb..d9bcf6ce 100644 --- a/rules/no-bind.js +++ b/rules/no-bind.js @@ -3,6 +3,11 @@ const utils = require( './utils.js' ); module.exports = utils.createCollectionMethodRule( - 'bind', - 'Prefer `$.on`/`EventTarget#addEventListener` to `$.bind`' + [ 'bind', 'unbind' ], + ( node ) => node === true ? + 'Prefer `$.on`/`$.off` or `EventTarget#addEventListener`/`removeEventListener`' : ( + node.callee.property.name === 'bind' ? + 'Prefer $.on/EventTarget#addEventListener to $.bind' : + 'Prefer $.off/EventTarget#removeEventListener to $.unbind' + ) ); diff --git a/rules/no-die.js b/rules/no-die.js index 4e84e005..478991f6 100644 --- a/rules/no-die.js +++ b/rules/no-die.js @@ -4,5 +4,6 @@ const utils = require( './utils.js' ); module.exports = utils.createCollectionMethodRule( 'die', - 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.die`' + 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.die`', + { deprecated: [ 'no-live' ] } ); diff --git a/rules/no-live.js b/rules/no-live.js index 61f951be..75d1d9b1 100644 --- a/rules/no-live.js +++ b/rules/no-live.js @@ -3,6 +3,11 @@ const utils = require( './utils.js' ); module.exports = utils.createCollectionMethodRule( - 'live', - 'Prefer `$.on`/`EventTarget#addEventListener` to `$.live`' + [ 'live', 'die' ], + ( node ) => node === true ? + 'Prefer `$.on`/`$.off` or `EventTarget#addEventListener`/`removeEventListener`' : ( + node.callee.property.name === 'live' ? + 'Prefer $.on/EventTarget#addEventListener to $.live' : + 'Prefer $.off/EventTarget#removeEventListener to $.die' + ) ); diff --git a/rules/no-unbind.js b/rules/no-unbind.js index fae2f159..3ab4c3d1 100644 --- a/rules/no-unbind.js +++ b/rules/no-unbind.js @@ -4,5 +4,6 @@ const utils = require( './utils.js' ); module.exports = utils.createCollectionMethodRule( 'unbind', - 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.unbind`' + 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.unbind`', + { deprecated: [ 'no-bind' ] } ); diff --git a/rules/no-undelegate.js b/rules/no-undelegate.js index 46ad4f49..f82dd903 100644 --- a/rules/no-undelegate.js +++ b/rules/no-undelegate.js @@ -4,5 +4,6 @@ const utils = require( './utils.js' ); module.exports = utils.createCollectionMethodRule( 'undelegate', - 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.undelegate`' + 'Prefer `$.off`/`EventTarget#removeEventListener` to `$.undelegate`', + { deprecated: [ 'no-delegate' ] } ); diff --git a/test-all/methods.js b/test-all/methods.js index cc9e9a15..12473e62 100644 --- a/test-all/methods.js +++ b/test-all/methods.js @@ -211,7 +211,7 @@ $x.toggle(); $x.toggleClass(); // eslint-disable-next-line rulesdir/no-trigger $x.trigger(); -// eslint-disable-next-line rulesdir/no-unbind +// eslint-disable-next-line rulesdir/no-bind, rulesdir/no-unbind $x.unbind(); // eslint-disable-next-line rulesdir/no-undelegate $x.undelegate(); diff --git a/tests/no-bind.js b/tests/no-bind.js index c0f89160..38e74ffd 100644 --- a/tests/no-bind.js +++ b/tests/no-bind.js @@ -3,7 +3,8 @@ const rule = require( '../rules/no-bind' ); const RuleTesterAndDocs = require( '../rule-tester-and-docs' ); -const error = 'Prefer $.on/EventTarget#addEventListener to $.bind'; +const bindError = 'Prefer $.on/EventTarget#addEventListener to $.bind'; +const unbindError = 'Prefer $.off/EventTarget#removeEventListener to $.unbind'; const ruleTester = new RuleTesterAndDocs(); ruleTester.run( 'no-bind', rule, { @@ -12,24 +13,26 @@ ruleTester.run( 'no-bind', rule, { '[].bind()', 'div.bind()', 'div.bind', - '$div.remove.bind($div)' + '$div.remove.bind($div)', + + 'unbind()', + '[].unbind()', + 'div.unbind()', + 'div.unbind', + '$div.remove.unbind($div)' ], invalid: [ - { - code: '$("div").bind()', - errors: [ { message: error, type: 'CallExpression' } ] - }, - { - code: '$div.bind()', - errors: [ { message: error, type: 'CallExpression' } ] - }, - { - code: '$("div").first().bind()', - errors: [ { message: error, type: 'CallExpression' } ] - }, - { - code: '$("div").append($("input").bind())', - errors: [ { message: error, type: 'CallExpression' } ] - } - ] + [ '$("div").bind()', bindError ], + [ '$div.bind()', bindError ], + [ '$("div").first().bind()', bindError ], + [ '$("div").append($("input").bind())', bindError ], + + [ '$("div").unbind()', unbindError ], + [ '$div.unbind()', unbindError ], + [ '$("div").first().unbind()', unbindError ], + [ '$("div").append($("input").unbind())', unbindError ] + ].map( ( codeError ) => ( { + code: codeError[ 0 ], + errors: [ { message: codeError[ 1 ], type: 'CallExpression' } ] + } ) ) } ); diff --git a/tests/no-die.js b/tests/no-die.js index b22dd82d..0c9510d0 100644 --- a/tests/no-die.js +++ b/tests/no-die.js @@ -3,7 +3,7 @@ const rule = require( '../rules/no-die' ); const RuleTesterAndDocs = require( '../rule-tester-and-docs' ); -const error = 'Prefer $.off/EventTarget#removeEventListener to $.die'; +const error = 'Prefer $.off/EventTarget#removeEventListener to $.die. This rule is deprecated, use no-live.'; const ruleTester = new RuleTesterAndDocs(); ruleTester.run( 'no-die', rule, { diff --git a/tests/no-unbind.js b/tests/no-unbind.js index f2ba18e1..2831eaaa 100644 --- a/tests/no-unbind.js +++ b/tests/no-unbind.js @@ -3,7 +3,7 @@ const rule = require( '../rules/no-unbind' ); const RuleTesterAndDocs = require( '../rule-tester-and-docs' ); -const error = 'Prefer $.off/EventTarget#removeEventListener to $.unbind'; +const error = 'Prefer $.off/EventTarget#removeEventListener to $.unbind. This rule is deprecated, use no-bind.'; const ruleTester = new RuleTesterAndDocs(); ruleTester.run( 'no-unbind', rule, { diff --git a/tests/no-undelegate.js b/tests/no-undelegate.js index 99b61080..febb008f 100644 --- a/tests/no-undelegate.js +++ b/tests/no-undelegate.js @@ -3,7 +3,7 @@ const rule = require( '../rules/no-undelegate' ); const RuleTesterAndDocs = require( '../rule-tester-and-docs' ); -const error = 'Prefer $.off/EventTarget#removeEventListener to $.undelegate'; +const error = 'Prefer $.off/EventTarget#removeEventListener to $.undelegate. This rule is deprecated, use no-delegate.'; const ruleTester = new RuleTesterAndDocs(); ruleTester.run( 'no-undelegate', rule, {