JavaScript stylistic rules for ESLint that enforce code formatting and style consistency without changing code logic.
—
Core spacing rules that control whitespace, indentation, and general code layout for improved readability.
Enforces consistent indentation.
const indent: Rule.RuleModule;
// Rule options
type IndentOptions =
| number
| 'tab'
| [number | 'tab', IndentConfig?];
interface IndentConfig {
SwitchCase?: number;
VariableDeclarator?: number | { var?: number; let?: number; const?: number };
outerIIFEBody?: number;
MemberExpression?: number | 'off';
FunctionDeclaration?: { parameters?: number | 'first' | 'off'; body?: number };
FunctionExpression?: { parameters?: number | 'first' | 'off'; body?: number };
CallExpression?: { arguments?: number | 'first' | 'off' };
ArrayExpression?: number | 'first' | 'off';
ObjectExpression?: number | 'first' | 'off';
ImportDeclaration?: number | 'first' | 'off';
flatTernaryExpressions?: boolean;
offsetTernaryExpressions?: boolean;
ignoredNodes?: string[];
ignoreComments?: boolean;
}Usage Examples:
// ✓ Good with 2 spaces
function example() {
if (condition) {
doSomething();
}
}
// ✓ Good with tab
function example() {
if (condition) {
doSomething();
}
}
// ✓ Good with SwitchCase: 1
switch (value) {
case 'a':
return 1;
case 'b':
return 2;
}Configuration:
rules: {
'@stylistic/js/indent': ['error', 2, { SwitchCase: 1 }]
}Enforces consistent spacing before blocks.
const spaceBeforeBlocks: Rule.RuleModule;
// Rule options
type SpaceBeforeBlocksOptions =
| 'always'
| 'never'
| {
functions?: 'always' | 'never';
keywords?: 'always' | 'never';
classes?: 'always' | 'never';
};Usage Examples:
// ✓ Good with "always" (default)
if (condition) {
doSomething();
}
function example() {
return true;
}
// ✓ Good with "never"
if (condition){
doSomething();
}
function example(){
return true;
}Configuration:
rules: {
'@stylistic/js/space-before-blocks': ['error', 'always']
}Requires spacing around infix operators.
const spaceInfixOps: Rule.RuleModule;
// Rule options
type SpaceInfixOpsOptions = {
int32Hint?: boolean;
};Usage Examples:
// ✓ Good
const sum = a + b;
const result = x * y / z;
const comparison = value === expected;
// ✗ Bad
const sum = a+b;
const result = x*y/z;
const comparison = value===expected;
// ✓ Good with int32Hint: false
const int32 = value|0; // bitwise OR for int32 conversionConfiguration:
rules: {
'@stylistic/js/space-infix-ops': ['error', { int32Hint: false }]
}Enforces consistent spacing before and after keywords.
const keywordSpacing: Rule.RuleModule;
// Rule options
type KeywordSpacingOptions = {
before?: boolean;
after?: boolean;
overrides?: {
[keyword: string]: {
before?: boolean;
after?: boolean;
};
};
};Usage Examples:
// ✓ Good with default settings
if (condition) {
return true;
} else {
return false;
}
try {
doSomething();
} catch (error) {
handleError(error);
}
// ✓ Good with custom overrides
if(condition) { // if: { after: false }
return true;
}else { // else: { before: false }
return false;
}Configuration:
rules: {
'@stylistic/js/keyword-spacing': ['error', {
before: true,
after: true,
overrides: {
if: { after: false },
for: { after: false },
while: { after: false }
}
}]
}Enforces consistent spacing before or after unary operators.
const spaceUnaryOps: Rule.RuleModule;
// Rule options
type SpaceUnaryOpsOptions = {
words?: boolean;
nonwords?: boolean;
overrides?: {
[operator: string]: boolean;
};
};Usage Examples:
// ✓ Good with default settings
const negated = -value;
const incremented = ++counter;
const typeCheck = typeof variable;
const deleted = delete obj.prop;
// Word operators (typeof, void, etc.) get spaces
const result = typeof value === 'string';
const nothing = void 0;
// Symbol operators (!, -, +, etc.) don't get spaces
const opposite = !condition;
const negative = -number;Configuration:
rules: {
'@stylistic/js/space-unary-ops': ['error', {
words: true,
nonwords: false
}]
}Disallows or enforces spaces inside of blocks after opening block and before closing block.
const blockSpacing: Rule.RuleModule;
// Rule options
type BlockSpacingOptions = 'always' | 'never';Usage Examples:
// ✓ Good with "always" (default)
if (condition) { doSomething(); }
function example() { return true; }
// ✓ Good with "never"
if (condition) {doSomething();}
function example() {return true;}
// Multi-line blocks not affected
if (condition) {
doSomething();
doSomethingElse();
}Configuration:
rules: {
'@stylistic/js/block-spacing': ['error', 'always']
}Disallows mixed spaces and tabs for indentation.
const noMixedSpacesAndTabs: Rule.RuleModule;
// Rule options
type NoMixedSpacesAndTabsOptions = 'smart-tabs' | boolean;Usage Examples:
// ✗ Bad - mixing spaces and tabs
function example() {
⇥ return true; // tab after spaces
}
// ✓ Good - consistent spaces
function example() {
return true;
}
// ✓ Good - consistent tabs
function example() {
⇥ return true;
}Configuration:
rules: {
'@stylistic/js/no-mixed-spaces-and-tabs': 'error'
}Disallows all tabs.
const noTabs: Rule.RuleModule;
// Rule options
type NoTabsOptions = {
allowIndentationTabs?: boolean;
};Usage Examples:
// ✗ Bad with default settings
function example() {
⇥ return true; // tab character
}
// ✓ Good
function example() {
return true; // spaces
}
// ✓ Good with allowIndentationTabs: true
function example() {
⇥ return true; // tabs for indentation OK
⇥ const message = "no⇥tabs⇥in⇥strings"; // tabs in strings still bad
}Configuration:
rules: {
'@stylistic/js/no-tabs': ['error', { allowIndentationTabs: false }]
}Requires or disallows newlines around variable declarations.
const oneVarDeclarationPerLine: Rule.RuleModule;
// Rule options
type OneVarDeclarationPerLineOptions = 'always' | 'initializations';Usage Examples:
// ✓ Good with "always"
var a,
b;
let x,
y;
const m = 1,
n = 2;
// ✓ Good with "initializations"
var a, b; // uninitialized OK on same line
var c = 1,
d = 2; // initialized require newlines
let x, y; // uninitialized OK
let z = 1,
w = 2; // initialized require newlinesConfiguration:
rules: {
'@stylistic/js/one-var-declaration-per-line': ['error', 'always']
}Enforces the location of single-line statements.
const nonblockStatementBodyPosition: Rule.RuleModule;
// Rule options
type NonblockStatementBodyPositionOptions =
| 'beside'
| 'below'
| ['beside' | 'below', {
overrides?: {
[statement: string]: 'beside' | 'below';
};
}];Usage Examples:
// ✓ Good with "beside" (default)
if (condition) doSomething();
while (condition) doSomething();
for (let i = 0; i < 10; i++) doSomething();
// ✓ Good with "below"
if (condition)
doSomething();
while (condition)
doSomething();
// ✓ Good with overrides
if (condition) doSomething(); // if: 'beside'
while (condition) // while: 'below'
doSomething();Configuration:
rules: {
'@stylistic/js/nonblock-statement-body-position': ['error', 'beside']
}rules: {
'@stylistic/js/indent': ['error', 2],
'@stylistic/js/space-before-blocks': ['error', 'always'],
'@stylistic/js/space-infix-ops': 'error',
'@stylistic/js/keyword-spacing': ['error', { before: true, after: true }],
'@stylistic/js/block-spacing': ['error', 'always'],
'@stylistic/js/no-mixed-spaces-and-tabs': 'error'
}rules: {
'@stylistic/js/indent': ['error', 2],
'@stylistic/js/space-before-blocks': ['error', 'never'],
'@stylistic/js/block-spacing': ['error', 'never'],
'@stylistic/js/keyword-spacing': ['error', {
before: false,
after: false,
overrides: {
'else': { before: true },
'catch': { before: true }
}
}]
}interface SpacingRules {
'indent': Rule.RuleModule;
'space-before-blocks': Rule.RuleModule;
'space-infix-ops': Rule.RuleModule;
'keyword-spacing': Rule.RuleModule;
'space-unary-ops': Rule.RuleModule;
'block-spacing': Rule.RuleModule;
'no-mixed-spaces-and-tabs': Rule.RuleModule;
'no-tabs': Rule.RuleModule;
}
interface IndentConfig {
SwitchCase?: number;
VariableDeclarator?: number | VariableDeclaratorConfig;
outerIIFEBody?: number;
MemberExpression?: number | 'off';
FunctionDeclaration?: FunctionConfig;
FunctionExpression?: FunctionConfig;
CallExpression?: CallConfig;
ArrayExpression?: number | 'first' | 'off';
ObjectExpression?: number | 'first' | 'off';
ImportDeclaration?: number | 'first' | 'off';
flatTernaryExpressions?: boolean;
offsetTernaryExpressions?: boolean;
ignoredNodes?: string[];
ignoreComments?: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-stylistic--eslint-plugin-js