Skip to content
ESLint rule for suggesting that object spread properties be used instead of Object.assign()
Branch: master
Clone or download
Latest commit 25faebb Apr 18, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Use prettier. Apr 18, 2017
test/rules Use prettier. Apr 18, 2017
.eslintrc Use prettier. Apr 18, 2017
.gitignore Use babel-plugin-istanbul instead of isparta. Mar 1, 2017
.travis.yml Use babel-plugin-istanbul instead of isparta. Mar 1, 2017
README.md Adjust install command Feb 12, 2016
package.json Release v1.2.1 Apr 18, 2017
yarn.lock Use prettier. Apr 18, 2017

README.md

build status test coverage npm

eslint-plugin-prefer-object-spread

ESLint rule for suggesting that object spread properties be used instead of Object.assign().

Installation

Install ESLint and eslint-plugin-prefer-object-spread:

$ npm install --save-dev eslint eslint-plugin-prefer-object-spread

Usage

Add prefer-object-spread to the plugins section of your .eslintrc configuration file, and configure the rule under the rules section.

{
  "plugins": [
    "prefer-object-spread"
  ],
  "rules": {
    "prefer-object-spread/prefer-object-spread": 2
  }
}

This rule suggests that object spread properties be used instead of Object.assign(). The rule is only applied when Object.assign() is used for cloning; not when it is used to extend an existing object. i.e., it applies when the first argument to Object.assign() is an object literal. This is because spread properties only iterate over own properties.

When using this rule the following patterns are considered problems:

var a = Object.assign({}, foo); // error Use a spread property instead of Object.assign().

var b = Object.assign({ c: 1 }, bar); // error Use a spread property instead of Object.assign().

The following patterns are considered okay:

var a = { ...foo };

var b = { c: 1, ...bar };

Object.assign(b, { d: 2 });
You can’t perform that action at this time.