Skip to content

Commit

Permalink
Merge branch 'master' into doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jdforrester authored Nov 19, 2024
2 parents 1497668 + 387002b commit d387c50
Show file tree
Hide file tree
Showing 25 changed files with 294 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
Expand All @@ -28,7 +28,7 @@ jobs:
env:
CI: true
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.lcov
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Where rules are included in the configs `recommended`, `slim`, `all` or `depreca
* [`no-jquery/no-data`](docs/rules/no-data.md) `all`
* [`no-jquery/no-deferred`](docs/rules/no-deferred.md) `all`
* [`no-jquery/no-delegate`](docs/rules/no-delegate.md) `3.0`, `all`
* [`no-jquery/no-done-fail`](docs/rules/no-done-fail.md) `all`
* [`no-jquery/no-each`](docs/rules/no-each.md)
* [`no-jquery/no-each-collection`](docs/rules/no-each-collection.md) `all`
* [`no-jquery/no-each-util`](docs/rules/no-each-util.md) `all`
Expand All @@ -120,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
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
27 changes: 27 additions & 0 deletions docs/rules/no-done-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[//]: # (This file is generated by eslint-docgen. Do not edit it directly.)

# no-done-fail

Disallows the [`.done`](https://api.jquery.com/deferred.done/)/[`.fail`](https://api.jquery.com/deferred.fail/) methods. Prefer `.then`.

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

## Rule details

❌ Examples of **incorrect** code:
```js
promise.done( callback );
promise.fail( callback );
```

✔️ Examples of **correct** code:
```js
promise.then( doneCallback, failCallback );
done();
fail();
```

## Resources

* [Rule source](/src/rules/no-done-fail.js)
* [Test source](/tests/rules/no-done-fail.js)
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)
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
'no-deferred': require( './rules/no-deferred' ),
'no-delegate': require( './rules/no-delegate' ),
'no-die': require( './rules/no-die' ),
'no-done-fail': require( './rules/no-done-fail' ),
'no-each': require( './rules/no-each' ),
'no-each-collection': require( './rules/no-each-collection' ),
'no-each-util': require( './rules/no-each-util' ),
Expand All @@ -38,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 @@ -117,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 Expand Up @@ -333,8 +336,10 @@ module.exports = {
'no-jquery/no-filter': 'warn',
'no-jquery/no-prop': 'warn',
'no-jquery/no-sub': 'warn',
'no-jquery/no-text': 'warn'
'no-jquery/no-text': 'warn',

// Other methods
'no-jquery/no-done-fail': 'warn'
}
}
}
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
11 changes: 11 additions & 0 deletions src/rules/no-done-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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

module.exports = utils.createUniversalMethodRule(
[ 'done', 'fail' ],
( node ) => node === true ?
'Prefer `.then`' :
`Prefer .then to .${ node.callee.property.name }`,
( method ) => `[\`.${ method }\`](https://api.jquery.com/deferred.${ method }/)`
);
5 changes: 3 additions & 2 deletions src/rules/no-event-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ const rule = utils.createCollectionMethodRule(
'mousemove',
'mouseout',
'mouseover',
'mouseup'
].concat( ajaxEvents ),
'mouseup',
...ajaxEvents
],
( node ) => node === true ?
'Use the `allowAjaxEvents` option to allow `ajax*` methods. Prefer `.on` or `.trigger`' :
`Prefer .on or .trigger to .${ node.callee.property.name }`,
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'
} );
}
} )
};
42 changes: 42 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,47 @@ function createCollectionOrUtilMethodRule( methods, message, options ) {
} ), description, options.fixable, options.deprecated );
}

/**
* Create a rule for a method on any object
*
* @param {string|string[]} methods Method or list of method names
* @param {string|Function} message Message to report. See createCollectionMethodRule.
* @param {Function} linkGenerator Function to generate a markdown link
* @param {Object} [options] Options. See createCollectionMethodRule.
* for a given function name.
* @return {Object} Rule
*/
function createUniversalMethodRule( methods, message, linkGenerator, options ) {
options = options || {};

options.mode = 'util';

methods = Array.isArray( methods ) ? methods : [ methods ];

let description = 'Disallows the ' + methods.map( linkGenerator ).join( '/' ) + ' ' +
( methods.length > 1 ? 'methods' : 'method' ) + '.';

description += messageSuffix( message );

return createRule( ( context ) => ( {
'CallExpression:exit': ( node ) => {
if ( node.callee.type !== 'MemberExpression' ) {
return;
}
const name = node.callee.property.name;
if ( !methods.includes( name ) ) {
return;
}

context.report( {
node,
message: messageToPlainString( message, node, name, options ),
fix: options.fix && options.fix.bind( this, node, context )
} );
}
} ), description, options.fixable, options.deprecated );
}

function eventShorthandFixer( node, context, fixer ) {
const name = node.callee.property.name;
if ( node.callee.parent.arguments.length ) {
Expand Down Expand Up @@ -580,6 +621,7 @@ module.exports = {
createUtilMethodRule,
createUtilPropertyRule,
createCollectionOrUtilMethodRule,
createUniversalMethodRule,
eventShorthandFixer,
jQueryCollectionLink,
jQueryGlobalLink,
Expand Down
2 changes: 1 addition & 1 deletion test-self/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"root": true,
"extends": "wikimedia/client-es5",
"extends": "wikimedia/client",
"plugins": [ "self" ],
"globals": {
"$": "readonly"
Expand Down
2 changes: 1 addition & 1 deletion test-self/all/constructor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line self/no-jquery-constructor
$( function () {} );
$( () => {} );

// eslint-disable-next-line self/no-jquery-constructor
$( '.selector' );
Expand Down
6 changes: 6 additions & 0 deletions test-self/all/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,9 @@ $x.wrap();
$x.wrapAll();
// eslint-disable-next-line self/no-wrap
$x.wrapInner();

// Other methods
// eslint-disable-next-line self/no-done-fail
promise.done();
// eslint-disable-next-line self/no-done-fail
promise.fail();
6 changes: 3 additions & 3 deletions test-self/deprecated-1.8/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ $x.andSelf();
$x.error( fn );

// eslint-disable-next-line self/no-load-shorthand
$x.load( function () {} );
$x.load( () => {} );

// eslint-disable-next-line self/no-on-ready
$( document ).on( 'ready', function () {} );
$( document ).on( 'ready', () => {} );

// eslint-disable-next-line self/no-size
$x.size();

// eslint-disable-next-line self/no-unload-shorthand
$x.unload( function () {} );
$x.unload( () => {} );

/* 1.7 */
// eslint-disable-next-line self/no-live
Expand Down
2 changes: 1 addition & 1 deletion test-self/recommended/test.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line self/variable-pattern
var foo = $( '.foo' );
const foo = $( '.foo' );
2 changes: 1 addition & 1 deletion test-self/slim/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $x.slideDown();
// eslint-disable-next-line self/no-ajax
$.get( 'url' );
// eslint-disable-next-line self/no-ajax-events
$x.on( 'ajaxComplete', function () {} );
$x.on( 'ajaxComplete', () => {} );
// eslint-disable-next-line self/no-load
$x.load();
// eslint-disable-next-line self/no-parse-xml
Expand Down
Loading

0 comments on commit d387c50

Please sign in to comment.