Skip to content

Commit 097ca08

Browse files
committed
Add no-events rule
1 parent d16933c commit 097ca08

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
55

66
## [Unreleased]
7-
- (none)
7+
### Added
8+
- Added [`no-events`] rule
89

910
## [1.1.0] - 2016-06-16
1011
### Added

docs/rules/no-events.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Forbid the use of the `events` module
2+
3+
The use of `EventEmitter` with the `events` module provided by Node.js promotes implicit side-effects by emitting and listening to events. Instead of `events`, you should prefer activating the wanted effects by calling the functions you wish to use explicitly.
4+
5+
The rule will simply warn whenever the `events` module is imported.
6+
7+
### Fail
8+
9+
```js
10+
import EventEmitter from 'events';
11+
12+
const EventEmitter = require('events')
13+
```

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
rules: {
55
'no-class': require('./rules/no-class'),
66
'no-delete': require('./rules/no-delete'),
7+
'no-events': require('./rules/no-let'),
78
'no-let': require('./rules/no-let'),
89
'no-loops': require('./rules/no-loops'),
910
'no-mutating-assign': require('./rules/no-mutating-assign'),
@@ -19,6 +20,7 @@ module.exports = {
1920
'no-var': 'error',
2021
'fp/no-class': 'error',
2122
'fp/no-delete': 'error',
23+
'fp/no-events': 'error',
2224
'fp/no-let': 'error',
2325
'fp/no-loops': 'error',
2426
'fp/no-mutating-assign': 'error',

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Configure it in `package.json`.
2727
"no-var": "error",
2828
"fp/no-class": "error",
2929
"fp/no-delete": "error",
30+
"fp/no-events": "error",
3031
"fp/no-let": "error",
3132
"fp/no-loops": "error",
3233
"fp/no-mutating-assign": "error",
@@ -45,6 +46,7 @@ Configure it in `package.json`.
4546

4647
- [no-class](docs/rules/no-class.md) - Forbid the use of `class`.
4748
- [no-delete](docs/rules/no-delete.md) - Forbid the use of `delete`.
49+
- [no-events](docs/rules/no-events.md) - Forbid the use of the `events` module.
4850
- [no-let](docs/rules/no-let.md) - Forbid the use of `let`.
4951
- [no-loops](docs/rules/no-loops.md) - Forbid the use of loops.
5052
- [no-mutating-assign](docs/rules/no-mutating-assign.md) - Forbid the use of [`Object.assign()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) with a variable as first argument.

rules/no-events.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
function isStaticRequire(node) {
4+
return node &&
5+
node.callee &&
6+
node.callee.type === 'Identifier' &&
7+
node.callee.name === 'require' &&
8+
node.arguments.length === 1 &&
9+
node.arguments[0].type === 'Literal';
10+
}
11+
12+
function report(context, node) {
13+
context.report({
14+
node: node,
15+
message: 'Unallowed use of `events`'
16+
});
17+
}
18+
19+
module.exports = function (context) {
20+
return {
21+
ImportDeclaration: function (node) {
22+
if (node.source.value === 'events') {
23+
report(context, node);
24+
}
25+
},
26+
CallExpression: function (node) {
27+
if (isStaticRequire(node) && node.arguments[0].value === 'events') {
28+
report(context, node);
29+
}
30+
}
31+
};
32+
};

test/no-events.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import test from 'ava';
2+
import avaRuleTester from 'eslint-ava-rule-tester';
3+
import rule from '../rules/no-events';
4+
5+
const ruleTester = avaRuleTester(test, {
6+
env: {
7+
es6: true
8+
},
9+
parserOptions: {
10+
sourceType: 'module'
11+
}
12+
});
13+
14+
const error = {
15+
ruleId: 'no-events',
16+
message: 'Unallowed use of `events`'
17+
};
18+
19+
ruleTester.run('no-events', rule, {
20+
valid: [
21+
'import foo from "foo"',
22+
'import {bar} from "foo"',
23+
'import events from "eventsE"',
24+
'import events from "Eevents"',
25+
'require("foo");',
26+
'require("eventsE");',
27+
'require("Efoo");',
28+
'var events;'
29+
],
30+
invalid: [
31+
{
32+
code: 'import EventEmitter from "events";',
33+
errors: [error]
34+
},
35+
{
36+
code: 'import {listen} from "events";',
37+
errors: [error]
38+
},
39+
{
40+
code: 'require("events");',
41+
errors: [error]
42+
}
43+
]
44+
});

0 commit comments

Comments
 (0)