or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

@babel/eslint-plugin

@babel/eslint-plugin provides companion ESLint rules for @babel/eslint-parser. It re-implements core ESLint rules to eliminate false positives and negatives when working with experimental Babel features like decorators, class fields, private methods, accessor properties, do expressions, and modern export syntax.

Package Information

  • Package Name: @babel/eslint-plugin
  • Package Type: npm (ESLint plugin)
  • Language: JavaScript
  • Installation: npm install @babel/eslint-plugin --save-dev

Core Imports

CommonJS (main entry point):

const babelEslintPlugin = require("@babel/eslint-plugin");
const { meta, rules, rulesConfig } = babelEslintPlugin;

ESM (when default export is available):

import babelEslintPlugin from "@babel/eslint-plugin";
const { meta, rules, rulesConfig } = babelEslintPlugin;

Destructured imports (with TypeScript types):

const { meta, rules, rulesConfig } = require("@babel/eslint-plugin");

Basic Usage

Configuration in .eslintrc.json:

{
  "plugins": ["@babel"],
  "rules": {
    "@babel/new-cap": "error",
    "@babel/no-invalid-this": "error",
    "@babel/no-undef": "error",
    "@babel/no-unused-expressions": "error",
    "@babel/object-curly-spacing": "error",
    "@babel/semi": "error"
  }
}

Each rule corresponds to a core ESLint rule and has the same options. Remember to disable the original ESLint rules when enabling the @babel equivalents.

Capabilities

Plugin Metadata

Access to plugin name and version information.

const meta: {
  name: string;
  version: string;
};

Rule Collection

Collection of all ESLint rules provided by the plugin.

const rules: {
  "new-cap": ESLintRule;
  "no-invalid-this": ESLintRule;
  "no-undef": ESLintRule;
  "no-unused-expressions": ESLintRule;
  "object-curly-spacing": ESLintRule;
  "semi": ESLintRule;
};

Default Rule Configuration

Default configuration object with all rules set to "off".

const rulesConfig: {
  "new-cap": "off";
  "no-invalid-this": "off";
  "no-undef": "off";
  "no-unused-expressions": "off";
  "object-curly-spacing": "off";
  "semi": "off";
};

Rules

new-cap Rule

Extends ESLint's new-cap rule to handle decorators properly. Prevents false positives when using decorator syntax (@Decorator).

// Rule: "@babel/new-cap"
// Handles: @MyDecorator class MyClass{}
// Options: Same as ESLint new-cap rule

Usage Example:

// ✅ Valid - decorator syntax
@MyDecorator(options)
class MyComponent {
  // class implementation
}

// Configuration
{
  "rules": {
    "new-cap": "off",           // Disable original
    "@babel/new-cap": "error"   // Enable Babel version
  }
}

no-invalid-this Rule

Extends ESLint's no-invalid-this rule to handle class fields and private class methods. Prevents false positives in modern class syntax.

// Rule: "@babel/no-invalid-this"
// Handles: class A { field = this.value; #private() { return this.field; } }
// Options: Same as ESLint no-invalid-this rule

Usage Example:

// ✅ Valid - class field with this reference
class MyClass {
  value = 42;
  field = this.value;  // Valid this usage
  
  #privateMethod() {
    return this.field;  // Valid this usage in private method
  }
}

no-undef Rule

Extends ESLint's no-undef rule to handle class accessor properties. Prevents false positives when using accessor syntax in classes.

// Rule: "@babel/no-undef"
// Handles: class A { accessor x = 2; }
// Options: Same as ESLint no-undef rule

Usage Example:

// ✅ Valid - class accessor property
class MyClass {
  accessor x = 2;  // Accessor property syntax
  
  getValue() {
    return this.x;
  }
}

no-unused-expressions Rule

Extends ESLint's no-unused-expressions rule to handle do expressions and optional call expressions. Prevents false positives when using experimental syntax.

// Rule: "@babel/no-unused-expressions"
// Handles: do { /* expression block */ } and optional call expressions
// Options: Same as ESLint no-unused-expressions rule

Usage Example:

// ✅ Valid - do expression
const result = do {
  if (condition) {
    "success";
  } else {
    "failure";
  }
};

// ✅ Valid - optional call expression
obj?.method?.();

object-curly-spacing Rule

Extends ESLint's object-curly-spacing rule to handle export default from syntax. Prevents false positives when using export x from 'module' syntax with autofixing.

// Rule: "@babel/object-curly-spacing"
// Handles: export x from './module';
// Options: Same as ESLint object-curly-spacing rule
// Features: Autofixable (🛠)

Usage Example:

// ✅ Valid - export default from syntax
export x from './utils';
export y from './helpers';

// Configuration with options
{
  "rules": {
    "object-curly-spacing": "off",
    "@babel/object-curly-spacing": ["error", "always"]
  }
}

semi Rule

Extends ESLint's semi rule to handle class properties. Manages semicolon usage in class property definitions with autofixing support.

// Rule: "@babel/semi"
// Handles: class A { prop = value; }
// Options: Same as ESLint semi rule
// Features: Autofixable (🛠)

Usage Example:

// ✅ Valid - class properties with semicolons
class MyClass {
  property = "value";     // Semicolon handled properly
  #private = 42;          // Private property
  
  method() {
    return this.property;
  }
}

// Configuration options
{
  "rules": {
    "semi": "off",
    "@babel/semi": ["error", "always", { 
      "omitLastInOneLineBlock": true 
    }]
  }
}

Types

ESLint Rule Module

All rules conform to the ESLint Rule Module interface.

interface ESLintRule {
  meta?: {
    type?: "problem" | "suggestion" | "layout";
    docs?: {
      description?: string;
      category?: string;
      recommended?: boolean;
    };
    fixable?: "code" | "whitespace";
    schema?: any;
  };
  create(context: RuleContext): RuleListener;
}

interface RuleContext {
  id: string;
  options: any[];
  settings: { [name: string]: any };
  parserPath: string;
  parserOptions: ParserOptions;
  parserServices: ParserServices;
  getAncestors(): ESNode[];
  getDeclaredVariables(node: ESNode): Variable[];
  getFilename(): string;
  getScope(): Scope;
  getSourceCode(): SourceCode;
  markVariableAsUsed(name: string): boolean;
  report(descriptor: ReportDescriptor): void;
}

Compatibility

  • Node.js: ^10.13.0 || ^12.13.0 || >=14.0.0
  • ESLint: ^7.5.0 || ^8.0.0 || ^9.0.0 (peer dependency)
  • @babel/eslint-parser: ^7.11.0 (peer dependency)

The plugin automatically adapts its behavior based on the ESLint version:

  • ESLint 8+: Uses native support for class properties and private methods where available
  • ESLint 7: Uses custom filtering and composition logic for experimental features

Error Handling

Rules follow standard ESLint error reporting patterns. Each rule may report violations with:

  • Message: Descriptive error message
  • Location: Line and column information
  • Fix: Automatic fix suggestion (for autofixable rules)
  • Severity: Based on rule configuration ("error", "warn", "off")

Common configuration patterns:

// Disable original rule, enable Babel version
{
  "rules": {
    "semi": "off",
    "@babel/semi": "error"
  }
}

// Use with options (same as original ESLint rule)
{
  "rules": {
    "@babel/object-curly-spacing": ["error", "always", {
      "arraysInObjects": false,
      "objectsInObjects": false
    }]
  }
}