list-style experimental โ
IMPORTANT
๐งช This rule is an experimental rule, changes may not follow semver.
Should prefix exp-
when using. For example: @stylistic/exp-list-style
Enforce consistent spacing and line break styles inside brackets.
Rule Details โ
This rule requires or disallows a line break between object/array/named imports/exports and function parameters and other similar structures.
It check the newline style of the first property or item and apply the same style to the rest of the properties or items. This allows you to easily wrap or unwrap your code consistently.
Options โ
This rule accepts an object option:
"singleLine"
: Options for when the node is single-line"spacing"
: Whether spaces are required inside the enclosing brackets"maxItems"
: Maximum number of elements allowed before auto-fixing to multi-line
"multiLine"
: Options for when the node is multi-line"minItems"
: Minimum number of elements allowed before auto-fixing to single-line
"overrides"
: Override options based on bracket type or node type
The default configuration of this rule is:
defaultOptions: [{
singleLine: {
spacing: 'never',
maxItems: Number.POSITIVE_INFINITY,
},
multiLine: {
minItems: 0,
},
overrides: {
'{}': { singleLine: { spacing: 'always' } },
},
}],
singleLine โ
spacing โ
"always" requires spaces, "never" disallows spaces.
Examples of incorrect code for this rule with the "always"
option:
/* eslint @stylistic/exp-list-style: ["error", { "singleLine": { "spacing": "always" } }] */
let foo = {a: 1, b: 2};
let bar = [1, 2];
let {a, b} = foo;
let [c, d] = bar;
function foo(a) {}
const foo = function (a) {}
foo(a, b);
new Foo<Bar>(a, b);
import {name} from 'package.json' with {type: 'json'}
export {name} from 'package.json' with {type: 'json'}
export * from 'package.json' with {type: 'json'}
type Foo<T> = {a: number; b: T};
type Bar = [1, 2];
type Baz<T> = (a: number, b: T) => void
function foo<T>(a: number, b: T): void;
Examples of correct code for this rule with the "always"
option:
/* eslint @stylistic/exp-list-style: ["error", { "singleLine": { "spacing": "always" } }] */
let foo = { a: 1, b: 2 };
let bar = [ 1, 2 ];
let { a, b } = foo;
let [ c, d ] = bar;
function foo( a ) {}
const foo = function ( a ) {}
foo( a, b );
new Foo< Bar >( a, b );
import { name } from 'package.json' with { type: 'json' }
export { name } from 'package.json' with { type: 'json' }
export * from 'package.json' with { type: 'json' }
type Foo< T > = { a: number; b: T };
type Bar = [ 1, 2 ];
type Baz< T > = ( a: number, b: T ) => void
function foo< T >( a: number, b: T ): void;
maxItems โ
Examples of incorrect code for this rule with the "maxItems"
option:
/* eslint @stylistic/exp-list-style: ["error", { "singleLine": { "maxItems": 1 } }] */
let foo = {a: 1, b: 2};
let bar = [1, 2];
let {a, b} = foo;
let [a, b] = bar;
Examples of correct code for this rule with the "maxItems"
option:
/* eslint @stylistic/exp-list-style: ["error", { "singleLine": { "maxItems": 1 } }] */
let foo = {
a: 1,
b: 2
};
let bar = [
1,
2
];
let {
a,
b
} = foo;
let [
a,
b
] = bar;
multiLine โ
minItems โ
Examples of incorrect code for this rule with the "minItems"
option:
/* eslint @stylistic/exp-list-style: ["error", { "multiLine": { "minItems": 3 } }] */
let foo = {
a: 1,
b: 2,
};
let bar = [
1,
2,
];
let {
a,
b,
} = foo;
let [
a,
b,
] = bar;
Examples of correct code for this rule with the "minItems"
option:
/* eslint @stylistic/exp-list-style: ["error", { "multiLine": { "minItems": 1 } }] */
let foo = {
a: 1,
b: 2,
};
let bar = [
1,
2,
];
let {
a,
b
} = foo;
let [
a,
b
] = bar;
overrides โ
You can specify different options for specific bracket types:
{}
- curly braces (objects)[]
- square brackets (arrays)()
- parentheses (function calls, parameters)<>
- angle brackets (TypeScript generics)
Examples of correct code for this rule with the "overrides" option specified for brackets:
/* eslint @stylistic/exp-list-style: ["error", { "overrides": { "{}": { "singleLine": { "spacing": "always" } } } }] */
let foo = { a: 1 };
let bar = [1];
let { a } = foo;
let [b] = bar;
You can also specify different options for various node types:
ArrayExpression
: arrays expressionsArrayPattern
: array patterns of destructuring assignmentsArrowFunctionExpression
: parameters of arrow function declarationsCallExpression
: parameters of call expressionsExportNamedDeclaration
: named exportsFunctionDeclaration
: parameters of function declarationsFunctionExpression
: parameters of function expressionsImportDeclaration
: named importsImportAttributes
: import attributesNewExpression
: parameters of new expressionsObjectExpression
: object literalsObjectPattern
: object patterns of destructuring assignmentsTSDeclareFunction
: parameters of function type declarationsTSFunctionType
: parameters of arrow function type declarationsTSInterfaceBody
: interfaces declarationsTSEnumBody
: enum declarationsTSTupleType
: tuple typesTSTypeLiteral
: type literalsTSTypeParameterDeclaration
: type parameter declarationsTSTypeParameterInstantiation
: type parameter instantiationsJSONArrayExpression
: arrays expressions in JSON filesJSONObjectExpression
: object literals in JSON files
Example of node-specific override:
/* eslint @stylistic/exp-list-style: ["error", {
"overrides": {
"ImportAttributes": { "singleLine": { "spacing": "never" } },
}
}] */
import def, { a, b } from 'foo' with {type: 'raw'};
let foo = { a: 1 };
let bar = [1];
let { a } = foo;
let [b] = bar;
When Not To Use It โ
If you do not want to enforce consistent line breaks after opening and before closing brackets, or if your project has existing inconsistent formatting that you don't wish to change, you can safely disable this rule.