Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Macil/eslint-plugin-no-dupe-class-fields

Repository files navigation

eslint-plugin-no-dupe-class-fields

GitHub license npm version CircleCI Status Greenkeeper badge

This project is an ESLint plugin which detects when classes have duplicate fields, including methods and properties.

Update: This plugin is unnecessary now as class properties have become a standardized part of Javascript and ESLint, and ESLint's own no-dupe-class-members rule has solved the issue that this project was made to work around.

class Foo {
  bar = 5;
  bar() {
    console.log('hello');
  }
}

let foo = new Foo();
foo.bar(); // Error because bar is 5. Whoops

This plugin will detect if there are multiple methods or properties with the same name on a single class. This plugin is an upgraded version of the built-in no-dupe-class-members rule, which does not detect class properties. When you use this plugin's recommended config, the built-in no-dupe-class-members rule will be disabled.

Class properties are currently an ECMAScript proposal, and are only supported with a babel plugin. ESLint does not support class properties by default, so you must currently use the babel-eslint parser with ESLint for this plugin to detect class properties.

This plugin is not recommended for use with Typescript because Typescript already does this check itself, and this plugin will incorrectly flag method overloads in type definitions.

Configuration

  1. Install babel-eslint and this plugin: npm install --save-dev babel-eslint eslint-plugin-no-dupe-class-fields or yarn add --dev babel-eslint eslint-plugin-no-dupe-class-fields.
  2. Set the ESLint config's parser property to babel-eslint.
  3. Add plugin:no-dupe-class-fields/recommended to the ESLint config's extends list.

Example:

{
  "root": true,
  "parser": "babel-eslint",
  "env": {
    "node": true,
    "es6": true
  },
  "extends": ["eslint:recommended", "plugin:no-dupe-class-fields/recommended"],
  "plugins": [],
  "rules": {
    "indent": ["error", 2]
  }
}

About

(Discontinued) ESLint rule to detect when a class has duplicate properties and methods

Resources

License

Stars

Watchers

Forks

Packages

No packages published