From 5d37ad442c1ddb57e866c70d4e48a1680af40209 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 22 Aug 2016 20:17:18 +0200 Subject: [PATCH] Add `allow` option to `no-nodejs-modules` rule (fixes #452) --- CHANGELOG.md | 3 ++- docs/rules/no-nodejs-modules.md | 9 +++++++++ src/rules/no-nodejs-modules.js | 11 ++++++---- tests/src/rules/no-nodejs-modules.js | 30 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3c3991969..49e1910952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). ## [Unreleased] - +- Added an `allow` option to [`no-nodejs-modules`] to allow exceptions ([#452], [#452]). ## [1.14.0] - 2016-08-22 ### Added @@ -331,6 +331,7 @@ for info on changes for earlier releases. [#478]: https://github.com/benmosher/eslint-plugin-import/issues/478 [#456]: https://github.com/benmosher/eslint-plugin-import/issues/456 [#453]: https://github.com/benmosher/eslint-plugin-import/issues/453 +[#452]: https://github.com/benmosher/eslint-plugin-import/issues/452 [#441]: https://github.com/benmosher/eslint-plugin-import/issues/441 [#423]: https://github.com/benmosher/eslint-plugin-import/issues/423 [#416]: https://github.com/benmosher/eslint-plugin-import/issues/416 diff --git a/docs/rules/no-nodejs-modules.md b/docs/rules/no-nodejs-modules.md index df3c27e4e5..59f1e36937 100644 --- a/docs/rules/no-nodejs-modules.md +++ b/docs/rules/no-nodejs-modules.md @@ -2,6 +2,12 @@ Forbid the use of Node.js builtin modules. Can be useful for client-side web projects that do not have access to those modules. +### Options + +This rule supports the following options: + +- `allow`: Array of names of allowed modules. Defaults to an empty array. + ## Rule Details ### Fail @@ -24,6 +30,9 @@ import foo from './foo'; var _ = require('lodash'); var foo = require('foo'); var foo = require('./foo'); + +/* eslint import/no-nodejs-modules: ["error", {"allow": ["path"]}] */ +import path from 'path'; ``` ## When Not To Use It diff --git a/src/rules/no-nodejs-modules.js b/src/rules/no-nodejs-modules.js index 7cdb94ec9c..f866330b43 100644 --- a/src/rules/no-nodejs-modules.js +++ b/src/rules/no-nodejs-modules.js @@ -1,20 +1,23 @@ import importType from '../core/importType' import isStaticRequire from '../core/staticRequire' -function reportIfMissing(context, node, name) { - if (importType(name, context) === 'builtin') { +function reportIfMissing(context, node, allowed, name) { + if (!allowed.includes(name) && importType(name, context) === 'builtin') { context.report(node, 'Do not import Node.js builtin module "' + name + '"') } } module.exports = function (context) { + const options = context.options[0] || {} + const allowed = options.allow || [] + return { ImportDeclaration: function handleImports(node) { - reportIfMissing(context, node, node.source.value) + reportIfMissing(context, node, allowed, node.source.value) }, CallExpression: function handleRequires(node) { if (isStaticRequire(node)) { - reportIfMissing(context, node, node.arguments[0].value) + reportIfMissing(context, node, allowed, node.arguments[0].value) } }, } diff --git a/tests/src/rules/no-nodejs-modules.js b/tests/src/rules/no-nodejs-modules.js index 826640e317..edfb8e7a0c 100644 --- a/tests/src/rules/no-nodejs-modules.js +++ b/tests/src/rules/no-nodejs-modules.js @@ -26,6 +26,36 @@ ruleTester.run('no-nodejs-modules', rule, { test({ code: 'var foo = require("foo")'}), test({ code: 'var foo = require("./")'}), test({ code: 'var foo = require("@scope/foo")'}), + test({ + code: 'import events from "events"', + options: [{ + allow: ['events'], + }], + }), + test({ + code: 'import path from "path"', + options: [{ + allow: ['path'], + }], + }), + test({ + code: 'var events = require("events")', + options: [{ + allow: ['events'], + }], + }), + test({ + code: 'var path = require("path")', + options: [{ + allow: ['path'], + }], + }), + test({ + code: 'import path from "path";import events from "events"', + options: [{ + allow: ['path', 'events'], + }], + }), ], invalid: [ test({