Skip to content

Commit

Permalink
add allowComputed option to namespace (fixes #456)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jul 27, 2016
1 parent 30e55b6 commit f1ca888
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docs/rules/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 29 additions & 4 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]

Expand Down

0 comments on commit f1ca888

Please sign in to comment.