CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-stylistic--eslint-plugin-js

JavaScript stylistic rules for ESLint that enforce code formatting and style consistency without changing code logic.

Pending
Overview
Eval results
Files

spacing-indentation.mddocs/

Spacing and Indentation

Core spacing rules that control whitespace, indentation, and general code layout for improved readability.

indent

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 }]
}

space-before-blocks

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']
}

space-infix-ops

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 conversion

Configuration:

rules: {
  '@stylistic/js/space-infix-ops': ['error', { int32Hint: false }]
}

keyword-spacing

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 }
    }
  }]
}

space-unary-ops

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
  }]
}

block-spacing

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']
}

no-mixed-spaces-and-tabs

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'
}

no-tabs

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 }]
}

one-var-declaration-per-line

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 newlines

Configuration:

rules: {
  '@stylistic/js/one-var-declaration-per-line': ['error', 'always']
}

nonblock-statement-body-position

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']
}

Common Spacing Combinations

Standard JavaScript Style

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'
}

Compact Style

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 }
    }
  }]
}

Type Definitions

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

docs

array-formatting.md

code-quality-consistency.md

function-formatting.md

index.md

line-breaks-newlines.md

modern-javascript.md

object-formatting.md

plugin-configuration.md

punctuation-operators.md

spacing-indentation.md

tile.json