eslint-plugin-newline-destructuring
raw JSON → 1.2.2 verified Sat Apr 25 auth: no javascript
ESLint plugin enforcing newlines in object destructuring assignments when the number of properties exceeds a configurable threshold. Current version 1.2.2 works with ESLint >=7.2.0, released in a stable series that added a `consistent` option in v1.1.0. Key differentiator: specifically targets destructuring patterns (not general object expressions), offering granular control via `items`, `itemsWithRest`, `maxLength`, `consistent`, and `allowAllPropertiesOnSameLine` options. Lightweight, zero runtime dependencies beyond ESLint peer dependency. Useful for teams wanting consistent formatting of destructuring statements without adopting a full formatter like Prettier.
Common errors
error Definition for rule 'newline-destructuring/newline' was not found ↓
cause The plugin is not installed or not loaded in the ESLint config.
fix
Install the plugin: npm install eslint-plugin-newline-destructuring --save-dev and add 'newline-destructuring' to the plugins array.
error ESLint couldn't find the plugin "eslint-plugin-newline-destructuring". ↓
cause The plugin is not installed in the project.
fix
Run: npm install eslint-plugin-newline-destructuring --save-dev
error TypeError: Cannot read properties of undefined (reading 'getStaticValue') ↓
cause Using incorrect rule options (e.g., passing an array where an object is expected).
fix
Ensure the rule config is an object, e.g., ["error", { "items": 2 }]
error Configuration for rule "newline-destructuring/newline" is invalid: Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '"error"') ↓
cause Using string severity in older ESLint versions where numeric severity is expected.
fix
Use numeric severity: 2 instead of "error" if using ESLint < 7. Or upgrade ESLint.
Warnings
deprecated The 'consistent' option was added in v1.1.0 and may change behavior for existing configurations that do not set it. ↓
fix If upgrading from <1.1.0, set 'consistent' explicitly to false to retain original behavior.
gotcha The rule only applies to object destructuring patterns, not array destructuring or object expressions. ↓
fix Ensure the rule is used for object destructuring; for other patterns, use other ESLint rules like 'object-curly-newline'.
breaking ESLint peer dependency requirement jumped to >=7.2.0 starting from v1.0.0; earlier versions might not be compatible. ↓
fix Upgrade ESLint to version 7.2.0 or higher, or use an older version of the plugin (e.g., 0.0.4).
gotcha The 'items' option sets the threshold for non-rest properties; if the count is exactly equal to 'items', the statement may still be kept on one line unless 'maxLength' is violated. ↓
fix To always break when exceeding 'items', set 'allowAllPropertiesOnSameLine' to false and ensure 'consistent' is false.
Install
npm install eslint-plugin-newline-destructuring yarn add eslint-plugin-newline-destructuring pnpm add eslint-plugin-newline-destructuring Imports
- plugin wrong
const newlineDestructuring = require('eslint-plugin-newline-destructuring')correctimport newlineDestructuring from 'eslint-plugin-newline-destructuring' - rule 'newline' wrong
rules: { 'newline-destructuring': 'error' }correctrules: { 'newline-destructuring/newline': 'error' } - config type wrong
plugins: ['eslint-plugin-newline-destructuring']correctplugins: ['newline-destructuring']
Quickstart
// .eslintrc.json
{
"plugins": ["newline-destructuring"],
"rules": {
"newline-destructuring/newline": ["error", {
"items": 2,
"itemsWithRest": 1,
"maxLength": 80,
"consistent": true,
"allowAllPropertiesOnSameLine": false
}]
}
}
// Example destructuring that passes
const { a, b, c } = obj;
// Example that triggers error (more than 2 non-rest items)
const { a, b, c, d } = obj;
// Should be:
const {
a,
b,
c,
d
} = obj;