├── .npmignore ├── index.js ├── .github └── workflows │ └── ci.yml ├── package.json ├── .gitignore ├── README.md ├── LICENSE ├── rules └── prefer-object-spread.js └── tests └── prefer-object-spread.js /.npmignore: -------------------------------------------------------------------------------- 1 | # Everything 2 | * 3 | 4 | !index.js 5 | !rules/* 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Suggest using the spread operator over Object.assign. 3 | * @author Erik Desjardins 4 | * @copyright 2016 Erik Desjardins. All rights reserved. 5 | * See LICENSE file in root directory for full license. 6 | */ 7 | 'use strict'; 8 | 9 | module.exports = { 10 | rules: { 11 | 'prefer-object-spread': require('./rules/prefer-object-spread') 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | tags: 8 | - v*.*.* 9 | pull_request: 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v1 16 | - uses: actions/setup-node@v1 17 | with: 18 | node-version: '12.x' 19 | registry-url: 'https://registry.npmjs.org' 20 | - run: npm install 21 | - run: npm test 22 | - run: npm publish 23 | if: startsWith(github.ref, 'refs/tags/') 24 | env: 25 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-prefer-spread", 3 | "version": "1.0.3", 4 | "description": "Suggest using the spread operator over Object.assign.", 5 | "keywords": [ 6 | "eslint", 7 | "eslintplugin", 8 | "eslint-plugin" 9 | ], 10 | "author": "Erik Desjardins", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/erikdesjardins/eslint-plugin-prefer-spread.git" 14 | }, 15 | "main": "index.js", 16 | "scripts": { 17 | "test": "mocha ./tests/*.js" 18 | }, 19 | "peerDependencies": { 20 | "eslint": ">=1.0.0" 21 | }, 22 | "devDependencies": { 23 | "eslint": "^6.6.0", 24 | "mocha": "^6.2.2" 25 | }, 26 | "license": "MIT" 27 | } 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-plugin-prefer-spread 2 | 3 | Suggest using the spread operator over Object.assign. 4 | 5 | Warns for the following (when the first argument is any object literal): 6 | 7 | ```js 8 | // copies own enumerable properties 9 | Object.assign({}, foo /* ... */); 10 | _.assign({}, foo /* ... */); 11 | ``` 12 | 13 | Also warns for the following (with the `"includeNearEquivalents"` option): 14 | 15 | ```js 16 | // copies own and inherited enumerable properties 17 | $.extend({}, foo /* ... */); 18 | _.assignIn({}, foo /* ... */); 19 | _.extend({}, foo /* ... */); 20 | ``` 21 | 22 | Does not warn: 23 | 24 | ```js 25 | // deep copy 26 | $.extend(true, {}, foo); 27 | ``` 28 | 29 | ## Usage 30 | 31 | `npm i --save-dev eslint-plugin-prefer-spread` 32 | 33 | ```json 34 | { 35 | "plugins": [ 36 | "prefer-spread" 37 | ], 38 | "rules": { 39 | "prefer-spread/prefer-object-spread": [2, "includeNearEquivalents"] 40 | } 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Erik Desjardins 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /rules/prefer-object-spread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Suggest using the spread operator over Object.assign. 3 | * @author Erik Desjardins 4 | * @copyright 2016 Erik Desjardins. All rights reserved. 5 | * See LICENSE file in root directory for full license. 6 | */ 7 | 'use strict'; 8 | 9 | module.exports = function(context) { 10 | var includeNearEquivalents = context.options[0] === 'includeNearEquivalents'; 11 | 12 | return { 13 | CallExpression: function(node) { 14 | if (!node.arguments.length) { 15 | return; 16 | } 17 | 18 | if (node.callee.type !== 'MemberExpression') { 19 | return; 20 | } 21 | 22 | var object = node.callee.object; 23 | 24 | if (object.type !== 'Identifier') { 25 | return; 26 | } 27 | 28 | var property = node.callee.property; 29 | 30 | var obj = object.name; 31 | var method = property.name; 32 | 33 | var isEquivalentMethod = ( 34 | obj === 'Object' && method === 'assign' || 35 | obj === '_' && method === 'assign' 36 | ); 37 | var isNearEquivalentMethod = includeNearEquivalents && ( 38 | obj === '$' && method === 'extend' || 39 | obj === '_' && (method === 'assignIn' || method === 'extend') 40 | ); 41 | 42 | if (!isEquivalentMethod && !isNearEquivalentMethod) { 43 | return; 44 | } 45 | 46 | if (node.arguments[0].type !== 'ObjectExpression') { 47 | return; 48 | } 49 | 50 | context.report({ 51 | node: property, 52 | message: 'Expected spread operator.' 53 | }); 54 | } 55 | }; 56 | }; 57 | 58 | module.exports.schema = [ 59 | { 60 | enum: ['includeNearEquivalents'] 61 | } 62 | ]; 63 | -------------------------------------------------------------------------------- /tests/prefer-object-spread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Suggest using the spread operator over Object.assign. 3 | * @author Erik Desjardins 4 | * @copyright 2016 Erik Desjardins. All rights reserved. 5 | * See LICENSE file in root directory for full license. 6 | */ 7 | 'use strict'; 8 | 9 | var rule = require('../rules/prefer-object-spread'); 10 | var RuleTester = require('eslint').RuleTester; 11 | 12 | var errorMessage = 'Expected spread operator.'; 13 | 14 | var ruleTester = new RuleTester(); 15 | ruleTester.run('prefer-object-spread', rule, { 16 | valid: [ 17 | //// without "includeNearEquivalents" 18 | 19 | // no args 20 | 'Object.assign(); _.assign();', 21 | // non-object-literal first arg 22 | 'Object.assign(foo, bar); _.assign(foo, bar);', 23 | // invalid object identifier 24 | 'object.assign({}, foo); __.assign({}, foo);', 25 | // deep property 26 | 'a.Object.assign({}, foo); a._.assign({}, foo);', 27 | // bare function 28 | 'assign({}, foo);', 29 | // near equivalents 30 | '$.extend({}, foo); _.assignIn({}, foo); _.extend({}, foo);', 31 | // near equivalents, one arg 32 | '$.extend({}); _.assignIn({}); _.extend({});', 33 | // near equivalents, non-empty object 34 | '$.extend({ foo: 5 }, bar); _.assignIn({ foo: 5 }, bar); _.extend({ foo: 5 }, bar);', 35 | 36 | //// with "includeNearEquivalents" 37 | 38 | // no args 39 | { code: '$.extend(); _.assignIn(); _.extend();', options: ['includeNearEquivalents'] }, 40 | // non-object-literal first arg 41 | { code: '$.extend(foo, bar); _.assignIn(foo, bar); _.extend(foo, bar);', options: ['includeNearEquivalents'] }, 42 | // invalid object identifier 43 | { code: '$$.extend({}, foo); __.assignIn({}, foo); __.extend(foo, bar);', options: ['includeNearEquivalents'] }, 44 | // deep property 45 | { code: 'a.$.extend({}, foo); a._.assignIn({}, foo); a._.extend(foo, bar);', options: ['includeNearEquivalents'] }, 46 | // bare function 47 | { code: 'extend({}, foo); assignIn({}, foo);', options: ['includeNearEquivalents'] }, 48 | // jQuery deep extend 49 | { code: '$.extend(true, {}, foo);', options: ['includeNearEquivalents'] }, 50 | ], 51 | invalid: [ 52 | //// without "includeNearEquivalents" 53 | 54 | // empty object literal 55 | { 56 | code: 'Object.assign({}, foo); _.assign({}, foo); $.extend({}, foo); _.assignIn({}, foo); _.extend({}, foo);', 57 | errors: [{ 58 | message: errorMessage, 59 | type: 'Identifier', 60 | line: 1, 61 | column: 8 62 | }, { 63 | message: errorMessage, 64 | type: 'Identifier', 65 | line: 1, 66 | column: 27 67 | }] 68 | }, 69 | // non-empty object literal 70 | { 71 | code: 'Object.assign({ foo: 5 }, foo); _.assign({ foo: 5 }, foo); $.extend({ foo: 5 }, foo); _.assignIn({ foo: 5 }, foo); _.extend({ foo: 5 }, foo);', 72 | errors: [{ 73 | message: errorMessage, 74 | type: 'Identifier', 75 | line: 1, 76 | column: 8 77 | }, { 78 | message: errorMessage, 79 | type: 'Identifier', 80 | line: 1, 81 | column: 35 82 | }] 83 | }, 84 | // just an object literal 85 | // you couldn't use the spread operator here, but it doesn't make sense, so might as well report it 86 | { 87 | code: 'Object.assign({}); _.assign({}); $.extend({}); $.assignIn({}); _.extend({});', 88 | errors: [{ 89 | message: errorMessage, 90 | type: 'Identifier', 91 | line: 1, 92 | column: 8 93 | }, { 94 | message: errorMessage, 95 | type: 'Identifier', 96 | line: 1, 97 | column: 22 98 | }] 99 | }, 100 | 101 | //// with "includeNearEquivalents" 102 | 103 | // empty object literal 104 | { 105 | code: 'Object.assign({}, foo); _.assign({}, foo); $.extend({}, foo); _.assignIn({}, foo); _.extend({}, foo);', 106 | options: ['includeNearEquivalents'], 107 | errors: [{ 108 | message: errorMessage, 109 | type: 'Identifier', 110 | line: 1, 111 | column: 8 112 | }, { 113 | message: errorMessage, 114 | type: 'Identifier', 115 | line: 1, 116 | column: 27 117 | }, { 118 | message: errorMessage, 119 | type: 'Identifier', 120 | line: 1, 121 | column: 46 122 | }, { 123 | message: errorMessage, 124 | type: 'Identifier', 125 | line: 1, 126 | column: 65 127 | }, { 128 | message: errorMessage, 129 | type: 'Identifier', 130 | line: 1, 131 | column: 86 132 | }] 133 | }, 134 | // non-empty object literal 135 | { 136 | code: 'Object.assign({ foo: 5 }, foo); _.assign({ foo: 5 }, foo); $.extend({ foo: 5 }, foo); _.assignIn({ foo: 5 }, foo); _.extend({ foo: 5 }, foo);', 137 | options: ['includeNearEquivalents'], 138 | errors: [{ 139 | message: errorMessage, 140 | type: 'Identifier', 141 | line: 1, 142 | column: 8 143 | }, { 144 | message: errorMessage, 145 | type: 'Identifier', 146 | line: 1, 147 | column: 35 148 | }, { 149 | message: errorMessage, 150 | type: 'Identifier', 151 | line: 1, 152 | column: 62 153 | }, { 154 | message: errorMessage, 155 | type: 'Identifier', 156 | line: 1, 157 | column: 89 158 | }, { 159 | message: errorMessage, 160 | type: 'Identifier', 161 | line: 1, 162 | column: 118 163 | }] 164 | }, 165 | // just an object literal 166 | { 167 | code: 'Object.assign({}); _.assign({}); $.extend({}); _.assignIn({}); _.extend({});', 168 | options: ['includeNearEquivalents'], 169 | errors: [{ 170 | message: errorMessage, 171 | type: 'Identifier', 172 | line: 1, 173 | column: 8 174 | }, { 175 | message: errorMessage, 176 | type: 'Identifier', 177 | line: 1, 178 | column: 22 179 | }, { 180 | message: errorMessage, 181 | type: 'Identifier', 182 | line: 1, 183 | column: 36 184 | }, { 185 | message: errorMessage, 186 | type: 'Identifier', 187 | line: 1, 188 | column: 50 189 | }, { 190 | message: errorMessage, 191 | type: 'Identifier', 192 | line: 1, 193 | column: 66 194 | }] 195 | }, 196 | ] 197 | }); 198 | --------------------------------------------------------------------------------