eslint-plugin-prefer-object-spread
raw JSON → 1.2.1 verified Sat Apr 25 auth: no javascript maintenance
ESLint rule that flags Object.assign() calls when the target is an object literal, recommending object spread properties instead. Version 1.2.1 is the latest stable release; the package has not been updated since 2016. It applies only when Object.assign() is used for cloning (first argument is an object literal) to avoid false positives when mutating an existing object. Requires ESLint >=0.8.0 and Node >=4.0.0. This plugin is lightweight and has a single rule. It is not actively maintained and may be superseded by built-in ESLint rules or new JavaScript features.
Common errors
error Error: Failed to load plugin 'prefer-object-spread' declared in '.eslintrc': Cannot find module 'eslint-plugin-prefer-object-spread' ↓
cause ESLint plugin not installed
fix
Run
npm install --save-dev eslint eslint-plugin-prefer-object-spread error Parsing error: The keyword 'prefer-object-spread' is not defined ↓
cause Rule name missing plugin prefix in config
fix
Use 'prefer-object-spread/prefer-object-spread' instead of just 'prefer-object-spread'
error Configuration for rule 'prefer-object-spread/prefer-object-spread' is invalid: Severity should be one of the following: 0 = off, 1 = warn, 2 = error ↓
cause Invalid severity value in ESLint config
fix
Use numbers (0,1,2) or strings ('off','warn','error')
Warnings
gotcha The rule only flags Object.assign() when the target is an object literal. If you mutate an existing object, the rule will not flag it. ↓
fix No fix needed; this is by design. Use the rule only for cloning patterns.
gotcha Object spread only copies own enumerable properties. Object.assign() also copies own enumerable properties, but the rule assumes both are equivalent. ↓
fix Consider if you have non-enumerable properties or accessor properties. Otherwise, spread is safe.
deprecated This plugin is no longer actively maintained. ESLint's built-in 'prefer-object-spread' rule may be preferred if available. ↓
fix Migrate to ESLint's built-in 'prefer-object-spread' rule (since ESLint v5.8.0) by removing the plugin and using `"prefer-object-spread/prefer-object-spread": "error" -> "prefer-object-spread": "error"`.
Install
npm install eslint-plugin-prefer-object-spread yarn add eslint-plugin-prefer-object-spread pnpm add eslint-plugin-prefer-object-spread Imports
- prefer-object-spread (plugin) wrong
const plugin = require('eslint-plugin-prefer-object-spread');correctimport { rules } from 'eslint-plugin-prefer-object-spread'; - prefer-object-spread rule wrong
"prefer-object-spread": "error"correct"prefer-object-spread/prefer-object-spread": "error" - Plugin registration in ESLint config wrong
{ "plugins": ["prefer-object-spread"], "rules": { "prefer-object-spread": "error" } }correct{ "plugins": ["prefer-object-spread"], "rules": { "prefer-object-spread/prefer-object-spread": "error" } }
Quickstart
// First install ESLint and the plugin:
// npm install --save-dev eslint eslint-plugin-prefer-object-spread
// In your .eslintrc.js:
module.exports = {
plugins: ['prefer-object-spread'],
rules: {
'prefer-object-spread/prefer-object-spread': 'error'
}
};
// Example code that would trigger the rule:
var a = Object.assign({}, foo); // Error: Use a spread property instead of Object.assign().
// Corrected version:
var a = { ...foo };