Skip to content

Commit

Permalink
New: no-class rule (fixes #2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Mar 18, 2015
1 parent 2e10725 commit 694ce37
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Expand Up @@ -15,6 +15,7 @@
"no-bitwise": 0,
"no-caller": 2,
"no-catch-shadow": 2,
"no-class": 0,
"no-comma-dangle": 0,
"no-cond-assign": 2,
"no-console": 2,
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -178,6 +178,7 @@ These rules are purely matters of style and are quite subjective.

These rules are only relevant to ES6 environments and are off by default.

* [no-class](no-class.md) - avoid use of `class` (off by default)
* [no-var](no-var.md) - require `let` or `const` instead of `var` (off by default)
* [generator-star](generator-star.md) - **(deprecated)** enforce the position of the `*` in generator functions (off by default)
* [generator-star-spacing](generator-star-spacing.md) - enforce the spacing around the `*` in generator functions (off by default)
Expand Down
30 changes: 30 additions & 0 deletions docs/rules/no-class.md
@@ -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
27 changes: 27 additions & 0 deletions lib/rules/no-class.js
@@ -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.");
}

};

};
40 changes: 40 additions & 0 deletions tests/lib/rules/no-class.js
@@ -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"
}]
}
]
});

0 comments on commit 694ce37

Please sign in to comment.