From 442d418ad2d28f796b593283e00dc49abe65749d Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Tue, 19 Nov 2024 14:58:26 +0000 Subject: [PATCH] New rule: `no-fx` 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). --- README.md | 1 + docs/rules/no-fx.md | 30 ++++++++++++++++++++++++++++++ src/index.js | 2 ++ src/rules/no-fx.js | 29 +++++++++++++++++++++++++++++ tests/rules/no-fx.js | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 docs/rules/no-fx.md create mode 100644 src/rules/no-fx.js create mode 100644 tests/rules/no-fx.js diff --git a/README.md b/README.md index 07294f8..7d6c9be 100644 --- a/README.md +++ b/README.md @@ -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) ⚙️ diff --git a/docs/rules/no-fx.md b/docs/rules/no-fx.md new file mode 100644 index 0000000..1e14efc --- /dev/null +++ b/docs/rules/no-fx.md @@ -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) diff --git a/src/index.js b/src/index.js index e8b0f1e..c5d492e 100644 --- a/src/index.js +++ b/src/index.js @@ -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' ), @@ -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', diff --git a/src/rules/no-fx.js b/src/rules/no-fx.js new file mode 100644 index 0000000..89fac82 --- /dev/null +++ b/src/rules/no-fx.js @@ -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' + } ); + } + } ) +}; diff --git a/tests/rules/no-fx.js b/tests/rules/no-fx.js new file mode 100644 index 0000000..32b1404 --- /dev/null +++ b/tests/rules/no-fx.js @@ -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 ] + } + ] +} );