From 76e1e4c1de263948f9c35bf43c4de526b17bebb9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 2 Apr 2016 18:08:27 -0700 Subject: [PATCH] [eslint config] [breaking] Add `no-dupe-class-members` rule + section. Closes #785. --- README.md | 30 +++++++++++++++++++--- packages/eslint-config-airbnb/rules/es6.js | 3 +++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ff42af3d..03c422d9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Other Style Guides 1. [Strings](#strings) 1. [Functions](#functions) 1. [Arrow Functions](#arrow-functions) - 1. [Constructors](#constructors) + 1. [Classes & Constructors](#constructors) 1. [Modules](#modules) 1. [Iterators and Generators](#iterators-and-generators) 1. [Properties](#properties) @@ -894,7 +894,7 @@ Other Style Guides **[⬆ back to top](#table-of-contents)** -## Constructors +## Classes & Constructors - [9.1](#constructors--use-class) Always use `class`. Avoid manipulating `prototype` directly. @@ -1008,7 +1008,7 @@ Other Style Guides ``` - - [9.5](#constructors--no-useless) Classes have a default constructor if one is not specified. An empty constructor function or one that just delegates to a parent class is unnecessary. [`no-useless-constructor`](http://eslint.org/docs/rules/no-useless-constructor) + - [9.5](#constructors--no-useless) Classes have a default constructor if one is not specified. An empty constructor function or one that just delegates to a parent class is unnecessary. eslint: [`no-useless-constructor`](http://eslint.org/docs/rules/no-useless-constructor) ```javascript // bad @@ -1036,6 +1036,30 @@ Other Style Guides } ``` + + - [9.6](#classes--no-duplicate-members) Avoid duplicate class members. eslint: [`no-dupe-class-members`](http://eslint.org/docs/rules/no-dupe-class-members) + + > Why? Duplicate class member declarations will silently prefer the last one - having duplicates is almost certainly a bug. + + ```javascript + // bad + class Foo { + bar() { return 1; } + bar() { return 2; } + } + + // good + class Foo { + bar() { return 1; } + } + + // good + class Foo { + bar() { return 2; } + } + ``` + + **[⬆ back to top](#table-of-contents)** diff --git a/packages/eslint-config-airbnb/rules/es6.js b/packages/eslint-config-airbnb/rules/es6.js index 78a43b94..a826da8d 100644 --- a/packages/eslint-config-airbnb/rules/es6.js +++ b/packages/eslint-config-airbnb/rules/es6.js @@ -35,6 +35,9 @@ module.exports = { }], // disallow modifying variables that are declared using const 'no-const-assign': 2, + // disallow duplicate class members + // http://eslint.org/docs/rules/no-dupe-class-members + 'no-dupe-class-members': 2, // disallow importing from the same path more than once // http://eslint.org/docs/rules/no-duplicate-imports 'no-duplicate-imports': 2,