JavaScript stylistic rules for ESLint that enforce code formatting and style consistency without changing code logic.
—
Rules that control the formatting and styling of JavaScript arrays, including bracket spacing, newlines, and element positioning.
Enforces linebreaks after opening and before closing array brackets.
const arrayBracketNewline: Rule.RuleModule;
// Rule options
type ArrayBracketNewlineOptions =
| 'always'
| 'never'
| 'consistent'
| {
multiline?: boolean;
minItems?: number | null;
};Usage Examples:
// ✓ Good with "always"
const items = [
'apple',
'banana'
];
// ✓ Good with "never"
const items = ['apple', 'banana'];
// ✓ Good with { multiline: true }
const items = ['apple', 'banana']; // single line ok
const items = [
'apple',
'banana'
]; // multiline requires brackets on new linesConfiguration:
rules: {
'@stylistic/js/array-bracket-newline': ['error', 'consistent']
}Enforces consistent spacing inside array brackets.
const arrayBracketSpacing: Rule.RuleModule;
// Rule options
type ArrayBracketSpacingOptions =
| 'always'
| 'never'
| ['always' | 'never', {
singleValue?: boolean;
objectsInArrays?: boolean;
arraysInArrays?: boolean;
}];Usage Examples:
// ✓ Good with "never" (default)
const items = ['apple', 'banana'];
// ✓ Good with "always"
const items = [ 'apple', 'banana' ];
// ✓ Good with custom options
const items = ['apple', 'banana']; // singleValue: false
const nested = [[ 'nested' ]]; // arraysInArrays: falseConfiguration:
rules: {
'@stylistic/js/array-bracket-spacing': ['error', 'never']
}Enforces line breaks after each array element.
const arrayElementNewline: Rule.RuleModule;
// Rule options
type ArrayElementNewlineOptions =
| 'always'
| 'never'
| 'consistent'
| {
multiline?: boolean;
minItems?: number | null;
};Usage Examples:
// ✓ Good with "always"
const items = [
'apple',
'banana',
'cherry'
];
// ✓ Good with "never"
const items = ['apple', 'banana', 'cherry'];
// ✓ Good with { minItems: 3 }
const small = ['a', 'b']; // under threshold, no newlines required
const large = [
'apple',
'banana',
'cherry'
]; // meets threshold, newlines requiredConfiguration:
rules: {
'@stylistic/js/array-element-newline': ['error', { minItems: 3 }]
}Here are some popular combinations for array formatting:
rules: {
'@stylistic/js/array-bracket-spacing': ['error', 'never'],
'@stylistic/js/array-bracket-newline': ['error', 'never'],
'@stylistic/js/array-element-newline': ['error', 'never']
}
// Result:
const items = ['apple', 'banana', 'cherry'];rules: {
'@stylistic/js/array-bracket-spacing': ['error', 'never'],
'@stylistic/js/array-bracket-newline': ['error', { multiline: true }],
'@stylistic/js/array-element-newline': ['error', { minItems: 3 }]
}
// Result:
const short = ['a', 'b'];
const long = [
'apple',
'banana',
'cherry'
];// Complete type definitions for array rules
interface ArrayBracketNewlineRule {
meta: {
type: 'layout';
docs: { description: string };
fixable: 'whitespace';
schema: JSONSchema4[];
};
create(context: Rule.RuleContext): Rule.RuleListener;
}
interface ArrayFormattingRules {
'array-bracket-newline': ArrayBracketNewlineRule;
'array-bracket-spacing': Rule.RuleModule;
'array-element-newline': Rule.RuleModule;
}Install with Tessl CLI
npx tessl i tessl/npm-stylistic--eslint-plugin-js