New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: no-class rule (fixes #2097) #2096
Conversation
I really want to have this rule in ESLint 👍. We require an open issue for every pull request and the commit messages have to be in a specific format. Please take a look at our contribution guidelines |
@lo1tuma yeah, I read that (almost :) ) but I missed the PS: already signed to CLA ;) |
@lo1tuma should be ok now (issue + commit message) |
@lo1tuma my bad, sorry. How about now? |
# Avoid use of `class` (no-class) | ||
|
||
ECMAScript 6 allows programmers to create `class` to "help" those coming from a OOP language, such as Java or C++, to create constructors, thus multiple instances of an object. | ||
In the JS community this is considered one of the bad parts (you can google it to read more about it). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove the comment to use google, because you already provided links for further reading at the bottom.
Overall it looks good, I’ve just added a few more comments. |
@lo1tuma made adjustments according to your comments, thanks 👍 |
We still need a rationalization for this rule. There is no consensus on classes being a "bad part", and I see no further rationalization in the issue. You can already disable classes using |
@nzakas well, disabling classes in the $ cat test-class.js
class Foo {
constructor() {
this.foo = 'bar'
}
}
$ cat .eslintrc
/* http://eslint.org/docs/rules/ */
{
"parser": "babel-eslint",
"ecmaFeatures": {
"arrowFunctions": true,
"blockBindings": true,
"classes": false,
"defaultParams": true,
"jsx": true,
"templateStrings": true
},
"env": {
"browser": true,
"jasmine": true,
"node": true
},
"rules": {
"camelcase": 2,
"consistent-return": 2,
"curly": [2, "multi"],
"eol-last": 2,
"eqeqeq": 2,
"new-cap": [2, {"capIsNew": false}],
"no-eq-null": 2,
"no-mixed-requires": 0,
"no-mixed-spaces-and-tabs": 2,
"no-multiple-empty-lines": [2, {max: 2}],
"no-trailing-spaces": 2,
"no-undef": 0,
"no-underscore-dangle": 0,
"no-unused-vars": 2,
"no-var": 2,
"quotes": [2, "single"],
"semi": 0,
"space-before-blocks": [2, "always"],
"space-before-function-parentheses": [2, "never"],
"space-return-throw-case": 2,
"strict": 0
}
}
$ eslint --ext .js,.jsx test-class.js
$ echo $? |
@nzakas The problems with class and constructors are actually quite well known and quite well documented. There's a whole section on object instantiation in the GoF "Design Patterns" book that exists to get around a catalog of well documented famous OO design problems:
Google "class considered harmful" for lots of references dating back to before JS was a twinkle in Brendan Eich's eye. The current class spec is riddled with problems that can prevent the common class -> factory refactor. That need is common enough that it is included in Martin Fowler's refactoring catalog from the seminal refactoring book. For more, watch "Classical Inheritance is Obsolete: How to Think in Prototypal OO" |
@nzakas as I said here #2097 (comment) it may look like a |
So in the end |
If your gripe is with inheritance, then ban |
@ericelliott I read your article and while I understand your point of view, I don't share it. The bar for core ESLint rules is relatively high, and it's far too early to declare anything in ES6 as "bad". The beauty of ESLint, though, is that you can always create and share your own rules to further any agenda that you want. We made it this way so we don't all have to agree in order for ESLint to be useful. |
Hi everybody,
this is my first attempt to add a new rule, so any feedback / help is much appreciated :)
The aim of the rule is to forbid the use of
class
(ES6) to encourage other ways to do inheritance.Thanks in advance