Skip to content

Commit

Permalink
Add fixer for no-event-shorthand and similar (#154)
Browse files Browse the repository at this point in the history
Also adds fixers for:
* `no-error-shorthand`
* `no-load-shorthand`
* `no-unload-shorthand`

Add 'load' and 'unload' to `no-event-shorthand`.

See #115
  • Loading branch information
edg2s authored and jdforrester committed Oct 15, 2019
1 parent 5fe47c8 commit 710a244
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 93 deletions.
8 changes: 7 additions & 1 deletion docs/no-error-shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This rule is enabled in `plugin:no-jquery/deprecated-1.8`.

❌ The following patterns are considered errors:
```js
$( 'div' ).error();
$( 'div' ).error( handler );
$div.error();
$( 'div' ).first().error();
$( 'div' ).append( $( 'input' ).error() );
Expand All @@ -22,3 +22,9 @@ div.error();
div.error;
$.error();
```

🔧 The `--fix` option can be used to fix problems reported by this rule:
```js
$( 'div' ).error( handler ); /**/ $( 'div' ).on( 'error', handler );
$div.error(); /**/ $div.trigger( 'error' );
```
276 changes: 215 additions & 61 deletions docs/no-event-shorthand.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions docs/no-load-shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This rule is enabled in `plugin:no-jquery/deprecated-1.8`.
```js
$( 'div' ).load( function () {} );
$div.load( function () {} );
$div.load();
$div.load( () => {} );
$( 'div' ).first().load( function () {} );
$( 'div' ).append( $( 'input' ).load( function () {} ) );
Expand All @@ -25,3 +26,9 @@ $.load();
$div.load( 'url' );
$div.load( couldBeUrl );
```

🔧 The `--fix` option can be used to fix problems reported by this rule:
```js
$( 'div' ).load( function () {} ); /**/ $( 'div' ).on( 'load', function () {} );
$div.load(); /**/ $div.trigger( 'load' );
```
8 changes: 7 additions & 1 deletion docs/no-unload-shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This rule is enabled in `plugin:no-jquery/deprecated-1.8`.

❌ The following patterns are considered errors:
```js
$( 'div' ).unload();
$( 'div' ).unload( handler );
$div.unload();
$( 'div' ).first().unload();
$( 'div' ).append( $( 'input' ).unload() );
Expand All @@ -22,3 +22,9 @@ div.unload();
div.unload;
$.unload();
```

🔧 The `--fix` option can be used to fix problems reported by this rule:
```js
$( 'div' ).unload( handler ); /**/ $( 'div' ).on( 'unload', handler );
$div.unload(); /**/ $div.trigger( 'unload' );
```
4 changes: 3 additions & 1 deletion rules/no-error-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const utils = require( './utils.js' );

module.exports = utils.createCollectionMethodRule(
'error',
'Prefer $.on or $.trigger to $.error'
'Prefer $.on or $.trigger to $.error',
'code',
utils.eventShorthandFixer
);
6 changes: 5 additions & 1 deletion rules/no-event-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module.exports = utils.createCollectionMethodRule(
[
// Browser
'error',
'load',
'resize',
'scroll',
'unload',
// Form
'blur',
'change',
Expand Down Expand Up @@ -40,5 +42,7 @@ module.exports = utils.createCollectionMethodRule(
'ajaxSuccess',
'ajaxSend'
],
( node ) => `Prefer $.on or $.trigger to $.${node.callee.property.name}`
( node ) => `Prefer $.on or $.trigger to $.${node.callee.property.name}`,
'code',
utils.eventShorthandFixer
);
19 changes: 11 additions & 8 deletions rules/no-load-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ module.exports = {
docs: {
description: 'Disallows the .load method when used as a shorthand for `.on( \'load\', function )` or `.trigger( \'load\' )`. Prefer $.on or $.trigger.'
},
fixable: 'code',
schema: []
},

create: function ( context ) {
return {
CallExpression: function ( node ) {
if (
node.callee.type !== 'MemberExpression' ||
utils.isjQueryConstructor( context, node.callee.object.name ) ||
node.callee.property.name !== 'load' ||
!node.arguments[ 0 ] ||
!utils.isFunction( node.arguments[ 0 ] )
) {
if ( !(
node.callee.type === 'MemberExpression' &&
!utils.isjQueryConstructor( context, node.callee.object.name ) &&
node.callee.property.name === 'load' && (
node.arguments.length === 0 ||
utils.isFunction( node.arguments[ 0 ] )
)
) ) {
return;
}

if ( utils.isjQuery( context, node.callee ) ) {
context.report( {
node: node,
message: 'Prefer $.on or $.trigger to $.load'
message: 'Prefer $.on or $.trigger to $.load',
fix: utils.eventShorthandFixer.bind( this, node )
} );
}
}
Expand Down
4 changes: 3 additions & 1 deletion rules/no-unload-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const utils = require( './utils.js' );

module.exports = utils.createCollectionMethodRule(
'unload',
'Prefer $.on or $.trigger to $.unload'
'Prefer $.on or $.trigger to $.unload',
'code',
utils.eventShorthandFixer
);
18 changes: 17 additions & 1 deletion rules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ function createCollectionOrUtilMethodRule( methods, message ) {
}, description );
}

function eventShorthandFixer( node, fixer ) {
const name = node.callee.property.name;
if ( node.callee.parent.arguments.length ) {
return [
fixer.replaceText( node.callee.property, 'on' ),
fixer.insertTextBefore( node.callee.parent.arguments[ 0 ], JSON.stringify( name ) + ', ' )
];
} else {
return [
fixer.replaceText( node.callee.property, 'trigger' ),
fixer.insertTextBeforeRange( [ node.end - 1 ], JSON.stringify( name ) )
];
}
}

module.exports = {
isjQuery: isjQuery,
isjQueryConstructor: isjQueryConstructor,
Expand All @@ -361,5 +376,6 @@ module.exports = {
createCollectionPropertyRule: createCollectionPropertyRule,
createUtilMethodRule: createUtilMethodRule,
createUtilPropertyRule: createUtilPropertyRule,
createCollectionOrUtilMethodRule: createCollectionOrUtilMethodRule
createCollectionOrUtilMethodRule: createCollectionOrUtilMethodRule,
eventShorthandFixer: eventShorthandFixer
};
8 changes: 5 additions & 3 deletions tests/no-error-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ ruleTester.run( 'no-error-shorthand', rule, {
valid: [ 'error()', '[].error()', 'div.error()', 'div.error', '$.error()' ],
invalid: [
{
code: '$("div").error()',
errors: [ { message: error, type: 'CallExpression' } ]
code: '$("div").error(handler)',
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").on("error", handler)'
},
{
code: '$div.error()',
errors: [ { message: error, type: 'CallExpression' } ]
errors: [ { message: error, type: 'CallExpression' } ],
output: '$div.trigger("error")'
},
{
code: '$("div").first().error()',
Expand Down
19 changes: 12 additions & 7 deletions tests/no-event-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,29 @@ forbidden.forEach( function ( rule ) {
);
invalid = invalid.concat(
{
code: '$("div").' + rule + '()',
errors: [ { message: error, type: 'CallExpression' } ]
code: '$("div").' + rule + '(handler)',
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").on("' + rule + '", handler)'
},
{
code: '$div.' + rule + '()',
errors: [ { message: error, type: 'CallExpression' } ]
errors: [ { message: error, type: 'CallExpression' } ],
output: '$div.trigger("' + rule + '")'
},
{
code: 'this.prop.$div.' + rule + '()',
errors: [ { message: error, type: 'CallExpression' } ]
code: 'this.prop.$div.' + rule + '(handler)',
errors: [ { message: error, type: 'CallExpression' } ],
output: 'this.prop.$div.on("' + rule + '", handler)'
},
{
code: '$("div").first().' + rule + '()',
errors: [ { message: error, type: 'CallExpression' } ]
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").first().trigger("' + rule + '")'
},
{
code: '$("div").append($("input").' + rule + '())',
errors: [ { message: error, type: 'CallExpression' } ]
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").append($("input").trigger("' + rule + '"))'
}
);
} );
Expand Down
16 changes: 11 additions & 5 deletions tests/no-load-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ ruleTester.run( 'no-load-shorthand', rule, {
],
invalid: [
{
code: '$("div").load(function() {})',
errors: [ { message: error, type: 'CallExpression' } ]
code: '$("div").load(function(){})',
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").on("load", function(){})'
},
{
code: '$div.load(function() {})',
code: '$div.load(function(){})',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$div.load()',
errors: [ { message: error, type: 'CallExpression' } ],
output: '$div.trigger("load")'
},
{
code: '$div.load(() => {})',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$("div").first().load(function() {})',
code: '$("div").first().load(function(){})',
errors: [ { message: error, type: 'CallExpression' } ]
},
{
code: '$("div").append($("input").load(function() {}))',
code: '$("div").append($("input").load(function(){}))',
errors: [ { message: error, type: 'CallExpression' } ]
}
]
Expand Down
8 changes: 5 additions & 3 deletions tests/no-unload-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ ruleTester.run( 'no-unload-shorthand', rule, {
],
invalid: [
{
code: '$("div").unload()',
errors: [ { message: error, type: 'CallExpression' } ]
code: '$("div").unload(handler)',
errors: [ { message: error, type: 'CallExpression' } ],
output: '$("div").on("unload", handler)'
},
{
code: '$div.unload()',
errors: [ { message: error, type: 'CallExpression' } ]
errors: [ { message: error, type: 'CallExpression' } ],
output: '$div.trigger("unload")'
},
{
code: '$("div").first().unload()',
Expand Down

0 comments on commit 710a244

Please sign in to comment.