Skip to content

Commit 5d37ad4

Browse files
committed
Add allow option to no-nodejs-modules rule (fixes #452)
1 parent 7995581 commit 5d37ad4

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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-
7+
- Added an `allow` option to [`no-nodejs-modules`] to allow exceptions ([#452], [#452]).
88

99
## [1.14.0] - 2016-08-22
1010
### Added
@@ -331,6 +331,7 @@ for info on changes for earlier releases.
331331
[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
332332
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
333333
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
334+
[#452]: https://github.com/benmosher/eslint-plugin-import/issues/452
334335
[#441]: https://github.com/benmosher/eslint-plugin-import/issues/441
335336
[#423]: https://github.com/benmosher/eslint-plugin-import/issues/423
336337
[#416]: https://github.com/benmosher/eslint-plugin-import/issues/416

docs/rules/no-nodejs-modules.md

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Forbid the use of Node.js builtin modules. Can be useful for client-side web projects that do not have access to those modules.
44

5+
### Options
6+
7+
This rule supports the following options:
8+
9+
- `allow`: Array of names of allowed modules. Defaults to an empty array.
10+
511
## Rule Details
612

713
### Fail
@@ -24,6 +30,9 @@ import foo from './foo';
2430
var _ = require('lodash');
2531
var foo = require('foo');
2632
var foo = require('./foo');
33+
34+
/* eslint import/no-nodejs-modules: ["error", {"allow": ["path"]}] */
35+
import path from 'path';
2736
```
2837

2938
## When Not To Use It

src/rules/no-nodejs-modules.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import importType from '../core/importType'
22
import isStaticRequire from '../core/staticRequire'
33

4-
function reportIfMissing(context, node, name) {
5-
if (importType(name, context) === 'builtin') {
4+
function reportIfMissing(context, node, allowed, name) {
5+
if (!allowed.includes(name) && importType(name, context) === 'builtin') {
66
context.report(node, 'Do not import Node.js builtin module "' + name + '"')
77
}
88
}
99

1010
module.exports = function (context) {
11+
const options = context.options[0] || {}
12+
const allowed = options.allow || []
13+
1114
return {
1215
ImportDeclaration: function handleImports(node) {
13-
reportIfMissing(context, node, node.source.value)
16+
reportIfMissing(context, node, allowed, node.source.value)
1417
},
1518
CallExpression: function handleRequires(node) {
1619
if (isStaticRequire(node)) {
17-
reportIfMissing(context, node, node.arguments[0].value)
20+
reportIfMissing(context, node, allowed, node.arguments[0].value)
1821
}
1922
},
2023
}

tests/src/rules/no-nodejs-modules.js

+30
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ ruleTester.run('no-nodejs-modules', rule, {
2626
test({ code: 'var foo = require("foo")'}),
2727
test({ code: 'var foo = require("./")'}),
2828
test({ code: 'var foo = require("@scope/foo")'}),
29+
test({
30+
code: 'import events from "events"',
31+
options: [{
32+
allow: ['events'],
33+
}],
34+
}),
35+
test({
36+
code: 'import path from "path"',
37+
options: [{
38+
allow: ['path'],
39+
}],
40+
}),
41+
test({
42+
code: 'var events = require("events")',
43+
options: [{
44+
allow: ['events'],
45+
}],
46+
}),
47+
test({
48+
code: 'var path = require("path")',
49+
options: [{
50+
allow: ['path'],
51+
}],
52+
}),
53+
test({
54+
code: 'import path from "path";import events from "events"',
55+
options: [{
56+
allow: ['path', 'events'],
57+
}],
58+
}),
2959
],
3060
invalid: [
3161
test({

0 commit comments

Comments
 (0)