Skip to content

Commit

Permalink
Add fixer for no-ready-shorthand
Browse files Browse the repository at this point in the history
Part of #115
  • Loading branch information
edg2s committed Oct 8, 2020
1 parent 6f55bb2 commit 6848f76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
30 changes: 20 additions & 10 deletions docs/rules/no-ready-shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ Disallows the [`.ready`](https://api.jquery.com/ready/) method. Prefer `$()`.

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

🔧 The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## Rule details

❌ Examples of **incorrect** code:
```js
$( document ).ready();
$div.ready();
$( 'div' ).first().ready();
$( 'div' ).append( $( 'input' ).ready() );
$( document ).ready( fn );
$div.ready( fn );
$( 'div' ).first().ready( fn );
$( 'div' ).append( $( 'input' ).ready( fn ) );
$div = $( 'div' ).ready( fn );
```

✔️ Examples of **correct** code:
```js
ready();
[].ready();
div.ready();
ready( fn );
[].ready( fn );
div.ready( fn );
div.ready;
$.ready();
$( document ).on( 'ready', function () {} );
$( function () {} );
$.ready( fn );
$( document ).on( 'ready', fn );
$( fn );
```

🔧 Examples of code **fixed** by this rule:
```js
$( document ).ready( fn ); /**/ $( fn );
$div.ready( fn ); /**/ $( fn );
$( 'div' ).first().ready( fn ); /**/ $( fn );
```

## Resources
Expand Down
10 changes: 9 additions & 1 deletion src/rules/no-ready-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ const utils = require( '../utils.js' );

module.exports = utils.createCollectionMethodRule(
'ready',
'Prefer `$()` to `.ready`'
'Prefer `$()` to `.ready`',
{
fixable: 'code',
fix: function ( node, fixer ) {
if ( node.parent.type === 'ExpressionStatement' ) {
return fixer.replaceText( node.callee, '$' );
}
}
}
);
32 changes: 20 additions & 12 deletions tests/rules/no-ready-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@ const error = 'Prefer $() to .ready';
const ruleTester = new RuleTester();
ruleTester.run( 'no-ready-shorthand', rule, {
valid: [
'ready()',
'[].ready()',
'div.ready()',
'ready(fn)',
'[].ready(fn)',
'div.ready(fn)',
'div.ready',
'$.ready()',
'$(document).on("ready", function(){})',
'$(function(){})'
'$.ready(fn)',
'$(document).on("ready", fn)',
'$(fn)'
],
invalid: [
{
code: '$(document).ready()',
errors: [ error ]
code: '$(document).ready(fn)',
errors: [ error ],
output: '$(fn)'
},
{
code: '$div.ready()',
errors: [ error ]
code: '$div.ready(fn)',
errors: [ error ],
output: '$(fn)'
},
{
code: '$("div").first().ready(fn)',
errors: [ error ],
output: '$(fn)'
},
// Can't fix if the result might be used
{
code: '$("div").first().ready()',
code: '$("div").append($("input").ready(fn))',
errors: [ error ]
},
{
code: '$("div").append($("input").ready())',
code: '$div = $("div").ready(fn)',
errors: [ error ]
}
]
Expand Down

0 comments on commit 6848f76

Please sign in to comment.