From d8064da63405d25573847f511d2c63747f28a646 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 | 4 ++++ docs/rules/namespace.md | 18 ++++++++++++++++++ src/rules/namespace.js | 33 +++++++++++++++++++++++++++++---- tests/src/rules/namespace.js | 6 ++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2d25b7775..b65e545b7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). ## [Unreleased] +### Added +- `allowComputed` option for [`namespace`] rule. If set to `true`, won't report + computed member references to namespaces. (see [#456]) ## [1.12.0] - 2016-07-26 ### Added @@ -304,6 +307,7 @@ 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 +[#456]: https://github.com/benmosher/eslint-plugin-import/issues/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 d88457f94e..6ff8531d91 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 9b4d375d46..827a8e70f9 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 43e0ea7d47..3e4ef095cb 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, ]