Skip to content

Commit

Permalink
Merge event unbinding rules in binding rules and deprecate (#186)
Browse files Browse the repository at this point in the history
Merges:
* no-die -> no-live
* no-unbind -> no-bind
* no-undelegate -> no-delegate

Part of #163
  • Loading branch information
edg2s authored and jdforrester committed Dec 17, 2019
1 parent ced013c commit faf961d
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 38 deletions.
11 changes: 10 additions & 1 deletion docs/no-bind.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand All @@ -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:
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/no-die.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/no-live.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand Down
2 changes: 1 addition & 1 deletion docs/no-unbind.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/no-undelegate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down
9 changes: 7 additions & 2 deletions rules/no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
);
3 changes: 2 additions & 1 deletion rules/no-die.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] }
);
9 changes: 7 additions & 2 deletions rules/no-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
);
3 changes: 2 additions & 1 deletion rules/no-unbind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] }
);
3 changes: 2 additions & 1 deletion rules/no-undelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] }
);
2 changes: 1 addition & 1 deletion test-all/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
41 changes: 22 additions & 19 deletions tests/no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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' } ]
} ) )
} );
2 changes: 1 addition & 1 deletion tests/no-die.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion tests/no-unbind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion tests/no-undelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit faf961d

Please sign in to comment.