diff --git a/docs/no-is-array.md b/docs/no-is-array.md index f783731b..89e41d83 100644 --- a/docs/no-is-array.md +++ b/docs/no-is-array.md @@ -8,7 +8,7 @@ This rule is enabled in `plugin:no-jquery/deprecated-3.2`. ✗ The following patterns are considered errors: ```js -$.isArray(); +$.isArray( [] ); ``` ✓ The following patterns are not considered errors: diff --git a/rules/no-is-array.js b/rules/no-is-array.js index 224af89d..a9f2ae24 100644 --- a/rules/no-is-array.js +++ b/rules/no-is-array.js @@ -4,5 +4,9 @@ const utils = require( './utils.js' ); module.exports = utils.createUtilMethodRule( 'isArray', - 'Prefer Array.isArray to $.isArray' + 'Prefer Array.isArray to $.isArray', + 'code', + function ( node, fixer ) { + return fixer.replaceText( node.callee, 'Array.isArray' ); + } ); diff --git a/rules/utils.js b/rules/utils.js index c0341bbf..b3bd7b5b 100644 --- a/rules/utils.js +++ b/rules/utils.js @@ -88,12 +88,13 @@ function isFunction( node ) { return node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression'; } -function createRule( create, description ) { +function createRule( create, description, fixable ) { return { meta: { docs: { description: description }, + fixable: fixable, schema: [] }, create: create @@ -168,7 +169,7 @@ function createCollectionPropertyRule( property, message ) { }, description ); } -function createUtilMethodRule( methods, message ) { +function createUtilMethodRule( methods, message, fixable, fix ) { methods = Array.isArray( methods ) ? methods : [ methods ]; let description = 'Disallows the $.' + methods.join( '/' ) + ' ' + @@ -196,11 +197,12 @@ function createUtilMethodRule( methods, message ) { node: node, message: typeof message === 'function' ? message( node ) : - message || '$.' + name + ' is not allowed' + message || '$.' + name + ' is not allowed', + fix: fix && fix.bind( this, node ) } ); } }; - }, description ); + }, description, fixable ); } function createUtilPropertyRule( property, message ) { diff --git a/tests/no-is-array.js b/tests/no-is-array.js index 5344310a..fbde7cda 100644 --- a/tests/no-is-array.js +++ b/tests/no-is-array.js @@ -10,8 +10,9 @@ ruleTester.run( 'no-is-array', rule, { valid: [ 'isArray()', '"test".isArray()', '"test".isArray' ], invalid: [ { - code: '$.isArray()', - errors: [ { message: error, type: 'CallExpression' } ] + code: '$.isArray( [] )', + errors: [ { message: error, type: 'CallExpression' } ], + output: 'Array.isArray( [] )' } ] } );