Skip to content

Commit 4de1d34

Browse files
authored
Implement a fixer for no-extend rule (#313)
Replaces $.extend with Object.assign if the first argument is not literal true.
1 parent 2166431 commit 4de1d34

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Where rules are included in the configs `recommended`, `slim`, `all` or `depreca
114114
* [`no-jquery/no-error-shorthand`](docs/rules/no-error-shorthand.md) 🔧 `1.8`
115115
* [`no-jquery/no-escape-selector`](docs/rules/no-escape-selector.md) 🔧 `all`
116116
* [`no-jquery/no-event-shorthand`](docs/rules/no-event-shorthand.md) ⚙️ 🔧 `3.5`, `3.3†`, `all`
117-
* [`no-jquery/no-extend`](docs/rules/no-extend.md) ⚙️ `all`
117+
* [`no-jquery/no-extend`](docs/rules/no-extend.md) ⚙️ 🔧 `all`
118118
* [`no-jquery/no-fade`](docs/rules/no-fade.md) `slim`, `all`
119119
* [`no-jquery/no-filter`](docs/rules/no-filter.md) `all`
120120
* [`no-jquery/no-find`](docs/rules/no-find.md)

docs/rules/no-extend.md

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Disallows the [`$.extend`](https://api.jquery.com/jQuery.extend/) utility. Prefe
66

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

9+
🔧 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.
10+
911
## Rule details
1012

1113
❌ Examples of **incorrect** code:
@@ -31,6 +33,16 @@ $.extend( {}, foo );
3133
$.extend( true, {}, foo );
3234
```
3335

36+
🔧 Examples of code **fixed** by this rule:
37+
```js
38+
$.extend( {}, foo ); /**/ Object.assign( {}, foo );
39+
```
40+
41+
🔧 Examples of code **fixed** by this rule with `[{"allowDeep":true}]` options:
42+
```js
43+
$.extend( {}, foo ); /**/ Object.assign( {}, foo );
44+
```
45+
3446
## Resources
3547

3648
* [Rule source](/src/rules/no-extend.js)

src/rules/no-extend.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
docs: {
99
description: 'Disallows the ' + utils.jQueryGlobalLink( 'extend' ) + ' utility. Prefer `Object.assign` or the spread operator.'
1010
},
11+
fixable: 'code',
1112
schema: [
1213
{
1314
type: 'object',
@@ -35,16 +36,19 @@ module.exports = {
3536
return;
3637
}
3738
const allowDeep = context.options[ 0 ] && context.options[ 0 ].allowDeep;
38-
if (
39-
allowDeep &&
40-
node.arguments[ 0 ] && node.arguments[ 0 ].value === true
41-
) {
39+
const isDeep = node.arguments[ 0 ] && node.arguments[ 0 ].value === true;
40+
if ( allowDeep && isDeep ) {
4241
return;
4342
}
4443

4544
context.report( {
4645
node: node,
47-
message: 'Prefer Object.assign or the spread operator to $.extend'
46+
message: 'Prefer Object.assign or the spread operator to $.extend',
47+
fix: function ( fixer ) {
48+
if ( !isDeep ) {
49+
return fixer.replaceText( node.callee, 'Object.assign' );
50+
}
51+
}
4852
} );
4953
}
5054
};

tests/rules/no-extend.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ ruleTester.run( 'no-extend', rule, {
1919
invalid: [
2020
{
2121
code: '$.extend({}, foo)',
22-
errors: [ error ]
22+
errors: [ error ],
23+
output: 'Object.assign({}, foo)'
2324
},
2425
{
2526
code: '$.extend(true, {}, foo)',
@@ -28,7 +29,8 @@ ruleTester.run( 'no-extend', rule, {
2829
{
2930
code: '$.extend({}, foo)',
3031
options: [ { allowDeep: true } ],
31-
errors: [ error ]
32+
errors: [ error ],
33+
output: 'Object.assign({}, foo)'
3234
}
3335
]
3436
} );

0 commit comments

Comments
 (0)