Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule fix: Add stop and finish methods to no-animate #339

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/rules/no-animate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# no-animate

Disallows the [`.animate`](https://api.jquery.com/animate/) method. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.
Disallows the [`.animate`](https://api.jquery.com/animate/)/[`.stop`](https://api.jquery.com/stop/)/[`.finish`](https://api.jquery.com/finish/) methods. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.

📋 This rule is enabled in `plugin:no-jquery/slim`.

Expand All @@ -13,6 +13,8 @@ Disallows the [`.animate`](https://api.jquery.com/animate/) method. Use the `all
❌ Examples of **incorrect** code:
```js
$( 'div' ).animate();
$( 'div' ).stop();
$( 'div' ).finish();
$div.animate();
$( 'div' ).first().animate();
$( 'div' ).append( $( 'input' ).animate() );
Expand All @@ -28,6 +30,14 @@ animate();
[].animate();
div.animate();
div.animate;
stop();
[].stop();
div.stop();
div.stop;
finish();
[].finish();
div.finish();
div.finish;
```

❌ Examples of **incorrect** code with `[{"allowScroll":false}]` options:
Expand All @@ -39,6 +49,8 @@ $div.animate( { scrollTop: 100 } );
```js
$div.animate();
$div.animate( { scrollTop: 100, width: 300 } );
$( 'div' ).stop( { scrollTop: 100, scrollLeft: 200 } );
$( 'div' ).finish( { scrollTop: 100, scrollLeft: 200 } );
```

✔️ Examples of **correct** code with `[{"allowScroll":true}]` options:
Expand Down
10 changes: 6 additions & 4 deletions src/rules/no-animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

const utils = require( '../utils.js' );

const methods = [ 'animate', 'stop', 'finish' ];

module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'Disallows the ' + utils.jQueryCollectionLink( 'animate' ) +
' method. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.'
'Disallows the ' + methods.map( utils.jQueryCollectionLink ).join( '/' ) +
' methods. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.'
},
schema: [
{
Expand All @@ -27,12 +29,12 @@ module.exports = {
'CallExpression:exit': ( node ) => {
if (
node.callee.type !== 'MemberExpression' ||
node.callee.property.name !== 'animate'
!methods.includes( node.callee.property.name )
) {
return;
}
const allowScroll = context.options[ 0 ] && context.options[ 0 ].allowScroll;
if ( allowScroll ) {
if ( node.callee.property.name === 'animate' && allowScroll ) {
const arg = node.arguments[ 0 ];
// Check properties list has more than just scrollTop/scrollLeft
if ( arg && arg.type === 'ObjectExpression' ) {
Expand Down
26 changes: 26 additions & 0 deletions tests/rules/no-animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ ruleTester.run( 'no-animate', rule, {
'[].animate()',
'div.animate()',
'div.animate',
'stop()',
'[].stop()',
'div.stop()',
'div.stop',
'finish()',
'[].finish()',
'div.finish()',
'div.finish',
{
code: '$div.animate({scrollTop: 100})',
options: [ { allowScroll: true } ]
Expand All @@ -31,6 +39,14 @@ ruleTester.run( 'no-animate', rule, {
code: '$("div").animate()',
errors: [ error ]
},
{
code: '$("div").stop()',
errors: [ error ]
},
{
code: '$("div").finish()',
errors: [ error ]
},
{
code: '$div.animate()',
errors: [ error ]
Expand Down Expand Up @@ -73,6 +89,16 @@ ruleTester.run( 'no-animate', rule, {
code: '$div.animate({scrollTop: 100, width: 300})',
options: [ { allowScroll: true } ],
errors: [ errorNoScroll ]
},
{
code: '$("div").stop({scrollTop: 100, scrollLeft: 200})',
options: [ { allowScroll: true } ],
errors: [ errorNoScroll ]
},
{
code: '$("div").finish({scrollTop: 100, scrollLeft: 200})',
options: [ { allowScroll: true } ],
errors: [ errorNoScroll ]
}
]
} );
Loading