Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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). | ||
|
||
|
||
## Rule Details | ||
|
||
This rule aims to discourage the use of `class` and to encourage other approaches such as functional inheritance, | ||
prototypal inheritance or object composition. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
class Foo { | ||
constructor() { | ||
this.foo = 'bar'; | ||
} | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
Existing ES6 JavaScript projects that are heavily using classical inheritance may not want to apply this rule if the cost of removing `class` is too costly. | ||
|
||
## Further Reading | ||
|
||
- https://www.youtube.com/watch?v=PSGEjv3Tqo0 | ||
- https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3 | ||
- https://medium.com/@_ericelliott/how-to-fix-the-es6-class-keyword-2d42bb3f4caf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @fileoverview Avoid using class inheritance, instead favor prototypes or object composition. | ||
* @author Nicola Molinari | ||
* @copyright 2015 Nicola Molinari. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
"ClassDeclaration": function(node) { | ||
context.report(node, "Unexpected class declaration (with ecmaFeatures.classes enabled)" + | ||
", you should use other methods like prototypal inheritance or object composition."); | ||
} | ||
|
||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @fileoverview Avoid using class inheritance, instead favor prototypes or object composition. | ||
* @author Nicola Molinari | ||
* @copyright 2015 Nicola Molinari. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslint = require("../../../lib/eslint"), | ||
ESLintTester = require("eslint-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest("lib/rules/no-class", { | ||
|
||
valid: [ | ||
{ | ||
code: "function foo() {}", | ||
ecmaFeatures: { classes: true } | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "class Foo { constructor() { this.foo = 'bar'; } }", | ||
ecmaFeatures: { classes: true }, | ||
errors: [{ | ||
message: "Unexpected class declaration (with ecmaFeatures.classes enabled)" + | ||
", you should use other methods like prototypal inheritance or object composition.", | ||
type: "ClassDeclaration" | ||
}] | ||
} | ||
] | ||
}); |