Skip to content

Commit

Permalink
New rule: no-fx
Browse files Browse the repository at this point in the history
In slim mode, $.fx is completely missing, so create a rule
for this (more generic that no-fx-interval, which exists as
that property is deprecated).
  • Loading branch information
edg2s authored and jdforrester committed Nov 19, 2024
1 parent 4039675 commit 238c2ea
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Where rules are included in the configs `recommended`, `slim`, `all` or `depreca
* [`no-jquery/no-find`](docs/rules/no-find.md)
* [`no-jquery/no-find-collection`](docs/rules/no-find-collection.md) `all`
* [`no-jquery/no-find-util`](docs/rules/no-find-util.md) `all`
* [`no-jquery/no-fx`](docs/rules/no-fx.md) `slim`
* [`no-jquery/no-fx-interval`](docs/rules/no-fx-interval.md) `3.0`
* [`no-jquery/no-global-eval`](docs/rules/no-global-eval.md) `all`
* [`no-jquery/no-global-selector`](docs/rules/no-global-selector.md) ⚙️
Expand Down
30 changes: 30 additions & 0 deletions docs/rules/no-fx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[//]: # (This file is generated by eslint-docgen. Do not edit it directly.)

# no-fx

Disallows `$.fx`.

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

## Rule details

❌ Examples of **incorrect** code:
```js
$.fx;
$.fx.interval;
$.fx.off;
$.fx.speeds.slow;
$.fx.start();
```

✔️ Examples of **correct** code:
```js
fx;
fx.interval;
a.fx;
```

## Resources

* [Rule source](/src/rules/no-fx.js)
* [Test source](/tests/rules/no-fx.js)
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
'no-find': require( './rules/no-find' ),
'no-find-collection': require( './rules/no-find-collection' ),
'no-find-util': require( './rules/no-find-util' ),
'no-fx': require( './rules/no-fx' ),
'no-fx-interval': require( './rules/no-fx-interval' ),
'no-global-eval': require( './rules/no-global-eval' ),
'no-global-selector': require( './rules/no-global-selector' ),
Expand Down Expand Up @@ -118,6 +119,7 @@ module.exports = {
'no-jquery/no-animate-toggle': 'error',
'no-jquery/no-fade': 'error',
'no-jquery/no-slide': 'error',
'no-jquery/no-fx': 'error',
// Ajax
'no-jquery/no-ajax': 'error',
'no-jquery/no-ajax-events': 'error',
Expand Down
29 changes: 29 additions & 0 deletions src/rules/no-fx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Disallows `$.fx`.'
},
schema: []
},

create: ( context ) => ( {
MemberExpression: ( node ) => {
if (
!utils.isjQueryConstructor( context, node.object.name ) ||
node.property.name !== 'fx'
) {
return;
}

context.report( {
node,
message: '$.fx is not allowed'
} );
}
} )
};
33 changes: 33 additions & 0 deletions tests/rules/no-fx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const rule = require( '../../src/rules/no-fx' );
const RuleTester = require( '../../tools/rule-tester' );

const error = '$.fx is not allowed';

const ruleTester = new RuleTester();
ruleTester.run( 'no-fx', rule, {
valid: [ 'fx', 'fx.interval', 'a.fx' ],
invalid: [
{
code: '$.fx',
errors: [ error ]
},
{
code: '$.fx.interval',
errors: [ error ]
},
{
code: '$.fx.off',
errors: [ error ]
},
{
code: '$.fx.speeds.slow',
errors: [ error ]
},
{
code: '$.fx.start()',
errors: [ error ]
}
]
} );

0 comments on commit 238c2ea

Please sign in to comment.