From f1ca888388c215a9448af5aecf585148b2eff2e7 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Tue, 26 Jul 2016 07:14:10 -0400 Subject: [PATCH] add `allowComputed` option to `namespace` (fixes #456) --- CHANGELOG.md | 10 +++++++++- docs/rules/namespace.md | 18 ++++++++++++++++++ src/rules/namespace.js | 33 +++++++++++++++++++++++++++++---- tests/src/rules/namespace.js | 6 ++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ba668a5..7084e49ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). ## [Unreleased] -### Modified +### Added +- `allowComputed` option for [`namespace`] rule. If set to `true`, won't report + computed member references to namespaces. (see [#456]) + +### Changed - Modified [`no-nodejs-modules`] error message to include the module's name ([#453], [#461]) ## [1.12.0] - 2016-07-26 @@ -307,7 +311,11 @@ for info on changes for earlier releases. [#157]: https://github.com/benmosher/eslint-plugin-import/pull/157 [#314]: https://github.com/benmosher/eslint-plugin-import/pull/314 +<<<<<<< 30e55b65d6a8a08585aa72c51f8e5871d9a832a6 [#453]: https://github.com/benmosher/eslint-plugin-import/issues/453 +======= +[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456 +>>>>>>> add `allowComputed` option to `namespace` (fixes #456) [#441]: https://github.com/benmosher/eslint-plugin-import/issues/441 [#423]: https://github.com/benmosher/eslint-plugin-import/issues/423 [#415]: https://github.com/benmosher/eslint-plugin-import/issues/415 diff --git a/docs/rules/namespace.md b/docs/rules/namespace.md index d88457f94..6ff8531d9 100644 --- a/docs/rules/namespace.md +++ b/docs/rules/namespace.md @@ -67,6 +67,24 @@ function deepTrouble() { ``` +### Options + +#### `allowComputed` + +Defaults to `false`. When false, will report the following: + +```js +/*eslint import/namespace: [2, { allowComputed: false }]*/ +import * as a from './a' + +function f(x) { + return a[x] // Unable to validate computed reference to imported namespace 'a'. +} +``` + +When set to `true`, the above computed namespace member reference is allowed, but +still can't be statically analyzed any further. + ## Further Reading - Lee Byron's [ES7] export proposal diff --git a/src/rules/namespace.js b/src/rules/namespace.js index 9b4d375d4..827a8e70f 100644 --- a/src/rules/namespace.js +++ b/src/rules/namespace.js @@ -4,7 +4,30 @@ import Exports from '../core/getExports' import importDeclaration from '../importDeclaration' import declaredScope from '../core/declaredScope' -module.exports = function (context) { +exports.meta = { + schema: [ + { + 'type': 'object', + 'properties': { + 'allowComputed': { + 'description': + 'If `false`, will report computed (and thus, un-lintable) references ' + + 'to namespace members.', + 'type': 'boolean', + 'default': false, + }, + }, + 'additionalProperties': false, + }, + ], +} + +exports.create = function namespaceRule(context) { + + // read options + const { + allowComputed = false, + } = context.options[0] || {} const namespaces = new Map() @@ -93,9 +116,11 @@ module.exports = function (context) { dereference.type === 'MemberExpression') { if (dereference.computed) { - context.report(dereference.property, - 'Unable to validate computed reference to imported namespace \'' + - dereference.object.name + '\'.') + if (!allowComputed) { + context.report(dereference.property, + 'Unable to validate computed reference to imported namespace \'' + + dereference.object.name + '\'.') + } return } diff --git a/tests/src/rules/namespace.js b/tests/src/rules/namespace.js index 43e0ea7d4..3e4ef095c 100644 --- a/tests/src/rules/namespace.js +++ b/tests/src/rules/namespace.js @@ -87,6 +87,12 @@ const valid = [ parser: 'babel-eslint', }), + // #456: optionally ignore computed references + test({ + code: `import * as names from './named-exports'; console.log(names['a']);`, + options: [{ allowComputed: true }], + }), + ...SYNTAX_CASES, ]