Skip to content

Commit f48ff25

Browse files
edg2sjdforrester
authored andcommitted
Rule fix: Add stop and finish methods to no-animate
These are core animation methods that aren't covered in any other animation rules (e.g. `no-fade`).
1 parent 4039675 commit f48ff25

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

docs/rules/no-animate.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# no-animate
44

5-
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.
5+
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.
66

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

@@ -13,6 +13,8 @@ Disallows the [`.animate`](https://api.jquery.com/animate/) method. Use the `all
1313
❌ Examples of **incorrect** code:
1414
```js
1515
$( 'div' ).animate();
16+
$( 'div' ).stop();
17+
$( 'div' ).finish();
1618
$div.animate();
1719
$( 'div' ).first().animate();
1820
$( 'div' ).append( $( 'input' ).animate() );
@@ -28,6 +30,14 @@ animate();
2830
[].animate();
2931
div.animate();
3032
div.animate;
33+
stop();
34+
[].stop();
35+
div.stop();
36+
div.stop;
37+
finish();
38+
[].finish();
39+
div.finish();
40+
div.finish;
3141
```
3242

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

4456
✔️ Examples of **correct** code with `[{"allowScroll":true}]` options:

src/rules/no-animate.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

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

5+
const methods = [ 'animate', 'stop', 'finish' ];
6+
57
module.exports = {
68
meta: {
79
type: 'suggestion',
810
docs: {
911
description:
10-
'Disallows the ' + utils.jQueryCollectionLink( 'animate' ) +
11-
' method. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.'
12+
'Disallows the ' + methods.map( utils.jQueryCollectionLink ).join( '/' ) +
13+
' methods. Use the `allowScroll` option to allow animations which are just used for scrolling. Prefer CSS transitions.'
1214
},
1315
schema: [
1416
{
@@ -27,12 +29,12 @@ module.exports = {
2729
'CallExpression:exit': ( node ) => {
2830
if (
2931
node.callee.type !== 'MemberExpression' ||
30-
node.callee.property.name !== 'animate'
32+
!methods.includes( node.callee.property.name )
3133
) {
3234
return;
3335
}
3436
const allowScroll = context.options[ 0 ] && context.options[ 0 ].allowScroll;
35-
if ( allowScroll ) {
37+
if ( node.callee.property.name === 'animate' && allowScroll ) {
3638
const arg = node.arguments[ 0 ];
3739
// Check properties list has more than just scrollTop/scrollLeft
3840
if ( arg && arg.type === 'ObjectExpression' ) {

tests/rules/no-animate.js

+26
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ ruleTester.run( 'no-animate', rule, {
1313
'[].animate()',
1414
'div.animate()',
1515
'div.animate',
16+
'stop()',
17+
'[].stop()',
18+
'div.stop()',
19+
'div.stop',
20+
'finish()',
21+
'[].finish()',
22+
'div.finish()',
23+
'div.finish',
1624
{
1725
code: '$div.animate({scrollTop: 100})',
1826
options: [ { allowScroll: true } ]
@@ -31,6 +39,14 @@ ruleTester.run( 'no-animate', rule, {
3139
code: '$("div").animate()',
3240
errors: [ error ]
3341
},
42+
{
43+
code: '$("div").stop()',
44+
errors: [ error ]
45+
},
46+
{
47+
code: '$("div").finish()',
48+
errors: [ error ]
49+
},
3450
{
3551
code: '$div.animate()',
3652
errors: [ error ]
@@ -73,6 +89,16 @@ ruleTester.run( 'no-animate', rule, {
7389
code: '$div.animate({scrollTop: 100, width: 300})',
7490
options: [ { allowScroll: true } ],
7591
errors: [ errorNoScroll ]
92+
},
93+
{
94+
code: '$("div").stop({scrollTop: 100, scrollLeft: 200})',
95+
options: [ { allowScroll: true } ],
96+
errors: [ errorNoScroll ]
97+
},
98+
{
99+
code: '$("div").finish({scrollTop: 100, scrollLeft: 200})',
100+
options: [ { allowScroll: true } ],
101+
errors: [ errorNoScroll ]
76102
}
77103
]
78104
} );

0 commit comments

Comments
 (0)